import { r as registerInstance, c as createEvent, h, g as getElement } from './core-58de451e.js';
import { e as debounce } from './utils-3aef1637.js';

const NavigationItem = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.anjNavigationItemSelected = createEvent(this, "anjNavigationItemSelected", 7);
    }
    onClick(e) {
        e.preventDefault();
        this.anjNavigationItemSelected.emit();
    }
    render() {
        const path = window.location.pathname;
        return (h("anj-headline", null, h("h4", null, h("a", { href: `${path}#${this.section}`, onClick: (e) => this.onClick(e) }, h("slot", null)))));
    }
    static get style() { return ".sc-anj-navigation-item-h{display:block;font-size:.875rem;font-family:var(--anj-font-family);font-weight:400;line-height:1.5;outline:none}.sc-anj-navigation-item-h, .sc-anj-navigation-item-h:after, .sc-anj-navigation-item-h:before{-webkit-box-sizing:border-box;box-sizing:border-box;outline:none}.sc-anj-navigation-item-h:active, .sc-anj-navigation-item-h .sc-anj-navigation-item:active, .sc-anj-navigation-item-h:focus, .sc-anj-navigation-item-h .sc-anj-navigation-item:focus{outline:none}.sc-anj-navigation-item-h:not(:last-child){margin-bottom:.5rem}[active].sc-anj-navigation-item-h a.sc-anj-navigation-item{color:var(--anj-text)}[active].sc-anj-navigation-item-h a.sc-anj-navigation-item:hover{text-decoration:none}"; }
};

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{display:block;font-size:.875rem;font-family:var(--anj-font-family);font-weight:400;line-height:1.5;outline:none;background-color:inherit;position:-webkit-sticky;position:sticky;top:0}.sc-anj-navigation-pane-h, .sc-anj-navigation-pane-h:after, .sc-anj-navigation-pane-h:before{-webkit-box-sizing:border-box;box-sizing:border-box;outline:none}.sc-anj-navigation-pane-h:active, .sc-anj-navigation-pane-h .sc-anj-navigation-pane:active, .sc-anj-navigation-pane-h:focus, .sc-anj-navigation-pane-h .sc-anj-navigation-pane:focus{outline:none}"; }
};

export { NavigationItem as anj_navigation_item, NavigationPane as anj_navigation_pane };
