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';
const serverOnlyFieldProperties = [
    'hooks',
    'access',
    'validate',
    'defaultValue',
    'filterOptions',
    'editor',
    'custom',
    'typescriptSchema',
    'dbName',
    'enumName'
];
const serverOnlyFieldAdminProperties = [
    'condition'
];
export const createClientField = ({ defaultIDType, field: incomingField, i18n, importMap })=>{
    const clientField = {};
    const isHidden = 'hidden' in incomingField && incomingField?.hidden;
    const disabledFromAdmin = incomingField?.admin && 'disabled' in incomingField.admin && incomingField.admin.disabled;
    if (fieldAffectsData(incomingField) && (isHidden || disabledFromAdmin)) {
        return null;
    }
    for(const key in incomingField){
        if (serverOnlyFieldProperties.includes(key)) {
            continue;
        }
        switch(key){
            case 'admin':
                if (!incomingField.admin) {
                    break;
                }
                clientField.admin = {};
                for(const adminKey in incomingField.admin){
                    if (serverOnlyFieldAdminProperties.includes(adminKey)) {
                        continue;
                    }
                    switch(adminKey){
                        case 'description':
                            if ('description' in incomingField.admin) {
                                if (typeof incomingField.admin?.description !== 'function') {
                                    clientField.admin.description = incomingField.admin.description;
                                }
                            }
                            break;
                        default:
                            clientField.admin[adminKey] = incomingField.admin[adminKey];
                    }
                }
                break;
            case 'blocks':
            case 'fields':
            case 'tabs':
                break;
            case 'label':
                //@ts-expect-error - would need to type narrow
                if (typeof incomingField.label === 'function') {
                    //@ts-expect-error - would need to type narrow
                    clientField.label = incomingField.label({
                        t: i18n.t
                    });
                } else {
                    //@ts-expect-error - would need to type narrow
                    clientField.label = incomingField.label;
                }
                break;
            default:
                clientField[key] = incomingField[key];
        }
    }
    switch(incomingField.type){
        case 'array':
        case 'collapsible':
        case 'group':
        case 'row':
            {
                const field = clientField;
                if (!field.fields) {
                    field.fields = [];
                }
                field.fields = createClientFields({
                    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({
                            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) {
                    field.tabs = [];
                    for(let i = 0; i < incomingField.tabs.length; i++){
                        const tab = incomingField.tabs[i];
                        const clientTab = {};
                        for(const key in tab){
                            if (serverOnlyFieldProperties.includes(key)) {
                                continue;
                            }
                            if (key === 'fields') {
                                clientTab.fields = createClientFields({
                                    defaultIDType,
                                    disableAddingID: true,
                                    fields: tab.fields,
                                    i18n,
                                    importMap
                                });
                            } else {
                                clientTab[key] = tab[key];
                            }
                        }
                        field.tabs[i] = clientTab;
                    }
                }
                break;
            }
        default:
            break;
    }
    return clientField;
};
export const createClientFields = ({ defaultIDType, disableAddingID, fields, i18n, importMap })=>{
    const clientFields = [];
    for(let i = 0; i < fields.length; i++){
        const field = fields[i];
        const clientField = createClientField({
            defaultIDType,
            field,
            i18n,
            importMap
        });
        if (clientField) {
            clientFields.push(clientField);
        }
    }
    const hasID = flattenTopLevelFields(fields).some((f)=>fieldAffectsData(f) && f.name === 'id');
    if (!disableAddingID && !hasID) {
        clientFields.push({
            name: 'id',
            type: defaultIDType,
            admin: {
                description: 'The unique identifier for this document',
                disableBulkEdit: true,
                disabled: true,
                hidden: true
            },
            hidden: true,
            label: 'ID'
        });
    }
    return clientFields;
};

//# sourceMappingURL=client.js.map