import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaInitializerChildren } from "../../../../application/schema-initializer/components/SchemaInitializerChildren.mjs";
import { SchemaInitializer } from "../../../../application/schema-initializer/SchemaInitializer.mjs";
import { useCompile } from "../../../../schema-component/index.mjs";
import { useAssociatedTableColumnInitializerFields, useInheritsTableColumnInitializerFields, useTableColumnInitializerFields } from "../../../../schema-initializer/utils.mjs";
const ParentCollectionFields = ()=>{
    const inheritFields = useInheritsTableColumnInitializerFields();
    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].filter((v)=>{
                var _v_field;
                return !(null == v ? void 0 : null == (_v_field = v.field) ? void 0 : _v_field.isForeignKey);
            })
        });
    });
    return /*#__PURE__*/ jsx(SchemaInitializerChildren, {
        children: res
    });
};
const AssociatedFields = ()=>{
    const associatedFields = useAssociatedTableColumnInitializerFields();
    const fieldSchema = useFieldSchema();
    const { t } = useTranslation();
    const schema = useMemo(()=>[
            {
                type: 'itemGroup',
                title: t('Display association fields'),
                children: associatedFields
            }
        ], [
        associatedFields,
        t
    ]);
    if (!(null == associatedFields ? void 0 : associatedFields.length) || 'AssociationField.SubTable' === fieldSchema['x-component']) return null;
    return /*#__PURE__*/ jsx(SchemaInitializerChildren, {
        children: schema
    });
};
const tableColumnInitializers = new SchemaInitializer({
    name: 'table:configureColumns',
    insertPosition: 'beforeEnd',
    icon: 'SettingOutlined',
    title: '{{t("Configure columns")}}',
    wrap: (s, param)=>{
        let { isInSubTable } = param;
        if (s['x-action-column']) return s;
        return {
            type: 'void',
            'x-decorator': 'TableV2.Column.Decorator',
            'x-toolbar': 'TableColumnSchemaToolbar',
            'x-settings': 'fieldSettings:TableColumn',
            'x-component': 'TableV2.Column',
            properties: {
                [s.name]: {
                    ...s
                }
            }
        };
    },
    items: [
        {
            name: 'displayFields',
            type: 'itemGroup',
            title: '{{t("Display fields")}}',
            useChildren: useTableColumnInitializerFields
        },
        {
            name: 'parentCollectionFields',
            Component: ParentCollectionFields
        },
        {
            name: 'associationFields',
            Component: AssociatedFields
        },
        {
            name: 'divider',
            type: 'divider',
            useVisible () {
                const fieldSchema = useFieldSchema();
                return 'AssociationField.SubTable' !== fieldSchema['x-component'];
            }
        },
        {
            type: 'item',
            name: 'add',
            title: '{{t("Action column")}}',
            Component: 'TableActionColumnInitializer',
            useVisible () {
                const fieldSchema = useFieldSchema();
                return 'AssociationField.SubTable' !== fieldSchema['x-component'];
            }
        }
    ]
});
export { tableColumnInitializers };
