(function () {
	var body = document.body;
	var menu = document.querySelector('[data-mobile-menu]');
	var toggle = document.querySelector('[data-menu-toggle]');
	var closeNodes = document.querySelectorAll('[data-menu-close]');
	var mobileCatalogToggle = document.querySelector('[data-mobile-catalog-toggle]');
	var mobileCatalog = document.querySelector('[data-mobile-catalog]');
	var catalogItem = document.querySelector('.site-header__menu-item--catalog');
	var catalogButton = document.querySelector('.site-header__menu-button');
	var cookieNote = document.querySelector('[data-cookie-note]');
	var cookieButton = document.querySelector('[data-cookie-accept]');
	var leadModal = document.querySelector('[data-lead-modal]');
	var leadOpenNodes = document.querySelectorAll('[data-lead-open]');
	var leadCloseNodes = document.querySelectorAll('[data-lead-close]');
	var phoneInputs = document.querySelectorAll('[data-phone-mask]');
	var emailInputs = document.querySelectorAll('[data-email-mask]');

	function setMenuState(isOpen) {
		if (!menu || !toggle) {
			return;
		}

		menu.classList.toggle('is-open', isOpen);
		body.classList.toggle('menu-is-open', isOpen);
		menu.setAttribute('aria-hidden', isOpen ? 'false' : 'true');
		toggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');

		if (!isOpen && mobileCatalogToggle && mobileCatalog) {
			mobileCatalogToggle.setAttribute('aria-expanded', 'false');
			mobileCatalog.hidden = true;
		}
	}

	function setLeadModalState(isOpen, source) {
		if (!leadModal) {
			return;
		}

		leadModal.hidden = !isOpen;
		body.classList.toggle('lead-modal-is-open', isOpen);

		if (!isOpen) {
			return;
		}

		var pageInput = leadModal.querySelector('input[name="lead_page"]');
		var sourceInput = leadModal.querySelector('input[name="lead_source"]');
		var firstInput = leadModal.querySelector('input[name="lead_name"]');

		if (pageInput) {
			pageInput.value = document.title + ' | ' + window.location.pathname;
		}

		if (sourceInput) {
			sourceInput.value = source || 'Попап заявки';
		}

		if (firstInput) {
			window.setTimeout(function () {
				firstInput.focus();
			}, 80);
		}
	}

	function applyPhoneMask(input) {
		var digits = input.value.replace(/\D/g, '');

		if (digits.charAt(0) === '8') {
			digits = '7' + digits.slice(1);
		}

		if (digits.charAt(0) === '7') {
			digits = digits.slice(1);
		}

		digits = digits.slice(0, 10);

		var value = '+7';
		if (digits.length > 0) {
			value += ' (' + digits.slice(0, 3);
		}
		if (digits.length >= 3) {
			value += ') ' + digits.slice(3, 6);
		}
		if (digits.length >= 6) {
			value += '-' + digits.slice(6, 8);
		}
		if (digits.length >= 8) {
			value += '-' + digits.slice(8, 10);
		}

		input.value = value;
	}

	if (menu && toggle) {
		toggle.addEventListener('click', function () {
			setMenuState(!menu.classList.contains('is-open'));
		});

		closeNodes.forEach(function (node) {
			node.addEventListener('click', function () {
				setMenuState(false);
			});
		});
	}

	if (mobileCatalogToggle && mobileCatalog) {
		mobileCatalogToggle.addEventListener('click', function () {
			var isOpen = mobileCatalogToggle.getAttribute('aria-expanded') === 'true';
			mobileCatalogToggle.setAttribute('aria-expanded', isOpen ? 'false' : 'true');
			mobileCatalog.hidden = isOpen;
		});
	}

	if (catalogItem && catalogButton) {
		catalogButton.addEventListener('click', function (event) {
			event.stopPropagation();
			var isOpen = catalogItem.classList.toggle('is-open');
			catalogButton.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
		});

		document.addEventListener('click', function (event) {
			if (!catalogItem.contains(event.target)) {
				catalogItem.classList.remove('is-open');
				catalogButton.setAttribute('aria-expanded', 'false');
			}
		});
	}

	leadOpenNodes.forEach(function (node) {
		node.addEventListener('click', function (event) {
			event.preventDefault();
			setMenuState(false);
			setLeadModalState(true, node.getAttribute('data-lead-source') || node.textContent.trim());
		});
	});

	leadCloseNodes.forEach(function (node) {
		node.addEventListener('click', function () {
			setLeadModalState(false);
		});
	});

	document.addEventListener('keydown', function (event) {
		if (event.key === 'Escape') {
			setMenuState(false);
			setLeadModalState(false);
		}
	});

	phoneInputs.forEach(function (input) {
		input.addEventListener('focus', function () {
			if (!input.value) {
				input.value = '+7';
			}
		});

		input.addEventListener('input', function () {
			applyPhoneMask(input);
		});
	});

	emailInputs.forEach(function (input) {
		input.addEventListener('input', function () {
			input.value = input.value.replace(/\s/g, '').toLowerCase();
		});
	});

	if (cookieNote && cookieButton && window.localStorage) {
		if (localStorage.getItem('sevTortCookieAccepted') !== 'yes') {
			cookieNote.hidden = false;
		}

		cookieButton.addEventListener('click', function () {
			localStorage.setItem('sevTortCookieAccepted', 'yes');
			cookieNote.hidden = true;
		});
	}
})();
