import { debounce } from '../../utils/utils';
export class NavigationPane {
    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'));
    }
    static get is() { return "anj-navigation-pane"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() { return {
        "$": ["navigation-pane.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["navigation-pane.css"]
    }; }
    static get methods() { return {
        "setActive": {
            "complexType": {
                "signature": "() => Promise<void>",
                "parameters": [],
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "HTMLElement": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        }
    }; }
    static get elementRef() { return "element"; }
    static get listeners() { return [{
            "name": "anjNavigationItemSelected",
            "method": "onItemSelected",
            "target": undefined,
            "capture": false,
            "passive": false
        }, {
            "name": "scroll",
            "method": "setActive",
            "target": "window",
            "capture": false,
            "passive": true
        }]; }
}
