"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformAtId = exports.findField = exports.walkFields = void 0;
const walkFields = (fields, visit) => {
    for (const field of fields) {
        visit(field);
        if (field.type === 'section') {
            (0, exports.walkFields)(field.fields, visit);
        }
    }
};
exports.walkFields = walkFields;
const findField = (fields, predicate) => {
    for (const field of fields) {
        if (predicate(field))
            return field;
        if (field.type === 'section') {
            const nested = (0, exports.findField)(field.fields, predicate);
            if (nested)
                return nested;
        }
    }
    return null;
};
exports.findField = findField;
const transformAtId = (fields, id, handler, parent = null) => {
    const index = fields.findIndex((field) => field.id === id);
    if (index !== -1) {
        return handler(fields, index, parent);
    }
    for (let i = 0; i < fields.length; i += 1) {
        const field = fields[i];
        if (field.type === 'section') {
            const result = (0, exports.transformAtId)(field.fields, id, handler, field);
            if (result !== null) {
                const next = [...fields];
                next[i] = { ...field, fields: result };
                return next;
            }
        }
    }
    return null;
};
exports.transformAtId = transformAtId;
//# sourceMappingURL=treeWalk.js.map
