"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useFormulaTitleOptions: ()=>useFormulaTitleOptions,
    EditFormulaTitleField: ()=>EditFormulaTitleField,
    useFormulaTitleVisible: ()=>useFormulaTitleVisible
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../collection-manager/index.js");
const external_schema_component_index_js_namespaceObject = require("../schema-component/index.js");
const external_SchemaSettings_js_namespaceObject = require("./SchemaSettings.js");
function useFormulaTitleOptions() {
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { getCollectionJoinField, getCollectionFields } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { fieldSchema: tableColumnSchema } = (0, external_schema_component_index_js_namespaceObject.useColumnSchema)();
    const schema = (0, schema_namespaceObject.useFieldSchema)();
    const fieldSchema = tableColumnSchema || schema;
    const collectionManage = (0, index_js_namespaceObject.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 ? void 0 : _getCollectionFields.filter((subField)=>subField.uiSchema).map((subField)=>({
                            label: subField.uiSchema ? compile(subField.uiSchema.title) : '',
                            value: subField.name
                        }))) && void 0 !== _getCollectionFields_filter_map ? _getCollectionFields_filter_map : []
                });
            }
        }
    });
    return options;
}
function EditFormulaTitleField() {
    var _collections_find, _field_componentProps_fieldNames, _field_componentProps;
    const { getCollectionJoinField, collections } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { dn } = (0, external_schema_component_index_js_namespaceObject.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 ? 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 === _collectionField_uiSchema ? void 0 : null === (_collectionField_uiSchema_xcomponentprops = _collectionField_uiSchema['x-component-props']) || void 0 === _collectionField_uiSchema_xcomponentprops ? 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__*/ (0, jsx_runtime_namespaceObject.jsx)(external_SchemaSettings_js_namespaceObject.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 === _field_componentProps ? void 0 : null === (_field_componentProps_fieldNames = _field_componentProps.fieldNames) || void 0 === _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 = (0, schema_namespaceObject.useFieldSchema)();
    const options = useFormulaTitleOptions();
    return options.length > 0 && ((null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.mode) === 'Select' || 'CollectionField' === fieldSchema['x-component']) && (null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : null === (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops1.fieldNames) || void 0 === _fieldSchema_xcomponentprops_fieldNames ? void 0 : _fieldSchema_xcomponentprops_fieldNames.value) !== void 0;
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
