/**
 * Template validation utilities
 */
import { get_fields, build_path } from './template_utils.js';
/**
 * Validate entire template (sections array with optional icons_behaviour)
 */
export function validate_template(sections, icons_behaviour) {
    const errors = [];
    const seen_ids = new Set();
    // Validate that there's at least one section
    if (sections.length === 0) {
        errors.push({
            field_path: '',
            field_id: 'template',
            message: 'Template must have at least one section',
        });
        return errors;
    }
    // Validate icons_behaviour if present
    if (icons_behaviour) {
        validate_icons_behaviour(icons_behaviour, errors);
    }
    // Validate each section
    for (let section_index = 0; section_index < sections.length; section_index++) {
        const section = sections[section_index];
        validate_section(section, section_index, errors, seen_ids);
    }
    return errors;
}
/**
 * Validate a single section
 */
function validate_section(section, section_index, errors, seen_ids) {
    const section_path = `s${section_index}`;
    // Validate section name
    if (!section.section_name || section.section_name.trim() === '') {
        errors.push({
            field_path: section_path,
            field_id: `section_${section_index}`,
            message: `Section ${section_index + 1} has no name`,
        });
    }
    // Validate fields/groups within section
    const fields = get_fields(section);
    validate_field_list(fields, section_index, [], errors, seen_ids);
}
/**
 * Validate icons_behaviour configuration
 */
function validate_icons_behaviour(icons_behaviour, errors) {
    // Validate roles
    if (icons_behaviour.roles && icons_behaviour.roles.length > 0) {
        const seen_role_ids = new Set();
        for (const role of icons_behaviour.roles) {
            // Check for duplicate role IDs
            if (seen_role_ids.has(role.role_id)) {
                errors.push({
                    field_path: 'icons_behaviour',
                    field_id: 'roles',
                    message: `Duplicate role ID: "${role.role_id}"`,
                });
            }
            else {
                seen_role_ids.add(role.role_id);
            }
            // Check for empty role name
            if (!role.role_name || role.role_name.trim() === '') {
                errors.push({
                    field_path: 'icons_behaviour',
                    field_id: role.role_id,
                    message: `Role "${role.role_id}" has an empty name`,
                });
            }
        }
    }
}
/**
 * Validate a list of fields
 */
function validate_field_list(fields, section_index, parent_indices, errors, seen_ids) {
    for (let i = 0; i < fields.length; i++) {
        const field = fields[i];
        const field_indices = [...parent_indices, i];
        const path = build_path(section_index, field_indices);
        validate_field(field, path, errors, seen_ids);
    }
}
/**
 * Validate a single field
 */
function validate_field(field, path, errors, seen_ids) {
    // ID validation
    if (!field.id || field.id.trim() === '') {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'Field ID is required',
        });
    }
    else if (seen_ids.has(field.id)) {
        errors.push({
            field_path: path,
            field_id: field.id,
            message: `Duplicate field ID: "${field.id}"`,
        });
    }
    else {
        seen_ids.add(field.id);
        // Validate ID format
        if (!/^[a-z][a-z0-9_]*$/i.test(field.id)) {
            errors.push({
                field_path: path,
                field_id: field.id,
                message: 'Field ID must start with a letter and contain only letters, numbers, and underscores',
            });
        }
    }
    // Label validation
    if (!field.label || field.label.trim() === '') {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'Field label is required',
        });
    }
    // Field type validation
    if (field.field_type === 'field') {
        validate_component_field(field, path, errors);
    }
    else if (field.field_type === 'group') {
        validate_group_field(field, path, errors, seen_ids);
    }
    // Dependency validation
    if (field.dependency) {
        validate_dependency(field.dependency, path, field.id, errors);
    }
    // File upload validation
    if ((field.accept_public_files && field.accept_public_files !== 'none') ||
        (field.accept_private_files && field.accept_private_files !== 'none')) {
        validate_file_upload(field, path, errors);
    }
}
/**
 * Validate a component field
 */
function validate_component_field(field, path, errors) {
    // Component type validation
    const valid_types = [
        'HazoCollabFormInputbox',
        'HazoCollabFormTextArea',
        'HazoCollabFormCheckbox',
        'HazoCollabFormRadio',
        'HazoCollabFormCombo',
        'HazoCollabFormDate',
        'HazoCollabFormDataTable',
        'HazoCollabFormDoc',
    ];
    if (!field.component_type) {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'Component type is required for fields',
        });
    }
    else if (!valid_types.includes(field.component_type)) {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: `Invalid component type: "${field.component_type}"`,
        });
    }
    // Options validation for radio/combo
    if ((field.component_type === 'HazoCollabFormRadio' ||
        field.component_type === 'HazoCollabFormCombo') &&
        (!field.input_options || field.input_options.length === 0)) {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'Radio and Combo fields require at least one option',
        });
    }
    // DataTable validation
    if (field.component_type === 'HazoCollabFormDataTable') {
        validate_data_table(field, path, errors);
    }
    // Input format validation
    if (field.input_format) {
        validate_input_format(field, path, errors);
    }
}
/**
 * Validate a group field
 */
function validate_group_field(field, path, errors, seen_ids) {
    // Groups should have fields
    if (!field.fields || !Array.isArray(field.fields)) {
        // Not an error, just no children
        return;
    }
    // Parse path to get section index and field indices
    const parts = path.substring(1).split('.'); // Remove 's' prefix
    const section_index = parseInt(parts[0], 10);
    const parent_indices = parts.slice(1).map(Number);
    // Validate nested fields
    validate_field_list(field.fields, section_index, parent_indices, errors, seen_ids);
}
/**
 * Validate DataTable configuration
 */
function validate_data_table(field, path, errors) {
    if (!field.table_config) {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'DataTable fields require table_config',
        });
        return;
    }
    if (!field.table_config.columns || field.table_config.columns.length === 0) {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'DataTable requires at least one column',
        });
        return;
    }
    // Validate each column
    const seen_column_ids = new Set();
    for (let i = 0; i < field.table_config.columns.length; i++) {
        const column = field.table_config.columns[i];
        validate_data_table_column(column, path, i, field.id, errors, seen_column_ids);
    }
}
/**
 * Validate a DataTable column
 */
function validate_data_table_column(column, field_path, column_index, field_id, errors, seen_ids) {
    const column_path = `${field_path}.columns[${column_index}]`;
    if (!column.id || column.id.trim() === '') {
        errors.push({
            field_path: column_path,
            field_id: field_id,
            message: `Column ${column_index + 1}: ID is required`,
        });
    }
    else if (seen_ids.has(column.id)) {
        errors.push({
            field_path: column_path,
            field_id: field_id,
            message: `Column ${column_index + 1}: Duplicate column ID "${column.id}"`,
        });
    }
    else {
        seen_ids.add(column.id);
    }
    if (!column.label || column.label.trim() === '') {
        errors.push({
            field_path: column_path,
            field_id: field_id,
            message: `Column ${column_index + 1}: Label is required`,
        });
    }
    // Validate options for dropdown/radio columns
    if ((column.field_type === 'dropdown' || column.field_type === 'radiobutton') &&
        (!column.options || column.options.length === 0)) {
        errors.push({
            field_path: column_path,
            field_id: field_id,
            message: `Column ${column_index + 1}: Dropdown/Radio columns require at least one option`,
        });
    }
}
/**
 * Validate dependency format
 */
function validate_dependency(dependency, path, field_id, errors) {
    const parts = dependency.split(':');
    if (parts.length !== 2) {
        errors.push({
            field_path: path,
            field_id: field_id || 'unknown',
            message: `Invalid dependency format: "${dependency}". Expected "field_id:value"`,
        });
    }
}
/**
 * Validate input format
 */
function validate_input_format(field, path, errors) {
    const format = field.input_format;
    if (!format)
        return;
    // Validate numeric constraints
    if (format.num_min !== undefined && format.num_max !== undefined) {
        if (format.num_min > format.num_max) {
            errors.push({
                field_path: path,
                field_id: field.id || 'unknown',
                message: 'Minimum value cannot be greater than maximum value',
            });
        }
    }
    // Validate text length constraints
    if (format.text_min_len !== undefined && format.text_max_len !== undefined) {
        if (format.text_min_len > format.text_max_len) {
            errors.push({
                field_path: path,
                field_id: field.id || 'unknown',
                message: 'Minimum length cannot be greater than maximum length',
            });
        }
    }
    // Validate regex
    if (format.regex) {
        try {
            new RegExp(format.regex);
        }
        catch {
            errors.push({
                field_path: path,
                field_id: field.id || 'unknown',
                message: `Invalid regex pattern: "${format.regex}"`,
            });
        }
    }
}
/**
 * Validate file upload configuration
 */
function validate_file_upload(field, path, errors) {
    if (!field.files_dir || field.files_dir.trim() === '') {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'File upload requires files_dir to be specified',
        });
    }
    if (field.min_files !== undefined && field.max_files !== undefined) {
        if (field.min_files > field.max_files) {
            errors.push({
                field_path: path,
                field_id: field.id || 'unknown',
                message: 'Minimum files cannot be greater than maximum files',
            });
        }
    }
    if (field.max_size !== undefined && field.max_size <= 0) {
        errors.push({
            field_path: path,
            field_id: field.id || 'unknown',
            message: 'Maximum file size must be greater than 0',
        });
    }
}
/**
 * Quick validation check - returns true if template is valid
 */
export function is_template_valid(sections, icons_behaviour) {
    return validate_template(sections, icons_behaviour).length === 0;
}
//# sourceMappingURL=validation.js.map