import { getItemDescriptor } from "../../utils";
export function buildWorkboxDescriptorRequest(page, currentItemDescriptor) {
    const items = [];
    const renderedItems = [];
    if (currentItemDescriptor) {
        items.push(currentItemDescriptor);
        renderedItems.push(currentItemDescriptor);
    }
    if (page?.item)
        renderedItems.push(page.item);
    if (page?.rootComponent)
        collectAllItems(page.rootComponent, items, renderedItems);
    return {
        items: dedupeItemDescriptors(items.filter(Boolean)),
        renderedItems: dedupeItemDescriptors(renderedItems.filter(Boolean)),
    };
}
function collectAllItems(component, items, renderedItems) {
    component.items.forEach((item) => renderedItems.push(item));
    if (component.datasourceItem)
        renderedItems.push(component.datasourceItem);
    component.placeholders.forEach((x) => {
        x.components.forEach((y) => {
            if (y.isShared && y.datasourceItem) {
                items.push(y.datasourceItem);
            }
            collectAllItems(y, items, renderedItems);
        });
    });
}
function dedupeItemDescriptors(items) {
    const seen = new Set();
    const result = [];
    items.forEach((item) => {
        const descriptor = getItemDescriptor(item);
        const key = [
            descriptor.id?.toLowerCase(),
            descriptor.language?.toLowerCase(),
            descriptor.version ?? 0,
            descriptor.database?.toLowerCase() ?? "",
        ].join("|");
        if (seen.has(key))
            return;
        seen.add(key);
        result.push(descriptor);
    });
    return result;
}
//# sourceMappingURL=workboxDescriptors.js.map