import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.mjs";
import { useFormBlockType } from "../../../../block-provider/index.mjs";
import { useFieldComponentName } from "../../../../common/useFieldComponentName.mjs";
import { useCollectionField } from "../../../../data-source/index.mjs";
import { useDesignable, useFieldModeOptions, useIsAddNewForm } from "../../../../schema-component/index.mjs";
import { isSubMode } from "../../../../schema-component/antd/association-field/util.mjs";
import { useIsFieldReadPretty, useIsFormReadPretty } from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
const allowMultiple = {
    name: 'allowMultiple',
    type: 'switch',
    useVisible () {
        const isFieldReadPretty = useIsFieldReadPretty();
        const collectionField = useCollectionField();
        return !isFieldReadPretty && [
            'hasMany',
            'belongsToMany'
        ].includes(null == collectionField ? void 0 : collectionField.type);
    },
    useComponentProps () {
        const { t } = useTranslation();
        const field = useField();
        const fieldSchema = useFieldSchema();
        const { dn, refresh } = useDesignable();
        return {
            title: t('Allow multiple'),
            checked: void 0 === (fieldSchema['x-component-props'] || {}).multiple ? true : fieldSchema['x-component-props'].multiple,
            onChange (value) {
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                field.componentProps = field.componentProps || {};
                fieldSchema['x-component-props'].multiple = value;
                field.componentProps.multiple = value;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                dn.emit('patch', {
                    schema
                });
                refresh();
            }
        };
    }
};
const fieldComponent = {
    name: 'fieldComponent',
    type: 'select',
    useComponentProps () {
        const { t } = useTranslation();
        const field = useField();
        const fieldSchema = useFieldSchema();
        const fieldModeOptions = useFieldModeOptions();
        const isAddNewForm = useIsAddNewForm();
        const fieldComponentName = useFieldComponentName();
        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 subformComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:Nester',
    items: [
        fieldComponent,
        allowMultiple,
        {
            name: 'allowDissociate',
            type: 'switch',
            useVisible () {
                const { type } = useFormBlockType();
                return !useIsFormReadPretty() && 'update' === type;
            },
            useComponentProps () {
                var _fieldSchema_xcomponentprops;
                const { t } = useTranslation();
                const field = useField();
                const fieldSchema = useFieldSchema();
                const { dn, refresh } = useDesignable();
                return {
                    title: t('Allow dissociate'),
                    checked: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.allowDissociate) !== false,
                    onChange (value) {
                        const schema = {
                            ['x-uid']: fieldSchema['x-uid']
                        };
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        field.componentProps = field.componentProps || {};
                        fieldSchema['x-component-props'].allowDissociate = value;
                        field.componentProps.allowDissociate = value;
                        schema['x-component-props'] = fieldSchema['x-component-props'];
                        dn.emit('patch', {
                            schema
                        });
                        refresh();
                    }
                };
            }
        }
    ]
});
export { subformComponentFieldSettings };
