import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaSettings } from "../../../../application/schema-settings/SchemaSettings.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 { useTitleFieldOptions } from "../../../../schema-component/antd/form-item/FormItem.Settings.mjs";
import { useColumnSchema } from "../../../../schema-component/antd/table-v2/Table.Column.Decorator.mjs";
import { useColorFields } from "../../../../schema-component/antd/table-v2/Table.Column.Designer.mjs";
const enableLink = {
    name: 'enableLink',
    type: 'switch',
    useVisible () {
        const field = useField();
        return field.readPretty;
    },
    useComponentProps () {
        var _fieldSchema_xcomponentprops;
        const { t } = useTranslation();
        const field = useField();
        const fieldSchema = useFieldSchema();
        const { dn } = useDesignable();
        return {
            title: t('Enable link'),
            checked: (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.enableLink) !== false,
            onChange (flag) {
                fieldSchema['x-component-props'] = {
                    ...null == fieldSchema ? void 0 : fieldSchema['x-component-props'],
                    enableLink: flag
                };
                field.componentProps['enableLink'] = flag;
                dn.emit('patch', {
                    schema: {
                        'x-uid': fieldSchema['x-uid'],
                        'x-component-props': {
                            ...null == fieldSchema ? void 0 : fieldSchema['x-component-props']
                        }
                    }
                });
                dn.refresh();
            }
        };
    }
};
const titleField = {
    name: 'titleField',
    type: 'select',
    useComponentProps () {
        var _field_componentProps_fieldNames, _field_componentProps;
        const { t } = useTranslation();
        const field = useField();
        const { fieldSchema: tableColumnSchema, collectionField: tableColumnField } = useColumnSchema();
        const schema = useFieldSchema();
        const fieldSchema = tableColumnSchema || schema;
        const options = useTitleFieldOptions();
        const targetCollectionField = useCollectionField();
        const collectionField = tableColumnField || targetCollectionField;
        const { dn } = useDesignable();
        return {
            title: t('Title field'),
            options,
            value: null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : null == (_field_componentProps_fieldNames = _field_componentProps.fieldNames) ? void 0 : _field_componentProps_fieldNames.label,
            onChange (label) {
                var _collectionField_uiSchema_xcomponentprops, _collectionField_uiSchema;
                const schema = {
                    ['x-uid']: fieldSchema['x-uid']
                };
                const fieldNames = {
                    ...null == collectionField ? void 0 : null == (_collectionField_uiSchema = collectionField.uiSchema) ? void 0 : null == (_collectionField_uiSchema_xcomponentprops = _collectionField_uiSchema['x-component-props']) ? void 0 : _collectionField_uiSchema_xcomponentprops['fieldNames'],
                    ...field.componentProps.fieldNames,
                    label
                };
                fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                fieldSchema['x-component-props']['fieldNames'] = fieldNames;
                schema['x-component-props'] = fieldSchema['x-component-props'];
                field.componentProps.fieldNames = fieldSchema['x-component-props'].fieldNames;
                dn.emit('patch', {
                    schema
                });
                dn.refresh();
            }
        };
    }
};
const fieldComponent = {
    name: 'fieldComponent',
    type: 'select',
    useComponentProps () {
        const { t } = useTranslation();
        const field = useField();
        const isAddNewForm = useIsAddNewForm();
        const { fieldSchema: tableColumnSchema, collectionField } = useColumnSchema();
        const fieldModeOptions = useFieldModeOptions({
            fieldSchema: tableColumnSchema,
            collectionField
        });
        const schema = useFieldSchema();
        const fieldSchema = tableColumnSchema || schema;
        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 tagComponentFieldSettings = new SchemaSettings({
    name: 'fieldSettings:component:Tag',
    items: [
        fieldComponent,
        {
            name: 'tagColorField',
            type: 'select',
            useComponentProps () {
                var _field_componentProps;
                const { t } = useTranslation();
                const field = useField();
                const schema = useFieldSchema();
                const targetCollectionField = useCollectionField();
                const { fieldSchema: tableColumnSchema, collectionField: tableColumnField } = useColumnSchema();
                const fieldSchema = tableColumnSchema || schema;
                const collectionField = tableColumnField || targetCollectionField;
                const { dn } = useDesignable();
                var _collectionField_target;
                const colorFieldOptions = useColorFields(null != (_collectionField_target = null == collectionField ? void 0 : collectionField.target) ? _collectionField_target : null == collectionField ? void 0 : collectionField.targetCollection);
                return {
                    title: t('Tag color field'),
                    options: colorFieldOptions,
                    value: null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.tagColorField,
                    onChange (tagColorField) {
                        const schema = {
                            ['x-uid']: fieldSchema['x-uid']
                        };
                        fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {};
                        fieldSchema['x-component-props']['tagColorField'] = tagColorField;
                        schema['x-component-props'] = fieldSchema['x-component-props'];
                        field.componentProps.tagColorField = tagColorField;
                        dn.emit('patch', {
                            schema
                        });
                        dn.refresh();
                    }
                };
            }
        },
        titleField,
        enableLink
    ]
});
export { tagComponentFieldSettings };
