/**
 * Section manipulation utilities for dynamic field/group insertion.
 */
/**
 * Insert a field after a given field or group ID in sections.
 * If `id` is not provided in field_config, one is auto-generated.
 *
 * @param sections - The sections array to modify (immutable — returns new array)
 * @param after_id - The field or group ID to insert after
 * @param field_config - Field configuration (id, label, field_type, etc.)
 * @returns New sections array with the field inserted
 */
export function insert_field_after(sections, after_id, field_config) {
    const new_field = {
        id: field_config.id ?? `field_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
        ...field_config,
    };
    return sections.map((section) => ({
        ...section,
        groups: (section.groups ?? []).map((group) => {
            // If after_id matches a group, append to the group's fields
            if (group.id === after_id) {
                return { ...group, fields: [...(group.fields || []), new_field] };
            }
            // If after_id matches a field within this group, insert after it
            const fields = group.fields || [];
            const field_index = fields.findIndex((f) => f.id === after_id);
            if (field_index !== -1) {
                const new_fields = [...fields];
                new_fields.splice(field_index + 1, 0, new_field);
                return { ...group, fields: new_fields };
            }
            return group;
        }),
    }));
}
//# sourceMappingURL=section_helpers.js.map