import '../init';
import windowIsAvailable from './windowIsAvailable';
if (windowIsAvailable) {
    window.cosmos.popoverMap ??= new WeakMap();
}
export const popoverMap = windowIsAvailable
    ? window.cosmos.popoverMap
    : new WeakMap();
const getAllPopovers = (parent) => {
    const rootNode = parent.getRootNode();
    if (!(rootNode instanceof Document) && !(rootNode instanceof DocumentFragment))
        return [];
    const popoverTargets = [...parent.querySelectorAll('[data-popover-target]')];
    return popoverTargets.flatMap(el => {
        if (!popoverMap.has(el))
            return [];
        const pointerIds = popoverMap.get(el);
        const popovers = [...pointerIds].map(id => rootNode.querySelector(`[data-popover-id="${id.replace(/"/g, '\\"')}"]`));
        return popovers.flatMap(popover => (popover ? [popover, ...getAllPopovers(popover)] : []));
    });
};
export default getAllPopovers;
//# sourceMappingURL=getAllPopovers.js.map