import lazy_index from "../components/lazy-index.js";
function registerAutoloader(roots) {
    if ("u" < typeof window) return;
    roots ??= [
        document.documentElement
    ];
    roots = Array.isArray(roots) ? roots : [
        roots
    ];
    const visitedNodes = new WeakSet();
    const observeStencilElementHydration = (atomicElement)=>{
        const attributeObserver = new MutationObserver(async ()=>{
            if (atomicElement.classList.contains('hydrated') && 'shadowRoot' in atomicElement && atomicElement.shadowRoot && !visitedNodes.has(atomicElement.shadowRoot)) {
                attributeObserver.disconnect();
                await discover(atomicElement.shadowRoot);
                observer.observe(atomicElement.shadowRoot, {
                    subtree: true,
                    childList: true
                });
            }
        });
        attributeObserver.observe(atomicElement, {
            attributes: true,
            attributeFilter: [
                'class'
            ]
        });
    };
    const discover = async (root)=>{
        visitedNodes.add(root);
        const rootTagName = root instanceof Element ? root.tagName.toLowerCase() : '';
        const rootIsCustomElement = rootTagName?.includes('-');
        const allCustomElements = [
            ...root.querySelectorAll('*')
        ].filter((el)=>el.tagName.toLowerCase().includes('-'));
        if (rootIsCustomElement && root instanceof Element && !customElements.get(rootTagName) && !allCustomElements.includes(root)) allCustomElements.push(root);
        if (rootIsCustomElement) {
            if ('shadowRoot' in root && root.shadowRoot && !visitedNodes.has(root.shadowRoot)) {
                await discover(root.shadowRoot);
                observer.observe(root.shadowRoot, {
                    subtree: true,
                    childList: true
                });
            }
        }
        const childTemplates = root.querySelectorAll('template');
        for (const template of childTemplates)if (!visitedNodes.has(template.content)) {
            await discover(template.content);
            observer.observe(template.content, {
                subtree: true,
                childList: true
            });
        }
        const litRegistrationPromises = [];
        for (const atomicElement of allCustomElements){
            const tagName = atomicElement.tagName.toLowerCase();
            if (tagName in lazy_index && !customElements.get(tagName)) {
                litRegistrationPromises.push(register(tagName));
                continue;
            }
            if ('shadowRoot' in atomicElement && atomicElement.shadowRoot && !visitedNodes.has(atomicElement.shadowRoot)) {
                await discover(atomicElement);
                continue;
            }
            if (!atomicElement.classList.contains('hydrated')) observeStencilElementHydration(atomicElement);
        }
        await Promise.allSettled(litRegistrationPromises);
        customElements.upgrade(root);
    };
    const register = (tagName)=>{
        if (customElements.get(tagName)) return Promise.resolve();
        return lazy_index[tagName]?.();
    };
    const observer = new MutationObserver(async (mutations)=>{
        for (const { addedNodes } of mutations)for (const node of addedNodes)if (node.nodeType === Node.ELEMENT_NODE) await discover(node);
    });
    const initializeDiscovery = ()=>{
        for (const root of roots){
            discover(root);
            observer.observe(root, {
                subtree: true,
                childList: true
            });
        }
    };
    if ('loading' === document.readyState) document.addEventListener('DOMContentLoaded', initializeDiscovery);
    else initializeDiscovery();
}
export { registerAutoloader };
