(function () {
	'use strict';

	function setActiveLink(links, activeLink) {
		links.forEach(function (link) {
			link.classList.toggle('is-active', link === activeLink);
		});
	}

	function collapseConfigSectionNav(link) {
		var board = link.closest('[data-config-section-board]');
		var shell;

		if (!board || board.classList.contains('is-nav-collapsed')) {
			return;
		}

		if (window.LogisticaPanelNav && typeof window.LogisticaPanelNav.setSectionNavCollapsed === 'function') {
			window.LogisticaPanelNav.setSectionNavCollapsed(board, true);
			return;
		}

		shell = board.closest('[data-logistica-inventory-shell]');
		if (shell && window.LogisticaPanelNav && typeof window.LogisticaPanelNav.toggleSectionNav === 'function') {
			window.LogisticaPanelNav.setSectionNavCollapsed(board, true);
		}
	}

	function initConfigSectionNav(board) {
		var nav = board.querySelector('[data-logistica-config-nav]');
		var links;
		var sections;

		if (!nav || nav.dataset.configNavBound === '1') {
			return;
		}

		nav.dataset.configNavBound = '1';
		links = nav.querySelectorAll('[data-config-section-nav]');
		sections = [];

		links.forEach(function (link) {
			var hash = (link.getAttribute('href') || '').replace(/^#/, '');
			var section = hash ? document.getElementById(hash) : null;

			if (!section) {
				return;
			}

			sections.push({ link: link, section: section });

			link.addEventListener('click', function (event) {
				event.preventDefault();
				section.scrollIntoView({ behavior: 'smooth', block: 'start' });
				if (window.history && window.history.replaceState) {
					window.history.replaceState(null, '', '#' + hash);
				}
				setActiveLink(links, link);
				collapseConfigSectionNav(link);
			});
		});

		if ('IntersectionObserver' in window && sections.length) {
			var observer = new IntersectionObserver(
				function (entries) {
					entries.forEach(function (entry) {
						if (!entry.isIntersecting) {
							return;
						}
						sections.forEach(function (item) {
							if (item.section === entry.target) {
								setActiveLink(links, item.link);
							}
						});
					});
				},
				{
					root: null,
					rootMargin: '-12% 0px -55% 0px',
					threshold: 0,
				}
			);

			sections.forEach(function (item) {
				observer.observe(item.section);
			});
		}
	}

	function scrollToConfigHash(board) {
		var hash = window.location.hash ? window.location.hash.slice(1) : '';
		var target;
		var nav;
		var links;
		var activeLink;

		if (!hash) {
			return false;
		}

		target = document.getElementById(hash);
		if (!target) {
			return false;
		}

		target.scrollIntoView({ behavior: 'smooth', block: 'start' });

		if (board) {
			nav = board.querySelector('[data-logistica-config-nav]');
			if (nav) {
				links = nav.querySelectorAll('[data-config-section-nav]');
				activeLink = nav.querySelector('[data-config-section-nav="' + hash + '"]');
				if (activeLink) {
					setActiveLink(links, activeLink);
				}
			}
		}

		return true;
	}

	function initAll(root) {
		var scope = root && root.querySelectorAll ? root : document;
		var boards = scope.querySelectorAll
			? scope.querySelectorAll('[data-config-section-board]')
			: document.querySelectorAll('[data-config-section-board]');

		if (root && root.matches && root.matches('[data-config-section-board]')) {
			boards = [root];
		}

		Array.prototype.forEach.call(boards, initConfigSectionNav);

		if (!window.location.hash) {
			return;
		}

		var board = boards[0] || document.querySelector('[data-config-section-board]');
		var delays = [120, 320, 700];

		delays.forEach(function (delay) {
			window.setTimeout(function () {
				scrollToConfigHash(board);
			}, delay);
		});
	}

	window.LogisticaConfigNav = {
		init: initAll
	};

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', function () {
			initAll();
		});
	} else {
		initAll();
	}
})();
