jQuery(
	function ( $ ) {
		if ( typeof window.dhlPolskaShippingSettingsConfig !== 'object' ) {
			return;
		}

		var config = window.dhlPolskaShippingSettingsConfig;
		var flexibleShipping = config.flexible_shipping || {};
		var instanceSettings = config.instance_settings || {};

		function getLogoVariants() {
			if ( ! config.logo_variants || typeof config.logo_variants !== 'object' || Array.isArray( config.logo_variants ) ) {
				return {};
			}

			return config.logo_variants;
		}

		function getDefaultLogoVariant() {
			var logoVariants = getLogoVariants();
			var configuredDefault = ( config.default_logo_variant || '' ).toString().trim();

			if ( configuredDefault && Object.prototype.hasOwnProperty.call( logoVariants, configuredDefault ) ) {
				return configuredDefault;
			}

			var variantKeys = Object.keys( logoVariants );

			return variantKeys.length ? variantKeys[ 0 ] : '';
		}

		function normalizeLogoVariant( value ) {
			var logoVariants = getLogoVariants();
			var normalizedLogoVariant = ( value || '' ).toString().trim();

			if ( normalizedLogoVariant && Object.prototype.hasOwnProperty.call( logoVariants, normalizedLogoVariant ) ) {
				return normalizedLogoVariant;
			}

			return getDefaultLogoVariant();
		}

		function getLogoUrlForVariant( value ) {
			var logoVariants = getLogoVariants();
			var logoVariant = normalizeLogoVariant( value );

			if ( ! logoVariant || ! Object.prototype.hasOwnProperty.call( logoVariants, logoVariant ) ) {
				return '';
			}

			return logoVariants[ logoVariant ];
		}

		function ensureLogoPreviewContainer( field ) {
			if ( ! field.length ) {
				return $();
			}

			var previewClass = ( config.logo_preview_class || 'dhl-polska-logo-variant-preview' ).toString().trim();
			var fieldset = field.closest( 'fieldset' );

			if ( ! fieldset.length ) {
				fieldset = field.parent();
			}

			if ( ! fieldset.length ) {
				return $();
			}

			var preview = fieldset.find( '.' + previewClass ).first();
			if ( ! preview.length ) {
				preview = $( '<div/>' ).addClass( previewClass );
				preview.css( 'margin-top', '8px' );
				fieldset.append( preview );
			}

			return preview;
		}

		function renderLogoPreview( field ) {
			if ( ! field.length ) {
				return;
			}

			var preview = ensureLogoPreviewContainer( field );
			if ( ! preview.length ) {
				return;
			}

			var logoUrl = getLogoUrlForVariant( field.val() );
			preview.empty();

			if ( ! logoUrl ) {
				preview.hide();
				return;
			}

				var image = $( '<img/>' )
					.attr( 'src', logoUrl )
					.attr( 'alt', config.logo_alt || '' )
					.attr( 'loading', 'lazy' )
					.css(
						{
							maxHeight: '40px',
							width: 'auto',
							display: 'block',
						}
					);

			preview.append( image ).show();
		}

		function renderLogoPreviews( context, selector ) {
			if ( ! selector ) {
				return;
			}

			$( context ).find( selector ).each(
				function () {
					renderLogoPreview( $( this ) );
				}
			);
		}

		function renderAllLogoPreviews( context ) {
			var container = context || document;

			renderLogoPreviews( container, flexibleShipping.logo_variant_selector || '' );
			renderLogoPreviews( container, instanceSettings.logo_variant_selector || '' );
		}

		function isCheckboxEnabled( field ) {
			if ( ! field.length ) {
				return false;
			}

			if ( field.is( ':checkbox' ) ) {
				return field.is( ':checked' );
			}

			return [ 'yes', '1', 'true', 'on' ].indexOf( ( field.val() || '' ).toString().toLowerCase() ) !== -1;
		}

		function toggleLogoVariantFieldRow( showLogoField, logoVariantField ) {
			if ( ! logoVariantField.length ) {
				return;
			}

			var shouldShow = isCheckboxEnabled( showLogoField );
			logoVariantField.closest( 'tr' ).toggle( shouldShow );

			if ( shouldShow ) {
				renderLogoPreview( logoVariantField );
			}
		}

		function togglePointTypeRows( fields, shouldShow ) {
			fields.forEach(
				function ( field ) {
					if ( field.length ) {
						field.closest( 'tr' ).toggle( shouldShow );
					}
				}
			);
		}

		function getFields( container, selectors ) {
			var fields = [];
			( selectors || [] ).forEach(
				function ( selector ) {
					fields.push( $( container ).find( selector ) );
				}
			);

			return fields;
		}

		function dhlPolskaToggleFlexibleShippingPointType() {
			var integrationField = $( flexibleShipping.integration_selector || '' );
			var serviceField = $( flexibleShipping.service_selector || '' );
			var pointTypeFields = getFields( document, flexibleShipping.point_type_selectors );

			var shouldShow = integrationField.val() === flexibleShipping.integration_id && serviceField.val() === flexibleShipping.service_point;
			togglePointTypeRows( pointTypeFields, shouldShow );
		}

		function dhlPolskaToggleFlexibleShippingIntegration() {
			var integrationField = $( flexibleShipping.integration_selector || '' );
			var shouldShow = integrationField.val() === flexibleShipping.integration_id;

			$( flexibleShipping.field_selector || '' ).closest( 'tr' ).toggle( shouldShow );
			dhlPolskaToggleFlexibleShippingPointType();
			dhlPolskaToggleFlexibleShippingLogoVariant();
		}

		function dhlPolskaToggleFlexibleShippingLogoVariant() {
			var integrationField = $( flexibleShipping.integration_selector || '' );
			var showLogoField = $( flexibleShipping.show_logo_selector || '' );
			var logoVariantField = $( flexibleShipping.logo_variant_selector || '' );
			var shouldShow = integrationField.val() === flexibleShipping.integration_id;

			if ( ! shouldShow ) {
				logoVariantField.closest( 'tr' ).hide();
				return;
			}

			toggleLogoVariantFieldRow( showLogoField, logoVariantField );
		}

		function dhlPolskaToggleInstanceSettingsPointType( context ) {
			var container = context || document;
			var serviceField = $( container ).find( instanceSettings.service_selector || '' );
			if ( ! serviceField.length ) {
				return;
			}

			var pointTypeFields = getFields( container, instanceSettings.point_type_selectors );
			var shouldShow = serviceField.val() === flexibleShipping.service_point;
			togglePointTypeRows( pointTypeFields, shouldShow );
		}

		function dhlPolskaToggleInstanceSettingsLogoVariant( context ) {
			var container = context || document;
			var showLogoField = $( container ).find( instanceSettings.show_logo_selector || '' );
			var logoVariantField = $( container ).find( instanceSettings.logo_variant_selector || '' );

			toggleLogoVariantFieldRow( showLogoField, logoVariantField );
		}

		$( flexibleShipping.integration_selector || '' ).on(
			'change',
			function () {
				dhlPolskaToggleFlexibleShippingIntegration();
			}
		);

		$( flexibleShipping.service_selector || '' ).on(
			'change',
			function () {
				dhlPolskaToggleFlexibleShippingPointType();
			}
		);

		$( flexibleShipping.show_logo_selector || '' ).on(
			'change',
			function () {
				dhlPolskaToggleFlexibleShippingLogoVariant();
			}
		);

		$( flexibleShipping.logo_variant_selector || '' ).on(
			'change',
			function () {
				renderLogoPreview( $( this ) );
			}
		);

		$( document.body ).on(
			'change',
			instanceSettings.service_selector || '',
			function () {
				dhlPolskaToggleInstanceSettingsPointType( $( this ).closest( 'form' ) );
			}
		);

		$( document.body ).on(
			'change',
			instanceSettings.show_logo_selector || '',
			function () {
				dhlPolskaToggleInstanceSettingsLogoVariant( $( this ).closest( 'form' ) );
			}
		);

		$( document.body ).on(
			'change',
			instanceSettings.logo_variant_selector || '',
			function () {
				renderLogoPreview( $( this ) );
			}
		);

		$( document.body ).on(
			'wc_backbone_modal_loaded',
			function ( event, target ) {
				if ( target === instanceSettings.settings_modal_id ) {
					var modalContext = $( '.wc-backbone-modal-content' );
					dhlPolskaToggleInstanceSettingsPointType( modalContext );
					dhlPolskaToggleInstanceSettingsLogoVariant( modalContext );
				}
			}
		);

		dhlPolskaToggleFlexibleShippingIntegration();
		dhlPolskaToggleInstanceSettingsPointType( document );
		dhlPolskaToggleInstanceSettingsLogoVariant( document );
	}
);
