import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__ from "../../../filter-provider/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaSettingsDataScope_mjs__ from "../../SchemaSettingsDataScope.mjs";
const Designer = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)(()=>{
    const { getCollectionFields, getCollectionField, getCollection, isTitleField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const { formSchema, data } = fieldSchema['x-designer-props'];
    const { form } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.useFormBlockContext)();
    const collectionName = field.componentProps.service.resource;
    const collection = getCollection(collectionName);
    const collectionFields = getCollectionFields(collectionName);
    if (!data) {
        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.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 {
            __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(data, "config.".concat(collectionName, ".filter"), (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)(filter));
        } catch (err) {
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.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 {
            __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(data, "config.".concat(collectionName, ".titleField"), titleField);
        } catch (err) {
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.GeneralSchemaDesigner, {
        draggable: false,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaSettingsDataScope_mjs__.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: __WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(filter) ? {} : (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)((0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.mergeFilter)([
                                    filter,
                                    getSelectedIdFilter(field.value)
                                ], '$or'))
                            };
                        });
                    } catch (err) {
                        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.error)(err);
                    }
                    formSchema['x-data-templates'] = data;
                    dn.emit('patch', {
                        schema: {
                            ['x-uid']: formSchema['x-uid'],
                            ['x-data-templates']: data
                        }
                    });
                    dn.refresh();
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaSettingsItem, {
        title: title,
        ...others,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            style: {
                alignItems: 'center',
                display: 'flex',
                justifyContent: 'space-between'
            },
            children: [
                title,
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Select, {
                    popupMatchSelectWidth: false,
                    bordered: false,
                    value: value,
                    onChange: onChange,
                    options: options,
                    style: {
                        textAlign: 'right',
                        minWidth: 100
                    },
                    ...others
                })
            ]
        })
    });
}
export { Designer, getSelectedIdFilter };
