/**
 * Utility to convert library ElementData to PendingFieldEntry
 */
import { is_field_element, is_group_element } from '../../hazo_field_library/utils/element_utils.js';
/**
 * Generate a unique ID for pending fields
 */
function generate_id() {
    return `pending_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
}
/**
 * Resolve accept_public_files from element data, handling legacy boolean format
 */
function resolve_accept_mode(data) {
    // New format: accept_public_files as FileAcceptMode string
    const new_val = data.accept_public_files;
    if (new_val === 'ai_upload' || new_val === 'basic_upload' || new_val === 'none') {
        return new_val;
    }
    // Legacy format: accept_files_public as boolean
    const old_val = data.accept_files_public;
    if (old_val === true)
        return 'basic_upload';
    return 'none';
}
/**
 * Convert a field element's component_type to AddFieldType
 * They share the same union, so this is a pass-through with fallback
 */
function map_component_type(component_type) {
    const valid_types = [
        'HazoCollabFormInputbox',
        'HazoCollabFormTextArea',
        'HazoCollabFormCheckbox',
        'HazoCollabFormRadio',
        'HazoCollabFormCombo',
        'HazoCollabFormDate',
        'HazoCollabFormDataTable',
        'HazoCollabFormDoc',
        'Group',
    ];
    if (component_type && valid_types.includes(component_type)) {
        return component_type;
    }
    return 'HazoCollabFormInputbox';
}
/**
 * Extract options from a FieldElementData's input_options
 */
function extract_options(data) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const input_options = data.input_options;
    if (!Array.isArray(input_options) || input_options.length === 0)
        return undefined;
    return input_options.map((opt) => ({
        label: opt.label || '',
        value: opt.value || '',
    }));
}
/**
 * Extract the full source config from a FieldElementData
 * Includes all FieldConfig properties except id, field_type, value
 */
function extract_source_config(data) {
    const { id: _id, field_type: _ft, value: _v, ...rest } = data;
    return rest;
}
/**
 * Convert a FieldElementData to a PendingFieldEntry
 */
export function field_element_to_pending(data, area, key) {
    const field_type = map_component_type(data.component_type);
    const options = extract_options(data);
    return {
        id: generate_id(),
        label: data.label || key,
        field_type,
        required: data.required === true,
        accept_public_files: resolve_accept_mode(data),
        options,
        is_expanded: false,
        source: 'library',
        library_key: `${area}.${key}`,
        source_config: extract_source_config(data),
    };
}
/**
 * Convert a GroupElementData to a PendingFieldEntry
 *
 * @param resolved_fields - Optional pre-resolved child field configs from the library.
 *   When provided, these are stored in source_config.fields so that
 *   new_field_definition_to_config can include them in the resulting FieldConfig.
 */
export function group_element_to_pending(data, area, key, resolved_fields) {
    const entry = {
        id: generate_id(),
        label: data.label || key,
        field_type: 'Group',
        required: false,
        accept_public_files: 'none',
        is_expanded: false,
        source: 'library',
        library_key: `${area}.${key}`,
    };
    // Include resolved child fields in source_config so they survive the
    // NewFieldDefinition → FieldConfig conversion pipeline
    if (resolved_fields && resolved_fields.length > 0) {
        entry.source_config = {
            label: data.label,
            description: data.description,
            fields: resolved_fields,
        };
    }
    return entry;
}
/**
 * Convert any ElementData to a PendingFieldEntry
 * Returns null if the element data is invalid
 */
export function element_to_pending(data, area, key) {
    if (is_field_element(data)) {
        return field_element_to_pending(data, area, key);
    }
    if (is_group_element(data)) {
        return group_element_to_pending(data, area, key);
    }
    return null;
}
//# sourceMappingURL=library_to_pending.js.map