/**
 * Generic field traversal utility
 *
 * Walks through a nested field structure, visiting each field.
 * Handles groups with child fields recursively.
 */
/**
 * Traverse fields recursively, calling visitor for each field
 *
 * @param fields - Array of fields to traverse
 * @param visitor - Callback function called for each field
 * @param options - Traversal options
 *
 * @example
 * ```ts
 * const all_ids: string[] = [];
 *
 * traverse_fields(fields, (field) => {
 *   all_ids.push(field.id);
 * });
 *
 * // With parent tracking
 * traverse_fields(fields, (field, parent) => {
 *   if (parent) {
 *     console.log(`${field.id} is child of ${parent.id}`);
 *   }
 * });
 * ```
 */
export function traverse_fields(fields, visitor, options = {}) {
    const { include_groups = true, max_depth = -1 } = options;
    const traverse = (field_list, parent_group, depth = 0) => {
        // Check depth limit
        if (max_depth >= 0 && depth > max_depth) {
            return;
        }
        for (const field of field_list) {
            const is_group = field.field_type === 'group';
            // Visit the field (skip groups if not included)
            if (!is_group || include_groups) {
                visitor(field, parent_group, depth);
            }
            // Recurse into child fields
            if (is_group && field.fields && field.fields.length > 0) {
                traverse(field.fields, field, depth + 1);
            }
        }
    };
    traverse(fields);
}
/**
 * Traverse sections, processing all fields within groups
 *
 * @param sections - Array of sections to traverse
 * @param visitor - Callback function called for each field
 * @param options - Traversal options
 *
 * @example
 * ```ts
 * traverse_sections(sections, (field, parent, depth) => {
 *   console.log(`Field ${field.id} at depth ${depth}`);
 * });
 * ```
 */
export function traverse_sections(sections, visitor, options = {}) {
    for (const section of sections) {
        if (section.groups) {
            traverse_fields(section.groups, visitor, options);
        }
    }
}
//# sourceMappingURL=traverse_fields.js.map