"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    Designer: ()=>Designer,
    getSelectedIdFilter: ()=>getSelectedIdFilter
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../../block-provider/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const utils_js_namespaceObject = require("../../../filter-provider/utils.js");
const external_schema_component_index_js_namespaceObject = require("../../../schema-component/index.js");
const external_SchemaSettingsDataScope_js_namespaceObject = require("../../SchemaSettingsDataScope.js");
const Designer = (0, schema_namespaceObject.observer)(()=>{
    const { getCollectionFields, getCollectionField, getCollection, isTitleField } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_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)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { formSchema, data } = fieldSchema['x-designer-props'];
    const { form } = (0, index_js_namespaceObject.useFormBlockContext)();
    const collectionName = field.componentProps.service.resource;
    const collection = getCollection(collectionName);
    const collectionFields = getCollectionFields(collectionName);
    if (!data) {
        (0, client_namespaceObject.error)('data is required');
        return null;
    }
    const getFilter = ()=>{
        var _data_config_collectionName, _data_config;
        return (null === (_data_config = data.config) || void 0 === _data_config ? void 0 : null === (_data_config_collectionName = _data_config[collectionName]) || void 0 === _data_config_collectionName ? void 0 : _data_config_collectionName.filter) || {};
    };
    const setFilter = (filter)=>{
        try {
            external_lodash_default().set(data, "config.".concat(collectionName, ".filter"), (0, external_schema_component_index_js_namespaceObject.removeNullCondition)(filter));
        } catch (err) {
            (0, client_namespaceObject.error)(err);
        }
    };
    const getTitleFIeld = ()=>{
        var _data_config_collectionName, _data_config;
        return (null === (_data_config = data.config) || void 0 === _data_config ? void 0 : null === (_data_config_collectionName = _data_config[collectionName]) || void 0 === _data_config_collectionName ? void 0 : _data_config_collectionName.titleField) || (null == collection ? void 0 : collection.titleField) || 'id';
    };
    const setTitleField = (titleField)=>{
        try {
            external_lodash_default().set(data, "config.".concat(collectionName, ".titleField"), titleField);
        } catch (err) {
            (0, client_namespaceObject.error)(err);
        }
    };
    const options = collectionFields.filter((field)=>isTitleField(field)).map((field)=>{
        var _field_uiSchema;
        return {
            value: null == field ? void 0 : field.name,
            label: compile(null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || (null == field ? void 0 : field.name)
        };
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_index_js_namespaceObject.GeneralSchemaDesigner, {
        draggable: false,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_SchemaSettingsDataScope_js_namespaceObject.SchemaSettingsDataScope, {
                collectionName: collectionName,
                defaultFilter: getFilter(),
                form: form,
                onSubmit: (param)=>{
                    let { filter } = param;
                    setFilter(filter);
                    try {
                        field.query('fieldReaction.items.*.layout.dataId').forEach((item)=>{
                            if (item.componentProps.service.resource !== collectionName) return;
                            item.componentProps.service.params = {
                                filter: external_lodash_default().isEmpty(filter) ? {} : (0, external_schema_component_index_js_namespaceObject.removeNullCondition)((0, utils_js_namespaceObject.mergeFilter)([
                                    filter,
                                    getSelectedIdFilter(field.value)
                                ], '$or'))
                            };
                        });
                    } catch (err) {
                        (0, client_namespaceObject.error)(err);
                    }
                    formSchema['x-data-templates'] = data;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: formSchema['x-uid'],
                            ['x-data-templates']: data
                        }
                    });
                    dn.refresh();
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SelectItem, {
                title: t('Title field'),
                options: options,
                value: getTitleFIeld(),
                onChange: (label)=>{
                    setTitleField(label);
                    try {
                        field.query('fieldReaction.items.*.layout.dataId').forEach((item)=>{
                            if (item.componentProps.service.resource !== collectionName) return;
                            item.componentProps.fieldNames.label = label;
                            item.componentProps.targetField = getCollectionField("".concat(collectionName, ".").concat(label || (null == collection ? void 0 : collection.titleField) || 'id'));
                        });
                    } catch (err) {
                        console.error(err);
                    }
                    formSchema['x-data-templates'] = data;
                    const schema = {
                        ['x-uid']: formSchema['x-uid'],
                        ['x-data-templates']: data
                    };
                    dn.emit('patch', {
                        schema
                    });
                    dn.refresh();
                }
            }, "title-field")
        ]
    });
}, {
    displayName: 'Designer'
});
function getSelectedIdFilter(selectedId) {
    return selectedId ? {
        id: {
            $eq: selectedId
        }
    } : null;
}
function SelectItem(props) {
    const { title, options, value, onChange, ...others } = props;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.SchemaSettingsItem, {
        title: title,
        ...others,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            style: {
                alignItems: 'center',
                display: 'flex',
                justifyContent: 'space-between'
            },
            children: [
                title,
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Select, {
                    popupMatchSelectWidth: false,
                    bordered: false,
                    value: value,
                    onChange: onChange,
                    options: options,
                    style: {
                        textAlign: 'right',
                        minWidth: 100
                    },
                    ...others
                })
            ]
        })
    });
}
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
});
