export const MANUAL_FIELD_ACTION_VISIBILITY_EVENT = "parhelia:manual-field-actions-visibility";
function collectFieldIds(root) {
    const ids = new Set();
    if (root instanceof HTMLElement) {
        const ownFieldId = root.getAttribute("data-field-id");
        if (ownFieldId)
            ids.add(ownFieldId);
        const closestFieldId = root.closest("[data-field-id]")?.getAttribute("data-field-id");
        if (closestFieldId)
            ids.add(closestFieldId);
    }
    root.querySelectorAll("[data-field-id]").forEach((element) => {
        const fieldId = element.getAttribute("data-field-id");
        if (fieldId)
            ids.add(fieldId);
    });
    return Array.from(ids);
}
export function getFieldIdsForManualTarget(elements) {
    const ids = new Set();
    elements.forEach((element) => {
        collectFieldIds(element).forEach((fieldId) => {
            ids.add(fieldId);
        });
    });
    if (ids.size > 0) {
        return Array.from(ids);
    }
    const panels = Array.from(document.querySelectorAll('[data-testid="editor-sidepanel"]'));
    const globalFieldIds = Array.from(document.querySelectorAll("[data-field-id]"))
        .map((element) => element.getAttribute("data-field-id"))
        .filter((fieldId) => Boolean(fieldId));
    panels.forEach((panel) => {
        collectFieldIds(panel).forEach((fieldId) => {
            ids.add(fieldId);
        });
    });
    if (ids.size === 0) {
        globalFieldIds.forEach((fieldId) => {
            ids.add(fieldId);
        });
    }
    return Array.from(ids);
}
export function dispatchManualFieldActionVisibility(detail) {
    window.dispatchEvent(new CustomEvent(MANUAL_FIELD_ACTION_VISIBILITY_EVENT, { detail }));
}
//# sourceMappingURL=manualActionEvents.js.map