const collectDatasourceItemDescriptors = (component, allDatasourceItemDescriptors) => {
    if (component.datasourceItem) {
        allDatasourceItemDescriptors.push(component.datasourceItem);
    }
    component.placeholders.forEach((placeholder) => {
        if (placeholder.components) {
            placeholder.components.forEach((c) => {
                collectDatasourceItemDescriptors(c, allDatasourceItemDescriptors);
            });
        }
    });
};
const mapComponent = (allDatasourceItems, component, filteredSchema, allowedComponentTypes) => {
    const item = allDatasourceItems.find((item) => item.id === component.id);
    console.log("Map component: ", component, allDatasourceItems);
    const fields = item?.fields
        .filter((x) => component.datasourceItem?.renderedFieldIds.find((y) => y === x.id))
        .map((field) => ({
        name: field.name || "unknown",
        value: field.rawValue,
        type: field.type,
    })) ?? [];
    return {
        id: component.datasourceItem?.id || component.id,
        name: component.name || "unknown",
        type: component.type,
        fields: fields,
        children: mapPlaceholders(component, allDatasourceItems, filteredSchema, allowedComponentTypes),
    };
};
const mapPlaceholders = (component, allDatasourceItems, filteredSchema, allowedComponentTypes) => {
    let components = [];
    component.placeholders.forEach((p) => {
        const placeholderComponents = mapPlaceholder(p, allDatasourceItems, filteredSchema, allowedComponentTypes);
        components = [...components, ...placeholderComponents];
    });
    return components;
};
// Recursively walks the component tree to find all components that should be included
// A component is included if it's allowed OR if it has allowed descendants
const findAllowedComponents = (component, allowedComponentTypes) => {
    const result = [];
    // Check if this component is allowed
    const isThisComponentAllowed = allowedComponentTypes.has(component.type);
    // If this component is allowed, include it and all its allowed descendants
    if (isThisComponentAllowed) {
        console.log(`Including component ${component.type} as it's in allowed types`);
        result.push(component);
    }
    else {
        // If this component is not allowed, check its children for allowed components
        // and include those directly (skip the non-allowed parent)
        component.placeholders.forEach((placeholder) => {
            placeholder.components.forEach((child) => {
                const allowedChildren = findAllowedComponents(child, allowedComponentTypes);
                result.push(...allowedChildren);
            });
        });
    }
    return result;
};
const mapPlaceholder = (placeholder, allDatasourceItems, filteredSchema, allowedComponentTypes) => {
    // Find all allowed components in this placeholder (walking the tree)
    const allowedComponents = [];
    placeholder.components.forEach((component) => {
        const componentsToInclude = findAllowedComponents(component, allowedComponentTypes);
        allowedComponents.push(...componentsToInclude);
    });
    console.log(`Placeholder has ${allowedComponents.length} allowed components out of ${placeholder.components.length} total`);
    return allowedComponents.map((c) => mapComponent(allDatasourceItems, c, filteredSchema, allowedComponentTypes));
};
export const convertToAiPageModel = async (page, editContext, filteredSchema) => {
    const allDatasourceItemDescriptors = [];
    collectDatasourceItemDescriptors(page.rootComponent, allDatasourceItemDescriptors);
    const allDatasourceItems = await editContext.itemsRepository.getItems(allDatasourceItemDescriptors);
    // If no filtered schema is provided, include all components (backward compatibility)
    if (!filteredSchema) {
        return {
            components: mapPlaceholders(page.rootComponent, allDatasourceItems, [], new Set()),
        };
    }
    // Build set of all allowed component types
    // The rule is: keep components that are either:
    // 1. Allowed on root, OR
    // 2. Present in the filtered schema (meaning they can be children of allowed components)
    const allowedComponentTypes = new Set();
    // Add all component types from the filtered schema
    // If they're in the filtered schema, they're allowed somewhere in the hierarchy
    filteredSchema.forEach((c) => allowedComponentTypes.add(c.type));
    console.log("All allowed component types from filtered schema:", Array.from(allowedComponentTypes));
    console.log("Root-only component types:", filteredSchema.filter((c) => c.allowedOnRoot).map((c) => c.type));
    console.log("Filtered schema:", filteredSchema);
    return {
        components: mapPlaceholders(page.rootComponent, allDatasourceItems, filteredSchema, allowedComponentTypes),
    };
};
//# sourceMappingURL=aiPageModel.js.map