/**
 * Convert form template sections into ExportElement format
 * Client-side port of seed-field-library.mjs conversion logic
 */
import { format_full_key } from './key_utils.js';
/**
 * Convert section_name to a snake_case area key
 */
export function section_to_area(name) {
    return name
        .toLowerCase()
        .replace(/[^a-z0-9\s]/g, '')
        .trim()
        .replace(/\s+/g, '_');
}
/**
 * Process a field (leaf element) and return an ExportElement
 */
function process_field(field, area) {
    const element = { ...field };
    delete element.value;
    // Migrate legacy boolean accept_files_* → FileAcceptMode accept_*_files
    if (element.accept_public_files === undefined && element.accept_files_public !== undefined) {
        element.accept_public_files = element.accept_files_public === true ? 'basic_upload' : 'none';
        delete element.accept_files_public;
    }
    if (element.accept_private_files === undefined && element.accept_files_private !== undefined) {
        element.accept_private_files = element.accept_files_private === true ? 'basic_upload' : 'none';
        delete element.accept_files_private;
    }
    return {
        area,
        key: String(field.id || ''),
        element: element,
    };
}
/**
 * Process a group recursively: extract child fields as separate elements,
 * replace inline fields with reference_element array
 */
function process_group(group, area) {
    const results = [];
    const inline_fields = group.fields || [];
    const refs = [];
    for (const child of inline_fields) {
        if (!child || !child.id)
            continue;
        if (child.field_type === 'group') {
            const nested = process_group(child, area);
            results.push(...nested);
        }
        else {
            results.push(process_field(child, area));
        }
        refs.push({ key: format_full_key(area, String(child.id)) });
    }
    // Build group element without inline fields
    const group_element = {
        id: group.id,
        label: group.label,
        field_type: 'group',
    };
    // Carry over optional group properties
    for (const prop of ['description', 'accept_public_files', 'accept_private_files', 'private_files_permission', 'dependency', 'tooltip']) {
        if (group[prop] !== undefined) {
            group_element[prop] = group[prop];
        }
    }
    // Migrate legacy boolean accept_files_public/private → FileAcceptMode accept_public/private_files
    if (group_element.accept_public_files === undefined && group.accept_files_public !== undefined) {
        group_element.accept_public_files = group.accept_files_public === true ? 'basic_upload' : 'none';
    }
    if (group_element.accept_private_files === undefined && group.accept_files_private !== undefined) {
        group_element.accept_private_files = group.accept_files_private === true ? 'basic_upload' : 'none';
    }
    group_element.reference_element = refs;
    results.push({
        area,
        key: String(group.id || ''),
        element: group_element,
    });
    return results;
}
/**
 * Convert template sections into ExportElement array.
 * Deduplicates by area.key within the batch.
 */
export function template_to_elements(sections) {
    const all = [];
    const seen = new Set();
    for (const section of sections) {
        const area = section_to_area(section.section_name || 'unnamed');
        const groups = section.groups || [];
        for (const item of groups) {
            if (!item || !item.id)
                continue;
            let records;
            if (item.field_type === 'group') {
                records = process_group(item, area);
            }
            else {
                records = [process_field(item, area)];
            }
            for (const r of records) {
                const full_key = format_full_key(r.area, r.key);
                if (!seen.has(full_key)) {
                    seen.add(full_key);
                    all.push(r);
                }
            }
        }
    }
    return all;
}
//# sourceMappingURL=template_to_elements.js.map