<?php
/**
 * @package DhlPolska\Integration\FlexibleShipping
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettingsFields;
use DhlPolska\Shipment\DhlServices;

?><script type="text/javascript">
	jQuery( document ).ready( function () {

		function dhl_polska_toggle_point_type() {
			var point_type_field = jQuery( '#woocommerce_flexible_shipping_dhl_polska_point_type' );
			if ( ! point_type_field.length ) {
				return;
			}

			var integration = jQuery( '#woocommerce_flexible_shipping_method_integration' ).val();
			var service = jQuery( '#woocommerce_flexible_shipping_dhl_polska_service' ).val();
			var should_show = integration === '<?php echo esc_attr( WooCommerceDHLPolskaSettingsFields::SHIPPING_METHOD_ID ); ?>'
				&& service === '<?php echo esc_attr( DhlServices::SERVICE_POINT ); ?>';

			point_type_field.closest( 'tr' ).toggle( should_show );
		}

		function dhl_polska_integration() {
			if ( jQuery( '#woocommerce_flexible_shipping_method_integration' ).val() === '<?php echo esc_attr( WooCommerceDHLPolskaSettingsFields::SHIPPING_METHOD_ID ); ?>' ) {
				jQuery( '.js--field-<?php echo esc_attr( WooCommerceDHLPolskaSettingsFields::SHIPPING_METHOD_ID ); ?>' ).closest( 'tr' ).show();
			} else {
				jQuery( '.js--field-<?php echo esc_attr( WooCommerceDHLPolskaSettingsFields::SHIPPING_METHOD_ID ); ?>' ).closest( 'tr' ).hide();
			}

			dhl_polska_toggle_point_type();
		}

		jQuery( '#woocommerce_flexible_shipping_method_integration' ).change( function () {
			dhl_polska_integration();
		} );

		jQuery( '#woocommerce_flexible_shipping_dhl_polska_service' ).change( function () {
			dhl_polska_toggle_point_type();
		} );

		dhl_polska_integration();
	} );
</script>
