import { closest } from "../../../utils/dom-utils.js";
import { buildCustomEvent } from "../../../utils/event-utils.js";
const dispatchSearchBoxSuggestionsEvent = (event, element, allowedSearchBoxElements = searchBoxElements)=>{
    const interfaceElement = closest(element, searchBoxElements.join(', '));
    if (!interfaceElement) throw new Error(`The "${element.nodeName.toLowerCase()}" component was not handled, as it is not a child of the following elements: ${allowedSearchBoxElements.join(', ')}`);
    dispatchSearchBoxSuggestionsEventEventually(interfaceElement, element, event);
};
const searchBoxElements = [
    'atomic-search-box',
    'atomic-insight-search-box',
    'atomic-commerce-search-box'
];
const isLitElementLoosely = (candidate)=>'updateComplete' in candidate && candidate.updateComplete instanceof Promise;
const dispatchSearchBoxSuggestionsEventEventually = async (interfaceElement, element, event)=>{
    await customElements.whenDefined(interfaceElement.nodeName.toLowerCase());
    if (isLitElementLoosely(interfaceElement)) await interfaceElement.updateComplete;
    else if ('componentOnReady' in interfaceElement) await interfaceElement.componentOnReady();
    element.dispatchEvent(buildCustomEvent('atomic/searchBoxSuggestion/register', event));
};
export { dispatchSearchBoxSuggestionsEvent };
