import { useField, useFieldSchema, useForm } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useFieldComponentName } from "../../../../common/useFieldComponentName.mjs";
import { useDesignable, useFieldModeOptions, useIsAddNewForm } from "../../../../schema-component/index.mjs";
import { isSubMode } from "../../../../schema-component/antd/association-field/util.mjs";
import { useIsAssociationField, useIsFieldReadPretty } from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
import { useColumnSchema } from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
import { useIsShowMultipleSwitch } from "../../../../schema-settings/hooks/useIsShowMultipleSwitch.mjs";
import { allowMultiple } from "../Select/selectComponentFieldSettings.mjs";
const fieldComponent = {
    name: 'fieldComponent',
    type: 'select',
    useComponentProps () {
        const { t } = useTranslation();
        const field = useField();
        const isAddNewForm = useIsAddNewForm();
        const fieldComponentName = useFieldComponentName();
        const { fieldSchema: tableColumnSchema, collectionField } = useColumnSchema();
        const schema = useFieldSchema();
        const fieldSchema = tableColumnSchema || schema;
        const fieldModeOptions = useFieldModeOptions({
            fieldSchema: tableColumnSchema,
            collectionField
        });
        const { dn } = useDesignable();
        return {
            title: t('Field component'),
            options: fieldModeOptions,
            value: fieldComponentName,
            onChange (mode) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['mode'] = mode;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps = field.componentProps || {};
                field.componentProps.mode = mode;
                if (isSubMode(fieldSchema) && isAddNewForm) {
                    schema.default = null;
                    fieldSchema.default = null;
                    field.setInitialValue(null);
                    field.setValue(null);
                }
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const fileManagerComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:FileManager',
    items: [
        fieldComponent,
        {
            name: 'quickUpload',
            type: 'switch',
            useComponentProps () {
                var _fieldSchema_xcomponentprops;
                const { t } = useTranslation();
                const field = useField();
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const schema = useFieldSchema();
                const fieldSchema = tableColumnSchema || schema;
                const { dn, refresh } = useDesignable();
                return {
                    title: t('Quick upload'),
                    checked: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.quickUpload) !== false,
                    onChange (value) {
                        const schema = {
                            ['x-uid']: fieldSchema['x-uid']
                        };
                        field.componentProps.quickUpload = value;
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props'].quickUpload = value;
                        schema['x-component-props'] = fieldSchema['x-component-props'];
                        dn.emit('patch', {
                            schema
                        });
                        refresh();
                    }
                };
            },
            useVisible () {
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const field = useField();
                const form = useForm();
                const isReadPretty = (null == tableColumnSchema ? void 0 : tableColumnSchema['x-read-pretty']) || field.readPretty || form.readPretty;
                return !isReadPretty;
            }
        },
        {
            name: 'selectFile',
            type: 'switch',
            useComponentProps () {
                var _fieldSchema_xcomponentprops;
                const { t } = useTranslation();
                const field = useField();
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const schema = useFieldSchema();
                const fieldSchema = tableColumnSchema || schema;
                const { dn, refresh } = useDesignable();
                return {
                    title: t('Select file'),
                    checked: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.selectFile) !== false,
                    onChange (value) {
                        const schema = {
                            ['x-uid']: fieldSchema['x-uid']
                        };
                        field.componentProps.selectFile = value;
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props'].selectFile = value;
                        schema['x-component-props'] = fieldSchema['x-component-props'];
                        dn.emit('patch', {
                            schema
                        });
                        refresh();
                    }
                };
            },
            useVisible () {
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                const field = useField();
                const form = useForm();
                const isReadPretty = (null == tableColumnSchema ? void 0 : tableColumnSchema['x-read-pretty']) || field.readPretty || form.readPretty;
                return !isReadPretty;
            }
        },
        {
            name: 'size',
            type: 'select',
            useVisible () {
                const readPretty = useIsFieldReadPretty();
                const { fieldSchema: tableColumnSchema } = useColumnSchema();
                return readPretty && !tableColumnSchema;
            },
            useComponentProps () {
                var _field_componentProps;
                const { t } = useTranslation();
                const field = useField();
                const fieldSchema = useFieldSchema();
                const { dn } = useDesignable();
                return {
                    title: t('Size'),
                    options: [
                        {
                            label: t('Large'),
                            value: 'large'
                        },
                        {
                            label: t('Default'),
                            value: 'default'
                        },
                        {
                            label: t('Small'),
                            value: 'small'
                        }
                    ],
                    value: (null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.size) || 'default',
                    onChange (size) {
                        const schema = {
                            ['x-uid']: fieldSchema['x-uid']
                        };
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props']['size'] = size;
                        schema['x-component-props'] = fieldSchema['x-component-props'];
                        field.componentProps = field.componentProps || {};
                        field.componentProps.size = size;
                        dn.emit('patch', {
                            schema
                        });
                        dn.refresh();
                    }
                };
            }
        },
        {
            ...allowMultiple,
            useVisible () {
                const isAssociationField = useIsAssociationField();
                const IsShowMultipleSwitch = useIsShowMultipleSwitch();
                return isAssociationField && IsShowMultipleSwitch();
            }
        }
    ]
});
export { fileManagerComponentFieldSettings };
