import { jsx } from "react/jsx-runtime";
import "react";
import { useForm } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaInitializerChildren } from "../../application/index.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/index.mjs";
import { useExtendCollections } from "../../data-source/index.mjs";
import { useActionContext, useCompile } from "../../schema-component/index.mjs";
import { removeGridFormItem, useAssociatedFormItemInitializerFields, useFilterAssociatedFormItemInitializerFields, useFilterInheritsFormItemInitializerFields, useInheritsFormItemInitializerFields } from "../utils.mjs";
const ParentCollectionFields = ()=>{
    const inheritFields = useInheritsFormItemInitializerFields();
    const { t } = useTranslation();
    const compile = 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__*/ jsx(SchemaInitializerChildren, {
        children: res
    });
};
const ExtendCollectionFields = (props)=>{
    const collections = useExtendCollections();
    const form = useForm();
    const compile = useCompile();
    const { getInterface, getCollection } = useCollectionManager_deprecated();
    const { fieldSchema } = 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 : 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.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.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['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['x-read-pretty']
                };
                const resultItem = {
                    type: 'item',
                    name: field.name,
                    title: (null == field ? void 0 : null == (_field_uiSchema1 = field.uiSchema) ? void 0 : _field_uiSchema1.title) || field.name,
                    Component: 'CollectionFieldInitializer',
                    remove: removeGridFormItem,
                    schemaInitialize: (s)=>{
                        var _interfaceConfig_schemaInitialize;
                        null == interfaceConfig || null == (_interfaceConfig_schemaInitialize = 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__*/ jsx(SchemaInitializerChildren, {
        children: res
    });
};
const AssociatedFields = ()=>{
    const associationFields = useAssociatedFormItemInitializerFields({
        readPretty: true,
        block: 'Form'
    });
    const { t } = useTranslation();
    if (0 === associationFields.length) return null;
    const schema = [
        {
            type: 'itemGroup',
            title: t('Display association fields'),
            children: associationFields
        }
    ];
    return /*#__PURE__*/ jsx(SchemaInitializerChildren, {
        children: schema
    });
};
const FilterParentCollectionFields = ()=>{
    const inheritFields = useFilterInheritsFormItemInitializerFields();
    const { t } = useTranslation();
    const compile = 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__*/ jsx(SchemaInitializerChildren, {
        children: res
    });
};
const FilterAssociatedFields = ()=>{
    const associationFields = useFilterAssociatedFormItemInitializerFields();
    const { t } = useTranslation();
    const res = [
        {
            type: 'itemGroup',
            title: t('Display association fields'),
            children: associationFields
        }
    ];
    return /*#__PURE__*/ jsx(SchemaInitializerChildren, {
        children: res
    });
};
export { AssociatedFields, ExtendCollectionFields, FilterAssociatedFields, FilterParentCollectionFields, ParentCollectionFields };
