import $ from 'jquery';
import { show_spinner } from '../spinner/show_spinner';
import { hide_spinner } from '../spinner/hide_spinner';
import { call_ajax } from '../call_ajax';
import { set_validation_passed } from './validation_passed';
import { get_request_data } from './get_request_data';

/**
 * Updates the dimensions of a product.
 *
 * @param {JQuery.SubmitEvent} event - The submit event object.
 * @returns {void}
 */
export const update_product_dimensions = ( event: JQuery.SubmitEvent ): void => {
	event.preventDefault();
	const form = $( event.currentTarget );
	const order_id = form.data( 'order_id' );
	const type = form.data( 'type' );
	const request_data = get_request_data( order_id, form );

	// @ts-ignore
	tb_remove();

	show_spinner( order_id );

	set_validation_passed( order_id, true );

	setTimeout( () => {
		call_ajax( request_data )
			.always( () => {
				hide_spinner( order_id );
			} )
			.done( () => {
				$( `.${ type }[data-order-id="${ order_id }"]` ).trigger( 'click' );
			} );
	}, 500 );
};
