/**
 * Utility functions for HazoFieldSelectorDialog
 */
/**
 * Flatten form template into searchable items (sections, groups, AND fields)
 */
export function flatten_form_template(template) {
    const items = [];
    template.sections.forEach((section, section_index) => {
        const section_name = section.section_name || `Section ${section_index + 1}`;
        const group_count = section.groups?.filter(g => g.field_type === 'group').length ?? 0;
        // Add SECTION item
        items.push({
            search_id: `section:${section_index}`,
            item_type: 'section',
            label: section_name,
            section_name,
            section_index,
            field_config: { id: `section_${section_index}`, label: section_name, field_type: 'group', value: '' },
            child_count: group_count,
        });
        // Add GROUPS and FIELDS
        section.groups?.forEach((group, group_index) => {
            if (group.field_type === 'group') {
                const field_count = group.fields?.length ?? 0;
                // Add GROUP item
                items.push({
                    search_id: `group:${group.id}`,
                    item_type: 'group',
                    label: group.label,
                    section_name,
                    section_index,
                    group_index,
                    field_config: group,
                    child_count: field_count,
                });
                // Add FIELD items
                group.fields?.forEach((field, field_index) => {
                    items.push({
                        search_id: `field:${group.id}:${field.id}`,
                        item_type: 'field',
                        label: field.label,
                        section_name,
                        section_index,
                        group_index,
                        field_index,
                        parent_group_id: group.id,
                        parent_group_label: group.label,
                        field_config: field,
                    });
                });
            }
        });
    });
    return items;
}
/**
 * Create FieldSelectorItem from search selection
 */
export function create_selection_from_search_item(item, template, existing_items) {
    switch (item.item_type) {
        case 'section': {
            // Add all groups from section
            const section = template.sections[item.section_index];
            const new_items = [];
            section.groups?.forEach((group, group_index) => {
                if (group.field_type === 'group' && !existing_items.some(e => e.id === group.id)) {
                    new_items.push({
                        id: group.id,
                        label: group.label,
                        section_name: item.section_name,
                        section_index: item.section_index,
                        group_index,
                        field_config: group,
                        order: existing_items.length + new_items.length,
                    });
                }
            });
            return new_items;
        }
        case 'group': {
            // Add group with all fields
            if (existing_items.some(e => e.id === item.field_config.id)) {
                return [];
            }
            return [{
                    id: item.field_config.id,
                    label: item.label,
                    section_name: item.section_name,
                    section_index: item.section_index,
                    group_index: item.group_index,
                    field_config: item.field_config,
                    order: existing_items.length,
                }];
        }
        case 'field': {
            // Add parent group with ONLY this field selected
            const parent_group_id = item.parent_group_id;
            const existing_group = existing_items.find(e => e.id === parent_group_id);
            if (existing_group) {
                return [];
            }
            // Find the parent group config
            const section = template.sections[item.section_index];
            const group = section.groups?.[item.group_index];
            if (!group)
                return [];
            const total_fields = group.fields?.length ?? 0;
            return [{
                    id: group.id,
                    label: group.label,
                    section_name: item.section_name,
                    section_index: item.section_index,
                    group_index: item.group_index,
                    field_config: group,
                    order: existing_items.length,
                    field_selection: {
                        selected_field_ids: new Set([item.field_config.id]),
                        total_fields,
                    },
                    is_expanded: true,
                }];
        }
    }
}
/**
 * Group selected items by section
 */
export function group_items_by_section(items) {
    const sections_map = new Map();
    const sorted_items = [...items].sort((a, b) => a.order - b.order);
    sorted_items.forEach(item => {
        if (!sections_map.has(item.section_name)) {
            sections_map.set(item.section_name, {
                section_name: item.section_name,
                section_index: item.section_index,
                items: [],
            });
        }
        sections_map.get(item.section_name).items.push(item);
    });
    return Array.from(sections_map.values()).sort((a, b) => a.section_index - b.section_index);
}
/**
 * Build output JSON from selected items with field-level filtering
 */
export function build_output_template(original_template, selected_items, include_icons_behaviour) {
    const by_section = group_items_by_section(selected_items);
    const sections = by_section.map(({ section_name, section_index, items }) => {
        const original_section = original_template.sections[section_index];
        const groups = items.map(item => {
            const group_config = item.field_config;
            // If no field_selection or no fields, return as-is
            if (!item.field_selection || !group_config.fields) {
                return group_config;
            }
            // Filter fields based on selection
            const { selected_field_ids } = item.field_selection;
            const filtered_fields = group_config.fields.filter(f => selected_field_ids.has(f.id));
            return {
                ...group_config,
                fields: filtered_fields,
            };
        });
        return {
            section_name,
            ...(original_section.accept_public_files !== undefined && { accept_public_files: original_section.accept_public_files }),
            ...(original_section.accept_private_files !== undefined && { accept_private_files: original_section.accept_private_files }),
            groups,
        };
    });
    const output = { sections };
    if (include_icons_behaviour && original_template.icons_behaviour) {
        output.icons_behaviour = original_template.icons_behaviour;
    }
    return output;
}
/**
 * Update item orders after reordering
 */
export function update_item_orders(items) {
    return items.map((item, index) => ({
        ...item,
        order: index,
    }));
}
/**
 * Toggle a field's selection within a group
 */
export function toggle_field_in_group(items, group_id, field_id) {
    return items.map(item => {
        if (item.id !== group_id)
            return item;
        const group_fields = item.field_config.fields ?? [];
        const total_fields = group_fields.length;
        let selected_ids = item.field_selection?.selected_field_ids
            ?? new Set(group_fields.map(f => f.id));
        selected_ids = new Set(selected_ids);
        if (selected_ids.has(field_id)) {
            if (selected_ids.size > 1) {
                selected_ids.delete(field_id);
            }
        }
        else {
            selected_ids.add(field_id);
        }
        const all_selected = selected_ids.size === total_fields;
        return {
            ...item,
            field_selection: all_selected ? undefined : {
                selected_field_ids: selected_ids,
                total_fields,
            },
        };
    });
}
/**
 * Toggle expansion state of a group
 */
export function toggle_group_expansion(items, group_id) {
    return items.map(item => item.id === group_id
        ? { ...item, is_expanded: !item.is_expanded }
        : item);
}
/**
 * Add field to existing group's selection
 */
export function add_field_to_existing_group(items, group_id, field_id) {
    return items.map(item => {
        if (item.id !== group_id)
            return item;
        const group_fields = item.field_config.fields ?? [];
        const total_fields = group_fields.length;
        let selected_ids = item.field_selection?.selected_field_ids
            ?? new Set(group_fields.map(f => f.id));
        selected_ids = new Set(selected_ids);
        selected_ids.add(field_id);
        const all_selected = selected_ids.size === total_fields;
        return {
            ...item,
            field_selection: all_selected ? undefined : {
                selected_field_ids: selected_ids,
                total_fields,
            },
            is_expanded: true,
        };
    });
}
/**
 * Move item from one position to another
 */
export function move_item(items, from_index, to_index, target_section_name) {
    const result = [...items];
    const [removed] = result.splice(from_index, 1);
    const updated_item = target_section_name && target_section_name !== removed.section_name
        ? { ...removed, section_name: target_section_name }
        : removed;
    result.splice(to_index, 0, updated_item);
    return update_item_orders(result);
}
/**
 * Find the section name at a given drop position
 */
export function get_section_at_index(items, drop_index) {
    if (items.length === 0)
        return null;
    if (drop_index >= items.length) {
        return items[items.length - 1].section_name;
    }
    return items[drop_index]?.section_name ?? null;
}
//# sourceMappingURL=utils.js.map