import { fieldHasSubFields } from '../fields/config/types.js';
const traverseArrayOrBlocksField = ({ callback, data, field, parentRef })=>{
    for (const ref of data){
        let fields;
        if (field.type === 'blocks' && typeof ref?.blockType === 'string') {
            const block = field.blocks.find((block)=>block.slug === ref.blockType);
            fields = block?.fields;
        } else if (field.type === 'array') {
            fields = field.fields;
        }
        if (fields) {
            traverseFields({
                callback,
                fields,
                parentRef,
                ref
            });
        }
    }
};
/**
 * Iterate a recurse an array of fields, calling a callback for each field
 *
 * @param fields
 * @param callback callback called for each field, discontinue looping if callback returns truthy
 * @param ref
 * @param parentRef
 */ export const traverseFields = ({ callback, fields, parentRef = {}, ref = {} })=>{
    fields.some((field)=>{
        let skip = false;
        const next = ()=>{
            skip = true;
        };
        if (callback && callback({
            field,
            next,
            parentRef,
            ref
        })) {
            return true;
        }
        if (skip) {
            return false;
        }
        // avoid mutation of ref for all fields
        let currentRef = ref;
        if (field.type === 'tabs' && 'tabs' in field) {
            for (const tab of field.tabs){
                if ('name' in tab && tab.name) {
                    if (typeof ref[tab.name] === 'undefined') {
                        ref[tab.name] = {};
                    }
                    parentRef = ref;
                    currentRef = ref[tab.name];
                    if (tab.localized) {
                        for(const key in currentRef){
                            if (currentRef[key]) {
                                traverseFields({
                                    callback,
                                    fields: tab.fields,
                                    parentRef,
                                    ref: currentRef[key]
                                });
                            }
                        }
                        continue;
                    }
                }
                if (callback && callback({
                    field: {
                        ...tab,
                        type: 'tab'
                    },
                    next,
                    parentRef,
                    ref: currentRef
                })) {
                    return true;
                }
                traverseFields({
                    callback,
                    fields: tab.fields,
                    parentRef,
                    ref: currentRef
                });
            }
            return;
        }
        if (field.type !== 'tab' && (fieldHasSubFields(field) || field.type === 'blocks')) {
            const parentRef = currentRef;
            if ('name' in field && field.name) {
                if (typeof ref[field.name] === 'undefined') {
                    if (field.type === 'array' || field.type === 'blocks') {
                        if (field.localized) {
                            ref[field.name] = {};
                        } else {
                            ref[field.name] = [];
                        }
                    }
                    if (field.type === 'group') {
                        ref[field.name] = {};
                    }
                }
                currentRef = ref[field.name];
            }
            if (field.type === 'group' && field.localized) {
                for(const key in currentRef){
                    if (currentRef[key]) {
                        traverseFields({
                            callback,
                            fields: field.fields,
                            parentRef,
                            ref: currentRef[key]
                        });
                    }
                }
                return;
            }
            if (field.type === 'blocks' || field.type === 'array') {
                if (field.localized) {
                    for(const key in currentRef ?? {}){
                        const localeData = currentRef[key];
                        if (!Array.isArray(localeData)) {
                            continue;
                        }
                        traverseArrayOrBlocksField({
                            callback,
                            data: localeData,
                            field,
                            parentRef
                        });
                    }
                } else if (Array.isArray(currentRef)) {
                    traverseArrayOrBlocksField({
                        callback,
                        data: currentRef,
                        field,
                        parentRef
                    });
                }
            } else {
                traverseFields({
                    callback,
                    fields: field.fields,
                    parentRef,
                    ref: currentRef
                });
            }
        }
    });
};

//# sourceMappingURL=traverseFields.js.map