export function findComponent(id, placeholders) {
    if (!placeholders)
        return;
    for (const p of placeholders) {
        for (const c of p.components) {
            if (c.id === id || c.datasourceItem?.id === id) {
                return c;
            }
            if (c.placeholders) {
                const match = findComponent(id, c.placeholders);
                if (match) {
                    return match;
                }
            }
        }
    }
    return;
}
export function getComponentById(id, page) {
    if (!page)
        return;
    if (page.rootComponent.datasourceItem?.id === id)
        return page.rootComponent;
    return findComponent(id, page.rootComponent.placeholders);
}
export function getComponentByIdFromHeadlessLayout(id, page) {
    if (!page)
        return;
    return findComponent(id, page.placeholders);
}
export function getAllPlaceholders(page) {
    const placeholders = [];
    const addPlaceholders = (p) => {
        p.forEach((x) => {
            placeholders.push(x);
            if (x.components) {
                x.components.forEach((c) => {
                    addPlaceholders(c.placeholders);
                });
            }
        });
    };
    addPlaceholders(page.rootComponent.placeholders);
    return placeholders;
}
export const isValidPlaceholder = (placeholder, dragObject) => {
    return (placeholder.insertOptions?.find((y) => y.typeId === dragObject.typeId ||
        y.typeId === dragObject?.templateId ||
        y.compatibleTypeIds?.find((z) => z == dragObject.typeId) !==
            undefined ||
        y.typeId === dragObject.templateId ||
        y.compatibleTypeIds?.find((z) => z == dragObject.templateId) !==
            undefined, undefined) !== undefined);
};
export function findAllComponentInstances(id, placeholders) {
    const instances = [];
    if (!placeholders)
        return instances;
    // First find the target component to get its criteria
    const targetComponent = findComponent(id, placeholders);
    if (!targetComponent)
        return instances;
    // If the incoming id is an actual component id, only return exact id matches.
    // This avoids pulling in unrelated components that happen to share a datasource item.
    let hasExactIdMatch = false;
    const detectExactIdMatches = (nodes) => {
        for (const p of nodes) {
            for (const c of p.components) {
                if (c.id === id) {
                    hasExactIdMatch = true;
                    return;
                }
                if (c.placeholders?.length) {
                    detectExactIdMatches(c.placeholders);
                    if (hasExactIdMatch)
                        return;
                }
            }
        }
    };
    detectExactIdMatches(placeholders);
    // Helper function to recursively find all matching components
    const findMatching = (placeholders) => {
        for (const p of placeholders) {
            for (const c of p.components) {
                // Only match by exact ID or same datasource item ID
                // This ensures we only get the same component instance, not just same type
                const matches = hasExactIdMatch
                    ? c.id === id
                    : !!(targetComponent.datasourceItem?.id &&
                        c.datasourceItem?.id === targetComponent.datasourceItem?.id);
                if (matches) {
                    instances.push(c);
                }
                if (c.placeholders) {
                    findMatching(c.placeholders);
                }
            }
        }
    };
    findMatching(placeholders);
    return instances;
}
export function getAllComponentInstances(id, page) {
    if (!page)
        return [];
    return findAllComponentInstances(id, page.rootComponent.placeholders);
}
//# sourceMappingURL=componentTreeHelper.js.map