import React from 'react';
import { mapActions } from './mapActions.js';
import { mapFields } from './mapFields.js';
export const buildComponentMap = (args)=>{
    const { DefaultCell, DefaultEditView, DefaultListView, children, config, permissions, readOnly: readOnlyOverride } = args;
    let entityPermissions;
    // Collections
    const collections = config.collections.reduce((acc, collectionConfig)=>{
        const { slug, fields } = collectionConfig;
        entityPermissions = permissions?.collections?.[collectionConfig.slug];
        const editViewFromConfig = collectionConfig?.admin?.components?.views?.Edit;
        const listViewFromConfig = collectionConfig?.admin?.components?.views?.List;
        const CustomEditView = typeof editViewFromConfig === 'function' ? editViewFromConfig : typeof editViewFromConfig === 'object' && typeof editViewFromConfig.Default === 'function' ? editViewFromConfig.Default : typeof editViewFromConfig?.Default === 'object' && 'Component' in editViewFromConfig.Default && typeof editViewFromConfig.Default.Component === 'function' ? editViewFromConfig.Default.Component : undefined;
        const CustomListView = typeof listViewFromConfig === 'function' ? listViewFromConfig : typeof listViewFromConfig === 'object' && typeof listViewFromConfig.Component === 'function' ? listViewFromConfig.Component : undefined;
        const Edit = CustomEditView || DefaultEditView;
        const List = CustomListView || DefaultListView;
        const beforeList = collectionConfig?.admin?.components?.BeforeList;
        const beforeListTable = collectionConfig?.admin?.components?.BeforeListTable;
        const afterList = collectionConfig?.admin?.components?.AfterList;
        const afterListTable = collectionConfig?.admin?.components?.AfterListTable;
        const BeforeList = beforeList && Array.isArray(beforeList) && beforeList?.map((Component)=>/*#__PURE__*/ React.createElement(Component, null)) || null;
        const BeforeListTable = beforeListTable && Array.isArray(beforeListTable) && beforeListTable?.map((Component)=>/*#__PURE__*/ React.createElement(Component, null)) || null;
        const AfterList = afterList && Array.isArray(afterList) && afterList?.map((Component)=>/*#__PURE__*/ React.createElement(Component, null)) || null;
        const AfterListTable = afterListTable && Array.isArray(afterListTable) && afterListTable?.map((Component)=>/*#__PURE__*/ React.createElement(Component, null)) || null;
        let AdminThumbnail = null;
        if (typeof collectionConfig?.upload?.adminThumbnail === 'function') {
            AdminThumbnail = collectionConfig?.upload?.adminThumbnail;
        } else if (typeof collectionConfig?.upload?.adminThumbnail === 'string') {
            AdminThumbnail = ()=>collectionConfig?.upload?.adminThumbnail;
        }
        const componentMap = {
            AdminThumbnail,
            AfterList,
            AfterListTable,
            BeforeList,
            BeforeListTable,
            Edit: /*#__PURE__*/ React.createElement(Edit, {
                collectionSlug: collectionConfig.slug
            }),
            List: /*#__PURE__*/ React.createElement(List, {
                collectionSlug: collectionConfig.slug
            }),
            actionsMap: mapActions({
                collectionConfig
            }),
            fieldMap: mapFields({
                DefaultCell,
                config,
                fieldSchema: fields,
                permissions: entityPermissions?.fields,
                readOnly: readOnlyOverride
            })
        };
        return {
            ...acc,
            [slug]: componentMap
        };
    }, {});
    // Globals
    const globals = config.globals.reduce((acc, globalConfig)=>{
        const { slug, fields } = globalConfig;
        entityPermissions = permissions?.globals?.[globalConfig.slug];
        const editViewFromConfig = globalConfig?.admin?.components?.views?.Edit;
        const CustomEditView = typeof editViewFromConfig === 'function' ? editViewFromConfig : typeof editViewFromConfig === 'object' && typeof editViewFromConfig.Default === 'function' ? editViewFromConfig.Default : typeof editViewFromConfig?.Default === 'object' && 'Component' in editViewFromConfig.Default && typeof editViewFromConfig.Default.Component === 'function' ? editViewFromConfig.Default.Component : undefined;
        const Edit = CustomEditView || DefaultEditView;
        const componentMap = {
            Edit: /*#__PURE__*/ React.createElement(Edit, {
                globalSlug: globalConfig.slug
            }),
            actionsMap: mapActions({
                globalConfig
            }),
            fieldMap: mapFields({
                DefaultCell,
                config,
                fieldSchema: fields,
                permissions: entityPermissions?.fields,
                readOnly: readOnlyOverride
            })
        };
        return {
            ...acc,
            [slug]: componentMap
        };
    }, {});
    const NestProviders = ({ children, providers })=>{
        const Component = providers[0];
        if (providers.length > 1) {
            return /*#__PURE__*/ React.createElement(Component, null, /*#__PURE__*/ React.createElement(NestProviders, {
                providers: providers.slice(1)
            }, children));
        }
        return /*#__PURE__*/ React.createElement(Component, null, children);
    };
    const LogoutButtonComponent = config.admin?.components?.logout?.Button;
    const LogoutButton = LogoutButtonComponent ? /*#__PURE__*/ React.createElement(LogoutButtonComponent, null) : null;
    const IconComponent = config.admin?.components?.graphics?.Icon;
    const Icon = IconComponent ? /*#__PURE__*/ React.createElement(IconComponent, null) : null;
    return {
        componentMap: {
            Icon,
            LogoutButton,
            actions: config.admin?.components?.actions?.map((Component)=>/*#__PURE__*/ React.createElement(Component, null)),
            collections,
            globals
        },
        wrappedChildren: Array.isArray(config.admin?.components?.providers) && config.admin?.components?.providers.length > 0 ? /*#__PURE__*/ React.createElement(NestProviders, {
            providers: config.admin?.components?.providers
        }, children) : children
    };
};

//# sourceMappingURL=index.js.map