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_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_index_mjs__ from "../action/index.mjs";
const useLabelFields = (collectionName)=>{
    var _targetFields_filter_map, _targetFields_filter, _targetFields_filter1;
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    if (!collectionName) return [];
    const targetFields = getCollectionFields(collectionName);
    return null == targetFields ? void 0 : null === (_targetFields_filter1 = targetFields.filter) || void 0 === _targetFields_filter1 ? void 0 : null === (_targetFields_filter = _targetFields_filter1.call(targetFields, (field)=>!(null == field ? void 0 : field.target) && 'boolean' !== field.type)) || void 0 === _targetFields_filter ? void 0 : null === (_targetFields_filter_map = _targetFields_filter.map) || void 0 === _targetFields_filter_map ? void 0 : _targetFields_filter_map.call(_targetFields_filter, (field)=>{
        var _field_uiSchema;
        return {
            value: field.name,
            label: compile((null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name)
        };
    });
};
const TableColumnDesigner = (props)=>{
    var _collectionField_uiSchema, _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops;
    const { uiSchema, fieldSchema, collectionField } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const columnSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { dn } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const initialValue = {
        title: null == columnSchema ? void 0 : columnSchema.title
    };
    const options = useLabelFields(null == collectionField ? void 0 : collectionField.target);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.GeneralSchemaDesigner, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsPopupItem, {
                title: '编辑',
                schema: {
                    title: '编辑字段',
                    'x-component': 'Action.Modal',
                    'x-component-props': {
                        width: 520
                    },
                    'x-decorator': 'Form',
                    'x-decorator-props': {
                        initialValue
                    },
                    type: 'void',
                    properties: {
                        title: {
                            'x-decorator': 'FormItem',
                            'x-component': 'Input',
                            title: '字段标题',
                            'x-component-props': {},
                            description: "原字段标题：".concat(null == collectionField ? void 0 : null === (_collectionField_uiSchema = collectionField.uiSchema) || void 0 === _collectionField_uiSchema ? void 0 : _collectionField_uiSchema.title)
                        },
                        footer: {
                            type: 'void',
                            'x-component': 'Action.Modal.Footer',
                            properties: {
                                cancel: {
                                    type: 'void',
                                    title: '{{t("Cancel")}}',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        useAction () {
                                            const ctx = (0, __WEBPACK_EXTERNAL_MODULE__action_index_mjs__.useActionContext)();
                                            return {
                                                async run () {
                                                    ctx.setVisible(false);
                                                }
                                            };
                                        }
                                    }
                                },
                                submit: {
                                    type: 'void',
                                    title: 'Submit',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        type: 'primary',
                                        useAction () {
                                            const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
                                            const ctx = (0, __WEBPACK_EXTERNAL_MODULE__action_index_mjs__.useActionContext)();
                                            return {
                                                async run () {
                                                    const { title } = form.values;
                                                    if (title) {
                                                        field.title = title;
                                                        columnSchema.title = title;
                                                        dn.emit('patch', {
                                                            schema: {
                                                                'x-uid': columnSchema['x-uid'],
                                                                title: columnSchema.title
                                                            }
                                                        });
                                                    }
                                                    ctx.setVisible(false);
                                                    dn.refresh();
                                                }
                                            };
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }),
            (null == collectionField ? void 0 : collectionField.target) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsSelectItem, {
                title: '标题字段',
                options: options,
                value: null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_fieldNames = _fieldSchema_xcomponentprops['fieldNames']) || void 0 === _fieldSchema_xcomponentprops_fieldNames ? void 0 : _fieldSchema_xcomponentprops_fieldNames['label'],
                onChange: (label)=>{
                    var _fieldSchema_xcomponentprops;
                    const fieldNames = {
                        ...null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops['fieldNames'],
                        label
                    };
                    __WEBPACK_EXTERNAL_MODULE_lodash__["default"].set(fieldSchema, 'x-component-props.fieldNames', fieldNames);
                    field.query(".*.".concat(fieldSchema.name)).take((f)=>{
                        f.componentProps.fieldNames = fieldNames;
                    });
                    dn.emit('patch', {
                        schema: {
                            'x-uid': fieldSchema['x-uid'],
                            'x-component-props': {
                                fieldNames
                            }
                        }
                    });
                    dn.refresh();
                }
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
export { TableColumnDesigner };
