"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    FilterParentCollectionFields: ()=>FilterParentCollectionFields,
    ExtendCollectionFields: ()=>ExtendCollectionFields,
    ParentCollectionFields: ()=>ParentCollectionFields,
    FilterAssociatedFields: ()=>FilterAssociatedFields,
    AssociatedFields: ()=>AssociatedFields
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../application/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../collection-manager/index.js");
const external_data_source_index_js_namespaceObject = require("../../data-source/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_utils_js_namespaceObject = require("../utils.js");
const ParentCollectionFields = ()=>{
    const inheritFields = (0, external_utils_js_namespaceObject.useInheritsFormItemInitializerFields)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const compile = (0, external_schema_component_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaInitializerChildren, {
        children: res
    });
};
const ExtendCollectionFields = (props)=>{
    const collections = (0, external_data_source_index_js_namespaceObject.useExtendCollections)();
    const form = (0, schema_namespaceObject.useForm)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { getInterface, getCollection } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const { fieldSchema } = (0, external_schema_component_index_js_namespaceObject.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: external_utils_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaInitializerChildren, {
        children: res
    });
};
const AssociatedFields = ()=>{
    const associationFields = (0, external_utils_js_namespaceObject.useAssociatedFormItemInitializerFields)({
        readPretty: true,
        block: 'Form'
    });
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    if (0 === associationFields.length) return null;
    const schema = [
        {
            type: 'itemGroup',
            title: t('Display association fields'),
            children: associationFields
        }
    ];
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaInitializerChildren, {
        children: schema
    });
};
const FilterParentCollectionFields = ()=>{
    const inheritFields = (0, external_utils_js_namespaceObject.useFilterInheritsFormItemInitializerFields)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const compile = (0, external_schema_component_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaInitializerChildren, {
        children: res
    });
};
const FilterAssociatedFields = ()=>{
    const associationFields = (0, external_utils_js_namespaceObject.useFilterAssociatedFormItemInitializerFields)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const res = [
        {
            type: 'itemGroup',
            title: t('Display association fields'),
            children: associationFields
        }
    ];
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaInitializerChildren, {
        children: res
    });
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
