import { Host, h } from "@stencil/core";
export class SpwTocContainer {
    componentDidLoad() {
        this.scanHeadings();
        this.setupMutationObserver();
    }
    disconnectedCallback() {
        if (this.observer) {
            this.observer.disconnect();
        }
    }
    async getItems() {
        return this.scanHeadings();
    }
    setupMutationObserver() {
        this.observer = new MutationObserver(() => {
            this.scanHeadings();
        });
        this.observer.observe(this.el, {
            childList: true,
            subtree: true,
            characterData: true,
        });
    }
    slugify(text) {
        return text
            .toLowerCase()
            .trim()
            .normalize('NFD')
            .replace(/[\u0300-\u036f]/g, '')
            .replace(/[^\w\s-]/g, '')
            .replace(/[\s_-]+/g, '-')
            .replace(/^-+|-+$/g, '');
    }
    scanHeadings() {
        const tocItems = [];
        const headings = this.el.querySelectorAll('h2, h3, h4, h5, h6');
        headings.forEach((heading) => {
            var _a;
            const text = ((_a = heading.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
            let id = heading.id;
            if (!id && text) {
                id = this.slugify(text);
                heading.id = id;
            }
            if (id && text) {
                const level = parseInt(heading.tagName.substring(1));
                tocItems.push({ id, text, level });
            }
        });
        this.tocItemsChanged.emit(tocItems);
        return tocItems;
    }
    render() {
        return (h(Host, { key: 'b65fc485e901264fc4df9443d7ad55b4c17cb52c' }, h("slot", { key: '186f67706aa14e2fd3ce5799e61de589bd1a5a59' })));
    }
    static get is() { return "spw-toc-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["spw-toc-container.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["spw-toc-container.css"]
        };
    }
    static get events() {
        return [{
                "method": "tocItemsChanged",
                "name": "tocItemsChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "TocItem[]",
                    "resolved": "TocItem[]",
                    "references": {
                        "TocItem": {
                            "location": "local",
                            "path": "/Users/pectonite/Desktop/SPW/spw-design-system/packages/spw-stencil-library/src/components/spw-toc/spw-toc-container/spw-toc-container.tsx",
                            "id": "src/components/spw-toc/spw-toc-container/spw-toc-container.tsx::TocItem"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "getItems": {
                "complexType": {
                    "signature": "() => Promise<TocItem[]>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "TocItem": {
                            "location": "local",
                            "path": "/Users/pectonite/Desktop/SPW/spw-design-system/packages/spw-stencil-library/src/components/spw-toc/spw-toc-container/spw-toc-container.tsx",
                            "id": "src/components/spw-toc/spw-toc-container/spw-toc-container.tsx::TocItem"
                        }
                    },
                    "return": "Promise<TocItem[]>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
}
