import $ from 'jquery';
import { calculate_total_weight } from './calculate_total_weight';
import { init_tooltips } from '../init_tooltips';
import { set_required_element_ids } from './required_element_ids';
import { initialize_modal_wrapper } from './initialize_modal_wrapper';
import { handle_response_data } from './handle_response_data';
import { configure_tb_window } from './configure_tb_window';


export const show_form_with_product_dimensions = (
	response: MyResponse<ProductDimensionsData[]>,
	order_id: string,
	type: string,
): void => {
	if ( ! response ) {
		return;
	}
	const MODAL_ID = 'wetail-shipping-engine-update-order-items-form';

	const WARNING_MESSAGE = window._wetail_shipping.i18n[
		'Your order can not be processed. Please enter the weight and/or dimensions of your package.'
		];

	const MODAL_TITLE =
		window._wetail_shipping.i18n[ 'Missing order info' ] +
		' - ' +
		window._wetail_shipping.i18n[ 'Order' ] +
		' #' +
		order_id;

	const MODAL_URL = `/?TB_inline&width=500&height=${ $( window ).height()! * 0.8 }&inlineId=${ MODAL_ID }`;

	const form_wrapper = initialize_modal_wrapper( MODAL_ID, WARNING_MESSAGE );

	handle_response_data( response.data, form_wrapper, order_id, type );

	$( 'body' ).prepend( form_wrapper );

	// @ts-ignore
	tb_show( MODAL_TITLE, MODAL_URL );

	configure_tb_window( order_id );

	calculate_total_weight();

	init_tooltips( '.total-weight__tip' );

	set_required_element_ids();
};
