import { ATOMIC_CUSTOM_ELEMENT_TAGS } from "./custom-element-tags.js";
import { closest } from "./dom-utils.js";
import { buildCustomEvent } from "./event-utils.js";
import { enqueueOrDispatchInitializationEvent } from "./init-queue.js";
function fetchBindings(element) {
    return new Promise((resolve, reject)=>{
        const event = buildCustomEvent(initializeEventName, (bindings)=>resolve(bindings));
        const parent = closest(element, initializableElements.join(', '));
        if (!parent) return void reject(new MissingInterfaceParentError(element.nodeName.toLowerCase()));
        enqueueOrDispatchInitializationEvent(parent, event, element);
    });
}
class MissingInterfaceParentError extends Error {
    constructor(elementName){
        super(`The "${elementName}" element must be the child of the following elements: ${initializableElements.join(', ')}`);
    }
}
const initializableElements = [
    'atomic-recs-interface',
    'atomic-search-interface',
    'atomic-commerce-interface',
    'atomic-commerce-recommendation-interface',
    'atomic-relevance-inspector',
    'atomic-insight-interface',
    'atomic-external'
];
const initializeEventName = 'atomic/initializeComponent';
async function waitForAtomicChildrenToBeDefined(host) {
    await Promise.all(Array.from(host.querySelectorAll('*')).filter((el)=>ATOMIC_CUSTOM_ELEMENT_TAGS.has(el.tagName.toLowerCase())).map((el)=>customElements.whenDefined(el.tagName.toLowerCase())));
}
export { MissingInterfaceParentError, fetchBindings, initializeEventName, waitForAtomicChildrenToBeDefined };
