import { getFromImportMap } from '../../bin/generateImportMap/getFromImportMap.js';
import { MissingEditorProp } from '../../errors/MissingEditorProp.js';
import { fieldAffectsData } from '../../fields/config/types.js';
import { flattenTopLevelFields } from '../../index.js';
import { removeUndefined } from '../../utilities/removeUndefined.js';
export const createClientField = ({ clientField = {}, defaultIDType, field: incomingField, i18n, importMap })=>{
    const serverOnlyFieldProperties = [
        'hooks',
        'access',
        'validate',
        'defaultValue',
        'filterOptions',
        'editor',
        'custom',
        'typescriptSchema',
        'dbName',
        'enumName'
    ];
    clientField.admin = clientField.admin || {};
    // clientField.admin.readOnly = true
    serverOnlyFieldProperties.forEach((key)=>{
        if (key in clientField) {
            delete clientField[key];
        }
    });
    const isHidden = 'hidden' in incomingField && incomingField?.hidden;
    const disabledFromAdmin = incomingField?.admin && 'disabled' in incomingField.admin && incomingField.admin.disabled;
    if (fieldAffectsData(clientField) && (isHidden || disabledFromAdmin)) {
        return null;
    }
    if ('label' in clientField && 'label' in incomingField && typeof incomingField.label === 'function') {
        clientField.label = incomingField.label({
            t: i18n.t
        });
    }
    switch(incomingField.type){
        case 'array':
        case 'collapsible':
        case 'group':
        case 'row':
            {
                const field = clientField;
                if (!field.fields) {
                    field.fields = [];
                }
                field.fields = createClientFields({
                    clientFields: field.fields,
                    defaultIDType,
                    disableAddingID: incomingField.type !== 'array',
                    fields: incomingField.fields,
                    i18n,
                    importMap
                });
                break;
            }
        case 'blocks':
            {
                const field = clientField;
                if (incomingField.blocks?.length) {
                    for(let i = 0; i < incomingField.blocks.length; i++){
                        const block = incomingField.blocks[i];
                        // prevent $undefined from being passed through the rsc requests
                        const clientBlock = removeUndefined({
                            slug: block.slug,
                            fields: field.blocks?.[i]?.fields || [],
                            imageAltText: block.imageAltText,
                            imageURL: block.imageURL
                        });
                        if (block.admin?.custom) {
                            clientBlock.admin = {
                                custom: block.admin.custom
                            };
                        }
                        if (block?.admin?.jsx) {
                            const jsxResolved = getFromImportMap({
                                importMap,
                                PayloadComponent: block.admin.jsx,
                                schemaPath: ''
                            });
                            clientBlock.jsx = jsxResolved;
                        }
                        if (block.labels) {
                            clientBlock.labels = {};
                            if (block.labels.singular) {
                                if (typeof block.labels.singular === 'function') {
                                    clientBlock.labels.singular = block.labels.singular({
                                        t: i18n.t
                                    });
                                } else {
                                    clientBlock.labels.singular = block.labels.singular;
                                }
                                if (typeof block.labels.plural === 'function') {
                                    clientBlock.labels.plural = block.labels.plural({
                                        t: i18n.t
                                    });
                                } else {
                                    clientBlock.labels.plural = block.labels.plural;
                                }
                            }
                        }
                        clientBlock.fields = createClientFields({
                            clientFields: clientBlock.fields,
                            defaultIDType,
                            fields: block.fields,
                            i18n,
                            importMap
                        });
                        if (!field.blocks) {
                            field.blocks = [];
                        }
                        field.blocks[i] = clientBlock;
                    }
                }
                break;
            }
        case 'radio':
        // falls through
        case 'select':
            {
                const field = clientField;
                if (incomingField.options?.length) {
                    for(let i = 0; i < incomingField.options.length; i++){
                        const option = incomingField.options[i];
                        if (typeof option === 'object' && typeof option.label === 'function') {
                            if (!field.options) {
                                field.options = [];
                            }
                            field.options[i] = {
                                label: option.label({
                                    t: i18n.t
                                }),
                                value: option.value
                            };
                        }
                    }
                }
                break;
            }
        case 'richText':
            {
                if (!incomingField?.editor) {
                    throw new MissingEditorProp(incomingField) // while we allow disabling editor functionality, you should not have any richText fields defined if you do not have an editor
                    ;
                }
                if (typeof incomingField?.editor === 'function') {
                    throw new Error('Attempted to access unsanitized rich text editor.');
                }
                break;
            }
        case 'tabs':
            {
                const field = clientField;
                if (incomingField.tabs?.length) {
                    for(let i = 0; i < incomingField.tabs.length; i++){
                        const tab = incomingField.tabs[i];
                        const clientTab = field.tabs[i];
                        serverOnlyFieldProperties.forEach((key)=>{
                            if (key in clientTab) {
                                delete clientTab[key];
                            }
                        });
                        clientTab.fields = createClientFields({
                            clientFields: clientTab.fields,
                            defaultIDType,
                            disableAddingID: true,
                            fields: tab.fields,
                            i18n,
                            importMap
                        });
                    }
                }
                break;
            }
        default:
            break;
    }
    const serverOnlyFieldAdminProperties = [
        'condition'
    ];
    if (!clientField.admin) {
        clientField.admin = {};
    }
    serverOnlyFieldAdminProperties.forEach((key)=>{
        if (key in clientField.admin) {
            delete clientField.admin[key];
        }
    });
    if (incomingField.admin && 'description' in incomingField.admin) {
        if (typeof incomingField.admin?.description === 'function') {
            delete clientField.admin.description;
        } else {
            clientField.admin.description = incomingField.admin.description;
        }
    }
    return clientField;
};
export const createClientFields = ({ clientFields, defaultIDType, disableAddingID, fields, i18n, importMap })=>{
    const newClientFields = [];
    for(let i = 0; i < fields.length; i++){
        const field = fields[i];
        const newField = createClientField({
            clientField: clientFields[i],
            defaultIDType,
            field,
            i18n,
            importMap
        });
        if (newField) {
            newClientFields.push(newField);
        }
    }
    const hasID = flattenTopLevelFields(fields).some((f)=>fieldAffectsData(f) && f.name === 'id');
    if (!disableAddingID && !hasID) {
        newClientFields.push({
            name: 'id',
            type: defaultIDType,
            admin: {
                description: 'The unique identifier for this document',
                disableBulkEdit: true,
                disabled: true,
                hidden: true
            },
            hidden: true,
            label: 'ID'
        });
    }
    return newClientFields;
};

//# sourceMappingURL=client.js.map