import { walkFields } from "./treeWalk.js";
export const ARRAY_ITEM_PREFIX = 'items[{{index}}].';
export const prefixNameForArrayMode = (name) => name.includes('{{index}}') ? name : `${ARRAY_ITEM_PREFIX}${name}`;
export const stripArrayModePrefix = (name) => name.startsWith(ARRAY_ITEM_PREFIX) ? name.slice(ARRAY_ITEM_PREFIX.length) : name;
export const collectNames = (fields) => {
    const names = new Set();
    walkFields(fields, (field) => {
        if (field.type !== 'section' && field.type !== 'text') {
            names.add(field.name);
        }
    });
    return names;
};
const extractBaseName = (name) => {
    const m = name.match(/^(.+?)_copy(?:_\d+)?$/);
    return m ? m[1] : name;
};
const findUniqueCopyName = (originalName, existing) => {
    const stem = extractBaseName(originalName);
    let candidate = `${stem}_copy`;
    let n = 2;
    while (existing.has(candidate)) {
        candidate = `${stem}_copy_${n}`;
        n += 1;
    }
    return candidate;
};
export const cloneFieldWithNewIds = (field, generateId, existingNames) => {
    const newId = generateId();
    if (field.type === 'section') {
        return {
            ...field,
            id: newId,
            title: field.title ? `${field.title} (copy)` : 'Section (copy)',
            fields: field.fields.map((child) => cloneFieldWithNewIds(child, generateId, existingNames)),
        };
    }
    if (field.type === 'text') {
        return { ...field, id: newId };
    }
    const newName = findUniqueCopyName(field.name, existingNames);
    existingNames.add(newName);
    return { ...field, id: newId, name: newName };
};
export const transformChildNames = (fields, transform) => fields.map((field) => {
    if (field.type === 'section') {
        return { ...field, fields: transformChildNames(field.fields, transform) };
    }
    if (field.type === 'text') {
        return field;
    }
    return { ...field, name: transform(field.name) };
});
export const maxIdNumericSuffix = (fields) => {
    let max = 0;
    walkFields(fields, (field) => {
        const match = /^fb2_(\d+)$/.exec(field.id);
        if (match) {
            const n = parseInt(match[1] ?? '0', 10);
            if (n > max)
                max = n;
        }
    });
    return max;
};
//# sourceMappingURL=fieldNames.js.map
