/**
 * ElementSell — barevné rozlišení řádku „Aktivní režim“ podle hodnoty selectu.
 */
( function () {
	'use strict';

	var MODE_CLASSES = [
		'elementsell-thepay-env-demo',
		'elementsell-thepay-env-production',
	];

	function styleEnvironmentControl() {
		var control = document.querySelector( '.elementor-control-environment' );

		if ( ! control ) {
			return;
		}

		var select = control.querySelector( 'select[data-setting="environment"]' );

		if ( ! select ) {
			select = control.querySelector( 'select' );
		}

		if ( ! select ) {
			return;
		}

		var isProduction = select.value === 'production';
		var i;

		for ( i = 0; i < MODE_CLASSES.length; i += 1 ) {
			control.classList.remove( MODE_CLASSES[ i ] );
		}

		control.classList.add(
			isProduction ? 'elementsell-thepay-env-production' : 'elementsell-thepay-env-demo'
		);
	}

	function bindEnvironmentSelectListener() {
		if ( document.body && document.body.dataset.elementsellThepayEnvBound === '1' ) {
			return;
		}

		if ( document.body ) {
			document.body.dataset.elementsellThepayEnvBound = '1';
		}

		document.addEventListener( 'change', function ( event ) {
			var target = event.target;

			if ( ! target || target.tagName !== 'SELECT' ) {
				return;
			}

			if (
				target.getAttribute( 'data-setting' ) === 'environment' ||
				target.closest( '.elementor-control-environment' )
			) {
				styleEnvironmentControl();
			}
		} );
	}

	function bindElementorPanelHooks() {
		if (
			typeof elementor === 'undefined' ||
			! elementor.hooks ||
			typeof elementor.hooks.addAction !== 'function'
		) {
			return;
		}

		elementor.hooks.addAction(
			'panel/open_editor/widget/elementsell_payment_form',
			function () {
				window.setTimeout( styleEnvironmentControl, 0 );
				window.setTimeout( styleEnvironmentControl, 150 );
			}
		);

		elementor.hooks.addAction( 'panel/open_editor/widget', function () {
			window.setTimeout( styleEnvironmentControl, 0 );
		} );

		elementor.hooks.addAction( 'panel/open_editor/section', function () {
			window.setTimeout( styleEnvironmentControl, 0 );
		} );
	}

	function bindElementorDataChanges() {
		if (
			typeof elementor === 'undefined' ||
			! elementor.channels ||
			! elementor.channels.data
		) {
			return;
		}

		elementor.channels.data.on( 'element:change', function () {
			window.requestAnimationFrame( styleEnvironmentControl );
		} );
	}

	function bindPanelMutationObserver() {
		var panel = document.getElementById( 'elementor-panel' );

		if ( ! panel || typeof MutationObserver === 'undefined' ) {
			return;
		}

		var scheduled = false;

		var observer = new MutationObserver( function () {
			if ( scheduled ) {
				return;
			}

			scheduled = true;
			window.requestAnimationFrame( function () {
				scheduled = false;
				styleEnvironmentControl();
			} );
		} );

		observer.observe( panel, {
			childList: true,
			subtree: true,
		} );
	}

	function initThepayEnvironmentColors() {
		bindEnvironmentSelectListener();
		bindElementorPanelHooks();
		bindElementorDataChanges();
		bindPanelMutationObserver();
		styleEnvironmentControl();
	}

	function bindEditorReady() {
		if ( typeof jQuery !== 'undefined' ) {
			jQuery( initThepayEnvironmentColors );
		} else if ( document.readyState === 'loading' ) {
			document.addEventListener( 'DOMContentLoaded', initThepayEnvironmentColors );
		} else {
			initThepayEnvironmentColors();
		}

		if (
			typeof elementor !== 'undefined' &&
			elementor.channels &&
			elementor.channels.editor
		) {
			elementor.channels.editor.on( 'editor:init', initThepayEnvironmentColors );
		}

		window.setTimeout( styleEnvironmentControl, 300 );
		window.setTimeout( styleEnvironmentControl, 1000 );
	}

	bindEditorReady();
} )();
