"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.swapInListDeep = exports.duplicateFieldDeep = exports.insertAtTargetDeep = exports.isArrayModeSection = exports.containsId = exports.addChildToSectionDeep = exports.removeFieldDeep = exports.updateFieldDeep = void 0;
const fieldNames_1 = require("./fieldNames.js");
const treeWalk_1 = require("./treeWalk.js");
const updateFieldDeep = (fields, fieldId, updates) => {
    return fields.map((field) => {
        if (field.id === fieldId) {
            return {
                ...field,
                ...updates,
                id: field.id,
            };
        }
        if (field.type === 'section') {
            return {
                ...field,
                fields: (0, exports.updateFieldDeep)(field.fields, fieldId, updates),
            };
        }
        return field;
    });
};
exports.updateFieldDeep = updateFieldDeep;
const removeFieldDeep = (fields, fieldId) => {
    const filtered = fields.filter((field) => field.id !== fieldId);
    return filtered.map((field) => field.type === 'section'
        ? { ...field, fields: (0, exports.removeFieldDeep)(field.fields, fieldId) }
        : field);
};
exports.removeFieldDeep = removeFieldDeep;
const addChildToSectionDeep = (fields, sectionId, newChild) => {
    return fields.map((field) => {
        if (field.id === sectionId && field.type === 'section') {
            return { ...field, fields: [...field.fields, newChild] };
        }
        if (field.type === 'section') {
            return {
                ...field,
                fields: (0, exports.addChildToSectionDeep)(field.fields, sectionId, newChild),
            };
        }
        return field;
    });
};
exports.addChildToSectionDeep = addChildToSectionDeep;
const containsId = (field, id) => (0, treeWalk_1.findField)([field], (f) => f.id === id) !== null;
exports.containsId = containsId;
const isArrayModeSection = (fields, sectionId) => {
    const section = (0, treeWalk_1.findField)(fields, (f) => f.id === sectionId);
    return section?.type === 'section' && Boolean(section.index);
};
exports.isArrayModeSection = isArrayModeSection;
const insertAtTargetDeep = (fields, targetId, offset, makeField) => (0, treeWalk_1.transformAtId)(fields, targetId, (siblings, index, parent) => {
    const arrayMode = Boolean(parent?.index);
    const newField = makeField(arrayMode);
    return [...siblings.slice(0, index + offset), newField, ...siblings.slice(index + offset)];
});
exports.insertAtTargetDeep = insertAtTargetDeep;
const duplicateFieldDeep = (fields, fieldId, generateId, existingNames) => (0, treeWalk_1.transformAtId)(fields, fieldId, (siblings, index) => {
    const clone = (0, fieldNames_1.cloneFieldWithNewIds)(siblings[index], generateId, existingNames);
    return [...siblings.slice(0, index + 1), clone, ...siblings.slice(index + 1)];
});
exports.duplicateFieldDeep = duplicateFieldDeep;
const swapInListDeep = (fields, fieldId, delta) => {
    let didSwap = false;
    const result = (0, treeWalk_1.transformAtId)(fields, fieldId, (siblings, index) => {
        const target = index + delta;
        if (target < 0 || target >= siblings.length) {
            return siblings;
        }
        const next = [...siblings];
        [next[index], next[target]] = [next[target], next[index]];
        didSwap = true;
        return next;
    });
    return result === null || !didSwap ? { fields, handled: false } : { fields: result, handled: true };
};
exports.swapInListDeep = swapInListDeep;
//# sourceMappingURL=fieldTreeOps.js.map
