jQuery( document ).on(
	'click',
	'.js--button[data-integration=' + __jsDHLPolskaVars.slug + ']',
	function ( e ) {
		var id = jQuery( this ).attr( 'data-id' );
		var action = jQuery( this ).attr( 'data-action' );

		var container = jQuery( this ).closest( '.flexible_shipping_shipment_content' );
		var new_package_field = jQuery( '.js--new-package-field', container );
		if ( new_package_field.length ) {
			container.data( 'new-package-template', new_package_field.html() );
			new_package_field.remove();
		}
		jQuery( '.js--button[data-id=' + id + '][data-integration=' + __jsDHLPolskaVars.slug + ']' ).prop( 'disabled', true );

		fs_ajax( this, id, action );

		return false;
	}
);

jQuery( document ).on(
	'click',
	'.js--delete-package[data-integration=' + __jsDHLPolskaVars.slug + ']',
	function ( e ) {
		var container = jQuery( this ).closest( '.js--packages-container' );
		if ( jQuery( '.js--package-row', container ).length > 1 ) {
			jQuery( this ).closest( '.js--package-row' ).remove();
		}

		return false;
	}
);

jQuery( document ).on(
	'click',
	'.js--add-package[data-integration=' + __jsDHLPolskaVars.slug + ']',
	function ( e ) {
		var container = jQuery( this ).closest( '.flexible_shipping_shipment_content' );
		var packages_container = jQuery( '.js--packages-container', container );
		var new_package_content = jQuery( '.js--new-package-field', container ).html();
		if ( ! new_package_content ) {
			new_package_content = container.data( 'new-package-template' );
		}
		if ( ! new_package_content ) {
			return false;
		}

		var timestamp = new Date().getUTCMilliseconds();

		packages_container.append( new_package_content.replace( /\[0\]/g, '[' + timestamp + ']' ) );

		return false;
	}
);

jQuery( document ).on(
	'change',
	'.js--conditional-field',
	function ( e ) {
		var field = jQuery( this );
		var container = field.closest( '.postbox ' );

		var data_fields = field.attr( 'data-fields' ) || field.parent().attr( 'data-fields' );
		if ( ! data_fields ) {
			return;
		}
		var field_depends = data_fields.split( ',' );

		for ( const field_name of field_depends ) {
			var field_container = jQuery( '[name=' + field_name + ']', container ).closest( '.form-row' );

			if ( jQuery( this ).prop( 'checked' ) ) {
				field_container.removeClass( 'hidden' );
			} else {
				field_container.addClass( 'hidden' );
			}
		}
	}
);

jQuery( document ).on(
	'change',
	'.js--dhl-service-select',
	function ( e ) {
		var container = jQuery( this ).closest( '.flexible_shipping_shipment_content' );
		var parcelshop_info_container = jQuery( '.js--parcelshop-info-container', container );
		var map_button_container = jQuery( '.js--service-point-map-button-container', container );

		if ( jQuery( this ).val() === __jsDHLPolskaVars.service_point ) {
			parcelshop_info_container.removeClass( 'hidden' );
			map_button_container.removeClass( 'hidden' );
		} else {
			parcelshop_info_container.addClass( 'hidden' );
			map_button_container.addClass( 'hidden' );
		}
	}
);

var active_shipment_container = null;

window.addEventListener(
	'message',
	function ( event ) {
		if ( event.data && event.origin && event.origin === __jsDHLPolskaVars.map_url && active_shipment_container ) {
			let point = null;
			if ( typeof event.data === 'object' ) {
				point = event.data;
			} else {
				try {
					point = JSON.parse( event.data );
				} catch ( e ) {
					return;
				}
			}

			if ( point && point.sap ) {
				jQuery( '.js--parcelshop-id', active_shipment_container ).val( point.id );
				jQuery( '.js--parcelshop-sap', active_shipment_container ).val( point.sap );
				jQuery( '.js--parcelshop-name', active_shipment_container ).val( point.name );
				jQuery( '.js--parcelshop-zip', active_shipment_container ).val( point.zip );
				jQuery( '.js--parcelshop-street', active_shipment_container ).val( point.street );
				jQuery( '.js--parcelshop-city', active_shipment_container ).val( point.city );
				jQuery( '.js--parcelshop-streetNo', active_shipment_container ).val( point.streetNo );
				jQuery( '.js--parcelshop-houseNo', active_shipment_container ).val( point.houseNo );

				var info_html = '<strong>' + point.name + '</strong><br/>' +
					point.street + ' ' + point.streetNo + '<br/>' +
					point.zip + ' ' + point.city + '<br/>' +
					'(' + point.sap + ')';

				var point_type_label = active_shipment_container.data( 'point-type-label' );
				if ( point_type_label ) {
					info_html += '<div class="js--parcelshop-point-type-line"><strong>' + __jsDHLPolskaVars.point_type_label + ':</strong> <span class="js--parcelshop-point-type-label">' + point_type_label + '</span></div>';
				}

				jQuery( '.js--parcelshop-info', active_shipment_container ).html( info_html );

				if ( typeof tb_remove === 'function' ) {
					tb_remove();
				}
				active_shipment_container = null;
			}
		}
	}
);

jQuery( document ).on(
	'click',
	'.js--dhl-polska-map-button',
	function ( e ) {
		active_shipment_container = jQuery( this ).closest( '.flexible_shipping_shipment_content' );

		var pointType = jQuery( this ).data( 'point-type' );
		if ( pointType ) {
			active_shipment_container.data( 'point-type', pointType );
			active_shipment_container.data( 'point-type-label', jQuery( this ).text().trim() );
			jQuery( '.js--parcelshop-point-type', active_shipment_container ).val( pointType );
		} else {
			active_shipment_container.removeData( 'point-type' );
			active_shipment_container.removeData( 'point-type-label' );
		}

		var width = jQuery( window ).width() * 0.9;
		var height = jQuery( window ).height() * 0.9;
		var country = jQuery( this ).data( 'country' );
		var pointType = jQuery( this ).data( 'point-type' );
		var params = new URLSearchParams();
		var normalized_country = ( country || '' ).toString().trim().toUpperCase();
		if ( country && 'PL' !== normalized_country ) {
			params.append( 'country', country );
		}
		if ( pointType ) {
			params.append( 'ptype', pointType );
		}
		params.append( 'TB_iframe', 'true' );
		params.append( 'width', width );
		params.append( 'height', height );
		var url = __jsDHLPolskaVars.map_url + '/mapa?' + params.toString();

		tb_show( jQuery( this ).text().trim(), url );
		jQuery( '#TB_iframeContent' ).attr( 'allow', 'geolocation' );
		setTimeout(
			function () {
				jQuery( '#TB_iframeContent' ).attr( 'allow', 'geolocation' );
			},
			300
		);

		return false;
	}
);

function dhl_polska_init() {
	jQuery( '.flexible_shipping_shipment label.checkbox[disabled] :checkbox' ).prop( 'disabled', true );
}

jQuery( document ).on(
	'flexible_shipping_ajax_fs_action_after',
	function () {
		dhl_polska_init();
	}
);

dhl_polska_init();
