(function (window, document) {
    'use strict';

    function getTrigger(event) {
        if (!event || !event.target || !event.target.closest) {
            return null;
        }
        return event.target.closest('[data-noesia-cs-settings-anchor]');
    }

    function getTargetId(trigger) {
        if (!trigger) {
            return '';
        }
        var targetId = trigger.getAttribute('data-noesia-cs-settings-jump') || '';
        if (!targetId) {
            var href = trigger.getAttribute('href') || '';
            if (href.charAt(0) === '#') {
                targetId = href.slice(1);
            }
        }
        try {
            return decodeURIComponent(targetId);
        } catch (error) {
            return targetId;
        }
    }

    function getScrollContainer(target) {
        var node = target ? target.parentElement : null;
        while (node && node !== document.body && node !== document.documentElement) {
            var style = window.getComputedStyle ? window.getComputedStyle(node) : null;
            var overflowY = style ? style.overflowY : '';
            if ((overflowY === 'auto' || overflowY === 'scroll') && node.scrollHeight > node.clientHeight) {
                return node;
            }
            node = node.parentElement;
        }
        return window;
    }

    function scrollToTarget(targetId) {
        var target = targetId ? document.getElementById(targetId) : null;
        if (!target) {
            return false;
        }

        var topbar = document.querySelector('.noesia-cs-topbar');
        var offset = 112;
        if (topbar) {
            offset = Math.max(offset, topbar.getBoundingClientRect().height + 52);
        }

        var container = getScrollContainer(target);
        if (container === window) {
            var y = target.getBoundingClientRect().top + window.pageYOffset - offset;
            try {
                window.scrollTo({ top: Math.max(0, y), behavior: 'smooth' });
            } catch (error) {
                window.scrollTo(0, Math.max(0, y));
            }
        } else {
            var containerTop = container.getBoundingClientRect().top;
            var targetTop = target.getBoundingClientRect().top;
            var scrollTop = container.scrollTop + targetTop - containerTop - 18;
            try {
                container.scrollTo({ top: Math.max(0, scrollTop), behavior: 'smooth' });
            } catch (error) {
                container.scrollTop = Math.max(0, scrollTop);
            }
        }

        target.classList.add('is-anchor-highlight');
        window.setTimeout(function () {
            target.classList.remove('is-anchor-highlight');
        }, 1400);
        return true;
    }

    document.addEventListener('click', function (event) {
        var trigger = getTrigger(event);
        if (!trigger) {
            return;
        }

        event.preventDefault();
        event.stopPropagation();
        if (event.stopImmediatePropagation) {
            event.stopImmediatePropagation();
        }

        var targetId = getTargetId(trigger);
        if (scrollToTarget(targetId) && window.history && window.history.replaceState) {
            window.history.replaceState(window.history.state || {}, '', window.location.pathname + window.location.search + '#' + targetId);
        }
    }, true);

    function scrollFromHash() {
        if (!window.location.hash || window.location.hash.indexOf('#noesia-cs-settings-') !== 0) {
            return;
        }
        var targetId = window.location.hash.slice(1);
        window.setTimeout(function () {
            scrollToTarget(targetId);
        }, 180);
    }

    document.addEventListener('DOMContentLoaded', scrollFromHash);
    document.addEventListener('noesiaCommerceSuiteTabLoaded', scrollFromHash);

    window.noesiaCommerceSuiteSettingsScrollTo = scrollToTarget;
}(window, document));
