import { jsx } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../collection-manager/index.mjs";
import { useColumnSchema, useCompile, useDesignable } from "../schema-component/index.mjs";
import { SchemaSettingsModalItem } from "./SchemaSettings.mjs";
function useFormulaTitleOptions() {
    const compile = useCompile();
    const { getCollectionJoinField, getCollectionFields } = useCollectionManager_deprecated();
    const { getField } = useCollection_deprecated();
    const { fieldSchema: tableColumnSchema } = useColumnSchema();
    const schema = useFieldSchema();
    const fieldSchema = tableColumnSchema || schema;
    const collectionManage = useCollectionManager_deprecated();
    const collectionManageField = collectionManage.collections.filter((value)=>value.name === fieldSchema['x-decorator-props'])[0];
    const collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    let fields = [];
    if (collectionField) fields = getCollectionFields(collectionField ? collectionField.target ? collectionField.target : collectionField.collectionName : fieldSchema['name']);
    else if (collectionManageField) fields = collectionManageField['fields'];
    const options = [];
    null == fields || fields.forEach((field)=>{
        if (![
            'm2m',
            'o2m'
        ].includes(field.interface)) {
            if (field.uiSchema) {
                var _getCollectionFields;
                var _getCollectionFields_filter_map;
                options.push({
                    label: compile(field.uiSchema.title),
                    value: field.name,
                    children: null != (_getCollectionFields_filter_map = null == (_getCollectionFields = getCollectionFields(field.target)) ? void 0 : _getCollectionFields.filter((subField)=>subField.uiSchema).map((subField)=>({
                            label: subField.uiSchema ? compile(subField.uiSchema.title) : '',
                            value: subField.name
                        }))) ? _getCollectionFields_filter_map : []
                });
            }
        }
    });
    return options;
}
function EditFormulaTitleField() {
    var _collections_find, _field_componentProps_fieldNames, _field_componentProps;
    const { getCollectionJoinField, collections } = useCollectionManager_deprecated();
    const { getField } = useCollection_deprecated();
    const field = useField();
    const fieldSchema = useFieldSchema();
    const { t } = useTranslation();
    const { dn } = useDesignable();
    let collectionField = getField(fieldSchema['name']) || getCollectionJoinField(fieldSchema['x-collection-field']);
    collectionField = collectionField ? collectionField : null == (_collections_find = collections.find((value)=>value.name === fieldSchema['x-decorator-props'])) ? void 0 : _collections_find.fields;
    const options = useFormulaTitleOptions();
    const editTitle = async (formula)=>{
        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,
            formula
        };
        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();
    };
    return /*#__PURE__*/ jsx(SchemaSettingsModalItem, {
        title: t('Custom option label'),
        schema: {
            type: 'object',
            title: t('Custom option label'),
            properties: {
                formula: {
                    required: true,
                    'x-decorator': 'FormItem',
                    'x-component': 'Variable.TextArea',
                    'x-component-props': {
                        scope: options
                    },
                    default: (null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : null == (_field_componentProps_fieldNames = _field_componentProps.fieldNames) ? void 0 : _field_componentProps_fieldNames.formula) || ''
                }
            }
        },
        onSubmit: (param)=>{
            let { formula } = param;
            if (formula) editTitle(formula);
        }
    }, "edit-field-title");
}
const useFormulaTitleVisible = ()=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops1;
    const fieldSchema = useFieldSchema();
    const options = useFormulaTitleOptions();
    return options.length > 0 && ((null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.mode) === 'Select' || 'CollectionField' === fieldSchema['x-component']) && (null == (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) ? void 0 : null == (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops1.fieldNames) ? void 0 : _fieldSchema_xcomponentprops_fieldNames.value) !== void 0;
};
export { EditFormulaTitleField, useFormulaTitleOptions, useFormulaTitleVisible };
