import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
const ParentCollectionFields = ()=>{
    const inheritFields = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useInheritsFormItemInitializerFields)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    if (!(null == inheritFields ? void 0 : inheritFields.length)) return null;
    const res = [];
    inheritFields.forEach((inherit)=>{
        Object.values(inherit)[0].length && res.push({
            type: 'itemGroup',
            divider: true,
            title: t("Parent collection fields") + '(' + compile("".concat(Object.keys(inherit)[0])) + ')',
            children: Object.values(inherit)[0]
        });
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerChildren, {
        children: res
    });
};
const ExtendCollectionFields = (props)=>{
    const collections = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useExtendCollections)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { getInterface, getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    if (!collections) return null;
    const children = collections.map((collection)=>{
        var _collection_fields_filter, _collection_fields;
        const readPretty = form.readPretty;
        const block = 'Form';
        const action = null == fieldSchema ? void 0 : fieldSchema['x-action'];
        return {
            type: 'subMenu',
            title: compile(collection.title),
            children: null === (_collection_fields = collection.fields) || void 0 === _collection_fields ? void 0 : null === (_collection_fields_filter = _collection_fields.filter((field)=>(null == field ? void 0 : field.interface) && !(null == field ? void 0 : field.isForeignKey) && !(null == field ? void 0 : field.treeChildren))) || void 0 === _collection_fields_filter ? void 0 : _collection_fields_filter.map((field)=>{
                var _getCollection, _field_uiSchema_xcomponentprops, _field_uiSchema, _field_uiSchema1;
                const interfaceConfig = getInterface(field.interface);
                const targetCollection = getCollection(field.target);
                const isFileCollection = (null == field ? void 0 : field.target) && (null === (_getCollection = getCollection(null == field ? void 0 : field.target)) || void 0 === _getCollection ? void 0 : _getCollection.template) === 'file';
                const isAssociationField = targetCollection;
                const fieldNames = null == field ? void 0 : null === (_field_uiSchema_xcomponentprops = field.uiSchema['x-component-props']) || void 0 === _field_uiSchema_xcomponentprops ? void 0 : _field_uiSchema_xcomponentprops['fieldNames'];
                const schema = {
                    type: 'string',
                    name: field.name,
                    'x-toolbar': 'FormItemSchemaToolbar',
                    'x-settings': 'fieldSettings:FormItem',
                    'x-component': 'CollectionField',
                    'x-decorator': 'FormItem',
                    'x-collection-field': "".concat(collection.name, ".").concat(field.name),
                    'x-component-props': isFileCollection ? {
                        fieldNames: {
                            label: 'preview',
                            value: 'id'
                        }
                    } : isAssociationField && fieldNames ? {
                        fieldNames: {
                            ...fieldNames,
                            label: (null == targetCollection ? void 0 : targetCollection.titleField) || fieldNames.label
                        }
                    } : {},
                    'x-read-pretty': null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema['x-read-pretty']
                };
                const resultItem = {
                    type: 'item',
                    name: field.name,
                    title: (null == field ? void 0 : null === (_field_uiSchema1 = field.uiSchema) || void 0 === _field_uiSchema1 ? void 0 : _field_uiSchema1.title) || field.name,
                    Component: 'CollectionFieldInitializer',
                    remove: __WEBPACK_EXTERNAL_MODULE__utils_mjs__.removeGridFormItem,
                    schemaInitialize: (s)=>{
                        var _interfaceConfig_schemaInitialize;
                        null == interfaceConfig || null === (_interfaceConfig_schemaInitialize = interfaceConfig.schemaInitialize) || void 0 === _interfaceConfig_schemaInitialize || _interfaceConfig_schemaInitialize.call(interfaceConfig, s, {
                            field,
                            block,
                            readPretty,
                            action,
                            targetCollection
                        });
                    },
                    schema
                };
                return resultItem;
            })
        };
    });
    const res = [];
    res.push({
        type: 'itemGroup',
        title: '{{ t("Extend collections") }}',
        children
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerChildren, {
        children: res
    });
};
const AssociatedFields = ()=>{
    const associationFields = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useAssociatedFormItemInitializerFields)({
        readPretty: true,
        block: 'Form'
    });
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    if (0 === associationFields.length) return null;
    const schema = [
        {
            type: 'itemGroup',
            title: t('Display association fields'),
            children: associationFields
        }
    ];
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerChildren, {
        children: schema
    });
};
const FilterParentCollectionFields = ()=>{
    const inheritFields = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useFilterInheritsFormItemInitializerFields)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const res = [];
    if ((null == inheritFields ? void 0 : inheritFields.length) > 0) inheritFields.forEach((inherit)=>{
        Object.values(inherit)[0].length && res.push({
            divider: true,
            type: 'itemGroup',
            title: t("Parent collection fields") + '(' + compile("".concat(Object.keys(inherit)[0])) + ')',
            children: Object.values(inherit)[0]
        });
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerChildren, {
        children: res
    });
};
const FilterAssociatedFields = ()=>{
    const associationFields = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useFilterAssociatedFormItemInitializerFields)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const res = [
        {
            type: 'itemGroup',
            title: t('Display association fields'),
            children: associationFields
        }
    ];
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__application_index_mjs__.SchemaInitializerChildren, {
        children: res
    });
};
export { AssociatedFields, ExtendCollectionFields, FilterAssociatedFields, FilterParentCollectionFields, ParentCollectionFields };
