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__application_schema_settings_SchemaSettings_mjs__ from "../../../../application/schema-settings/SchemaSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_CollectionProvider_mjs__ from "../../../../data-source/collection/CollectionProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_commonsSettingsItem_fieldComponent_mjs__ from "../../../../data-source/commonsSettingsItem/fieldComponent.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_hooks_mjs__ from "../../../../schema-component/antd/association-field/hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__ from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "./utils.mjs";
const tableColumnSettings = new __WEBPACK_EXTERNAL_MODULE__application_schema_settings_SchemaSettings_mjs__.SchemaSettings({
    name: 'fieldSettings:TableColumn',
    items: [
        {
            name: 'decoratorOptions',
            type: 'itemGroup',
            hideIfNoChildren: true,
            useComponentProps () {
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                return {
                    title: t('Generic properties')
                };
            },
            children: [
                {
                    name: 'customColumnTitle',
                    type: 'modal',
                    useComponentProps () {
                        var _collectionField_uiSchema;
                        const { fieldSchema, collectionField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                        const columnSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                        return {
                            title: t('Custom column title'),
                            schema: {
                                type: 'object',
                                title: t('Custom column title'),
                                properties: {
                                    title: {
                                        title: t('Column title'),
                                        default: null == columnSchema ? void 0 : columnSchema.title,
                                        description: "".concat(t('Original field title: ')).concat((null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _collectionField_uiSchema ? void 0 : _collectionField_uiSchema.title) || (null == fieldSchema ? void 0 : fieldSchema.title)),
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Input',
                                        'x-component-props': {}
                                    }
                                }
                            },
                            onSubmit: (param)=>{
                                let { title } = param;
                                if (title) {
                                    field.title = title;
                                    columnSchema.title = title;
                                    dn.emit('patch', {
                                        schema: {
                                            'x-uid': columnSchema['x-uid'],
                                            title: columnSchema.title
                                        }
                                    });
                                }
                                dn.refresh();
                            }
                        };
                    }
                },
                {
                    name: 'columnWidth',
                    type: 'modal',
                    useComponentProps () {
                        var _columnSchema_xcomponentprops;
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                        const columnSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                        return {
                            title: t('Column width'),
                            schema: {
                                type: 'object',
                                title: t('Column width'),
                                properties: {
                                    width: {
                                        default: (null == columnSchema ? void 0 : null === (_columnSchema_xcomponentprops = columnSchema['x-component-props']) || void 0 === _columnSchema_xcomponentprops ? void 0 : _columnSchema_xcomponentprops['width']) || 200,
                                        'x-decorator': 'FormItem',
                                        'x-component': 'InputNumber',
                                        'x-component-props': {}
                                    }
                                }
                            },
                            onSubmit: (param)=>{
                                let { width } = param;
                                const props = columnSchema['x-component-props'] || {};
                                props['width'] = width;
                                const schema = {
                                    ['x-uid']: columnSchema['x-uid']
                                };
                                schema['x-component-props'] = props;
                                columnSchema['x-component-props'] = props;
                                field.componentProps.width = width;
                                dn.emit('patch', {
                                    schema
                                });
                                dn.refresh();
                            }
                        };
                    }
                },
                {
                    name: 'sortable',
                    type: 'switch',
                    useVisible () {
                        const collection = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_CollectionProvider_mjs__.useCollection)();
                        const { collectionField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        const { getInterface } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
                        const interfaceCfg = getInterface(null == collectionField ? void 0 : collectionField.interface);
                        const { currentMode } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_association_field_hooks_mjs__.useAssociationFieldContext)();
                        return (null == interfaceCfg ? void 0 : interfaceCfg.sortable) === true && !currentMode && (null == collection ? void 0 : collection.name) === (null == collectionField ? void 0 : collectionField.collectionName);
                    },
                    useComponentProps () {
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                        const columnSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
                        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                        return {
                            title: t('Sortable'),
                            checked: field.componentProps.sorter,
                            onChange: (v)=>{
                                const schema = {
                                    ['x-uid']: columnSchema['x-uid']
                                };
                                columnSchema['x-component-props'] = {
                                    ...columnSchema['x-component-props'],
                                    sorter: v
                                };
                                schema['x-component-props'] = columnSchema['x-component-props'];
                                field.componentProps.sorter = v;
                                dn.emit('patch', {
                                    schema
                                });
                                dn.refresh();
                            }
                        };
                    }
                },
                {
                    name: 'setDefaultValue',
                    useVisible () {
                        const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        if (!fieldSchema) return false;
                        return !(null == fieldSchema ? void 0 : fieldSchema['x-read-pretty']);
                    },
                    Component: __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsDefaultValue,
                    useComponentProps () {
                        const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        return {
                            fieldSchema
                        };
                    }
                },
                {
                    name: 'required',
                    type: 'switch',
                    useVisible () {
                        const { uiSchema, fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        if (!fieldSchema) return false;
                        const isSubTableColumn = [
                            'QuickEdit',
                            'FormItem'
                        ].includes(fieldSchema['x-decorator']);
                        return isSubTableColumn && !field.readPretty && !(null == uiSchema ? void 0 : uiSchema['x-read-pretty']);
                    },
                    useComponentProps () {
                        const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                        return {
                            key: 'required',
                            title: t('Required'),
                            checked: fieldSchema.required,
                            onChange: (required)=>{
                                var _field_path, _field_path1;
                                if (!fieldSchema) return console.error('fieldSchema is required');
                                const schema = {
                                    ['x-uid']: fieldSchema['x-uid']
                                };
                                fieldSchema['required'] = required;
                                schema['required'] = required;
                                const path = null === (_field_path1 = field.path) || void 0 === _field_path1 ? void 0 : _field_path1.splice((null === (_field_path = field.path) || void 0 === _field_path ? void 0 : _field_path.length) - 1, 1);
                                field.form.query("".concat(path.concat("*." + fieldSchema.name))).forEach((f)=>{
                                    f.required = required;
                                });
                                dn.emit('patch', {
                                    schema
                                });
                                dn.refresh();
                            }
                        };
                    }
                },
                {
                    name: 'pattern',
                    type: 'select',
                    useVisible () {
                        const { fieldSchema, collectionField } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        if (!fieldSchema) return false;
                        const isSubTableColumn = [
                            'QuickEdit',
                            'FormItem'
                        ].includes(fieldSchema['x-decorator']);
                        return isSubTableColumn && !(null == field ? void 0 : field.readPretty) && (null == collectionField ? void 0 : collectionField.interface) !== 'o2m' && !(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.isPatternDisabled)(fieldSchema);
                    },
                    useComponentProps () {
                        const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_table_v2_Table_Column_Decorator_mjs__.useColumnSchema)();
                        const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
                        const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                        const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
                        let readOnlyMode = 'editable';
                        if (!fieldSchema) return console.error('fieldSchema is required');
                        if (true === fieldSchema['x-disabled']) readOnlyMode = 'readonly';
                        if (true === fieldSchema['x-read-pretty']) readOnlyMode = 'read-pretty';
                        return {
                            key: 'pattern',
                            title: t('Pattern'),
                            options: [
                                {
                                    label: t('Editable'),
                                    value: 'editable'
                                },
                                {
                                    label: t('Readonly'),
                                    value: 'readonly'
                                },
                                {
                                    label: t('Easy-reading'),
                                    value: 'read-pretty'
                                }
                            ],
                            value: readOnlyMode,
                            onChange: (v)=>{
                                var _field_path, _field_path1;
                                const schema = {
                                    ['x-uid']: fieldSchema['x-uid']
                                };
                                const path = null === (_field_path1 = field.path) || void 0 === _field_path1 ? void 0 : _field_path1.splice((null === (_field_path = field.path) || void 0 === _field_path ? void 0 : _field_path.length) - 1, 1);
                                switch(v){
                                    case 'readonly':
                                        fieldSchema['x-read-pretty'] = false;
                                        fieldSchema['x-disabled'] = true;
                                        schema['x-read-pretty'] = false;
                                        schema['x-disabled'] = true;
                                        field.form.query("".concat(path.concat("*." + fieldSchema.name))).forEach((f)=>{
                                            f.readonly = true;
                                            f.disabled = true;
                                        });
                                        break;
                                    case 'read-pretty':
                                        fieldSchema['x-read-pretty'] = true;
                                        fieldSchema['x-disabled'] = false;
                                        schema['x-read-pretty'] = true;
                                        schema['x-disabled'] = false;
                                        field.form.query("".concat(path.concat("*." + fieldSchema.name))).forEach((f)=>{
                                            f.readPretty = true;
                                        });
                                        break;
                                    default:
                                        fieldSchema['x-read-pretty'] = false;
                                        fieldSchema['x-disabled'] = false;
                                        schema['x-read-pretty'] = false;
                                        schema['x-disabled'] = false;
                                        field.form.query("".concat(path.concat("*." + fieldSchema.name))).forEach((f)=>{
                                            f.readPretty = false;
                                            f.disabled;
                                        });
                                        break;
                                }
                                dn.emit('patch', {
                                    schema
                                });
                                dn.refresh();
                            }
                        };
                    }
                },
                __WEBPACK_EXTERNAL_MODULE__data_source_commonsSettingsItem_fieldComponent_mjs__.fieldComponentSettingsItem
            ]
        },
        {
            name: 'componentOptions',
            type: 'itemGroup',
            hideIfNoChildren: true,
            useComponentProps () {
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                return {
                    title: t('Specific properties')
                };
            },
            useChildren () {
                const app = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
                const fieldComponentName = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useFieldComponentName)();
                const componentSettings = app.schemaSettingsManager.get("fieldSettings:component:".concat(fieldComponentName));
                return (null == componentSettings ? void 0 : componentSettings.items) || [];
            }
        },
        {
            name: 'divider',
            type: 'divider'
        },
        {
            name: 'delete',
            type: 'remove',
            sort: 100,
            useComponentProps () {
                const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
                return {
                    removeParentsIfNoChildren: true,
                    confirm: {
                        title: t('Delete field')
                    },
                    breakRemoveOn: {
                        'x-component': 'Grid'
                    }
                };
            }
        }
    ]
});
export { tableColumnSettings };
