import { r as registerInstance, c as getElement } from './core-3f33e631.js';
import { e as debounce } from './utils-3aef1637.js';

const NavigationPane = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
    }
    connectedCallback() {
        this.setActive = debounce(this.setActive.bind(this), 50);
    }
    async componentWillRender() {
        return this.setActive();
    }
    onItemSelected({ target }, bypassScroll) {
        this.getItems().forEach(i => i.active = i === target);
        const section = document.getElementById(target.section);
        if (bypassScroll)
            return;
        if (section) {
            window.scrollTo({
                top: section.offsetTop,
                left: 0,
                behavior: 'smooth'
            });
        }
    }
    async setActive() {
        const sections = this.getItems().map(i => document.getElementById(i.section));
        let closest = sections[0];
        sections.slice(1).forEach(el => {
            if (el && Math.abs(window.scrollY - el.offsetTop) < Math.abs(window.scrollY - closest.offsetTop)) {
                closest = el;
            }
        });
        if (!closest)
            return;
        const target = this.element.querySelector(`anj-navigation-item[section="${closest.getAttribute('id')}"]`);
        this.onItemSelected({ target }, true);
    }
    getItems() {
        return Array.from(this.element.querySelectorAll('anj-navigation-item'));
    }
    get element() { return getElement(this); }
    static get style() { return ".sc-anj-navigation-pane-h {\n  display: block;\n  font-size: 0.875rem;\n  font-family: var(--anj-font-family);\n  font-weight: normal;\n  line-height: 1.5;\n  outline: none;\n  background-color: inherit;\n  position: -webkit-sticky;\n  position: sticky;\n  top: 0;\n}\n.sc-anj-navigation-pane-h, .sc-anj-navigation-pane-h::before, .sc-anj-navigation-pane-h::after {\n  -webkit-box-sizing: border-box;\n  box-sizing: border-box;\n  outline: none;\n}\n.sc-anj-navigation-pane-h:focus, .sc-anj-navigation-pane-h:active, .sc-anj-navigation-pane-h *.sc-anj-navigation-pane:focus, .sc-anj-navigation-pane-h *.sc-anj-navigation-pane:active {\n  outline: none;\n}"; }
};

export { NavigationPane as anj_navigation_pane };
