(function () {
    'use strict';

    var root = document.documentElement;
    var body = document.body;
    var page = String(body && body.getAttribute('data-native-sidebar-page') || '');
    if (page.indexOf('diar-') !== 0) return;

    var sidebar = document.querySelector('[data-native-sidebar]');
    if (!sidebar) return;

    var LEGACY_COLLAPSE_CLASSES = [
        'sidebar-collapsed', 'sidebar-mini', 'sidebar-compact', 'sidebar-hidden',
        'nav-collapsed', 'is-collapsed', 'collapsed', 'elite-sidebar-collapsed'
    ];
    var LEGACY_SHELL_CLASSES = ['diar-layout-parent', 'diar-layout-grid', 'diar-layout-flex'];
    var LEGACY_SIDEBAR_CLASSES = ['diar-layout-sidebar', 'diar-layout-sidebar-fixed'];
    var LEGACY_MAIN_CLASSES = ['diar-layout-main', 'diar-layout-main-fixed-offset'];
    var resizeTimer = 0;

    function removeClasses(node, names) {
        if (!node || !node.classList) return;
        names.forEach(function (name) { node.classList.remove(name); });
    }

    function clearProperties(node, names) {
        if (!node || !node.style) return;
        names.forEach(function (name) { node.style.removeProperty(name); });
    }

    function setOpen(group, open) {
        var toggle = group.querySelector('[data-nav-group-toggle]');
        group.classList.toggle('is-open', open);
        if (toggle) toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
    }

    function closeOtherGroups(groups, current) {
        groups.forEach(function (group) {
            if (group !== current) setOpen(group, false);
        });
    }

    function enforceShell() {
        var shell = sidebar.closest('[data-native-shell]') || sidebar.parentElement;
        var main = shell ? shell.querySelector('[data-native-main]') : null;
        var rtl = root.dir === 'rtl' || String(root.lang || '').toLowerCase().indexOf('ar') === 0;

        removeClasses(body, LEGACY_COLLAPSE_CLASSES);
        removeClasses(shell, LEGACY_COLLAPSE_CLASSES.concat(LEGACY_SHELL_CLASSES));
        removeClasses(sidebar, LEGACY_COLLAPSE_CLASSES.concat(LEGACY_SIDEBAR_CLASSES));
        removeClasses(main, LEGACY_COLLAPSE_CLASSES.concat(LEGACY_MAIN_CLASSES));
        removeClasses(sidebar, ['native-sidebar-height-compact', 'native-sidebar-height-ultra']);

        clearProperties(shell, [
            'display', 'grid-template-columns', 'grid-template-areas', 'width', 'max-width',
            'min-width', 'margin', 'margin-left', 'margin-right', 'padding', 'transform',
            'translate', 'direction'
        ]);
        clearProperties(sidebar, [
            'display', 'position', 'width', 'min-width', 'max-width', 'flex', 'flex-basis',
            'margin', 'margin-left', 'margin-right', 'left', 'right', 'inset',
            'inset-inline-start', 'inset-inline-end', 'transform', 'translate', 'visibility',
            'opacity', 'clip-path', 'direction'
        ]);
        clearProperties(main, [
            'position', 'width', 'min-width', 'max-width', 'margin', 'margin-left',
            'margin-right', 'left', 'right', 'inset', 'transform', 'translate', 'flex',
            'flex-basis', 'direction'
        ]);

        if (shell) {
            shell.setAttribute('data-native-shell-contract', 'R4');
            shell.setAttribute('data-native-direction', rtl ? 'rtl' : 'ltr');
        }
        sidebar.setAttribute('data-native-sidebar-contract', 'R4');
        sidebar.setAttribute('data-native-sidebar-density', 'FIXED');
        sidebar.setAttribute('data-native-sidebar-no-scroll', 'SCROLL-CONTAINED');
        sidebar.setAttribute('dir', rtl ? 'rtl' : 'ltr');
        sidebar.removeAttribute('data-diar-sidebar-target');
        if (main) main.setAttribute('data-native-main-contract', 'R4');

        root.classList.add('native-sidebar-enhanced', 'diar-sidebar-r5-stable');
        root.setAttribute('data-native-sidebar-direction', rtl ? 'RTL' : 'LTR');
        root.setAttribute('data-native-sidebar-release', 'R5S');
        root.setAttribute('data-native-sidebar-no-scroll', 'SCROLL-CONTAINED');
    }

    function verifyGeometry() {
        var shell = sidebar.closest('[data-native-shell]') || sidebar.parentElement;
        var main = shell ? shell.querySelector('[data-native-main]') : null;
        var mobile = window.matchMedia && window.matchMedia('(max-width: 760px)').matches;
        var rtl = root.dir === 'rtl' || String(root.lang || '').toLowerCase().indexOf('ar') === 0;
        var pass = true;

        if (!mobile && shell && main) {
            var s = sidebar.getBoundingClientRect();
            var m = main.getBoundingClientRect();
            var h = shell.getBoundingClientRect();
            var tolerance = 5;
            var widthOk = s.width >= 286 && s.width <= 306;
            var sideOk = rtl ? Math.abs(h.right - s.right) <= tolerance : Math.abs(s.left - h.left) <= tolerance;
            var overlapOk = rtl ? m.right <= s.left + 2 : m.left >= s.right - 2;
            pass = widthOk && sideOk && overlapOk;
            root.setAttribute('data-native-sidebar-width', widthOk ? 'PASS' : 'FAIL');
            root.setAttribute('data-native-sidebar-side', sideOk ? (rtl ? 'RIGHT' : 'LEFT') : 'FAIL');
            root.setAttribute('data-native-sidebar-overlap', overlapOk ? 'PASS' : 'FAIL');
        }

        root.setAttribute('data-native-sidebar-shell-contract', pass ? 'PASS' : 'FAIL');
        root.setAttribute('data-native-sidebar-contract-ready', pass ? 'PASS' : 'FAIL');
        return pass;
    }

    function scheduleResizeVerification() {
        window.clearTimeout(resizeTimer);
        resizeTimer = window.setTimeout(function () {
            enforceShell();
            window.requestAnimationFrame(verifyGeometry);
        }, 120);
    }

    function initGroups() {
        var groups = Array.prototype.slice.call(sidebar.querySelectorAll('[data-nav-group]'));
        if (!groups.length) return;

        var activeGroup = null;
        groups.forEach(function (group) {
            if (group.querySelector('[aria-current="page"]')) activeGroup = group;
        });
        if (!activeGroup) {
            activeGroup = groups.filter(function (group) { return group.classList.contains('is-open'); })[0] || groups[0];
        }
        closeOtherGroups(groups, activeGroup);
        setOpen(activeGroup, true);

        groups.forEach(function (group) {
            var toggle = group.querySelector('[data-nav-group-toggle]');
            if (!toggle) return;
            toggle.addEventListener('click', function () {
                var shouldOpen = !group.classList.contains('is-open');
                closeOtherGroups(groups, group);
                setOpen(group, shouldOpen);
                window.requestAnimationFrame(verifyGeometry);
            });
        });

        sidebar.querySelectorAll('.native-nav-item[href]').forEach(function (link) {
            link.addEventListener('click', function () {
                if (window.matchMedia && window.matchMedia('(max-width: 760px)').matches) {
                    body.classList.remove('sidebar-open');
                }
            });
        });
    }

    function boot() {
        enforceShell();
        initGroups();
        window.requestAnimationFrame(function () {
            window.requestAnimationFrame(verifyGeometry);
        });
        window.addEventListener('resize', scheduleResizeVerification, { passive: true });
        window.addEventListener('orientationchange', scheduleResizeVerification, { passive: true });
        if (document.fonts && document.fonts.ready) {
            document.fonts.ready.then(function () {
                window.requestAnimationFrame(verifyGeometry);
            });
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', boot, { once: true });
    } else {
        boot();
    }
}());
