import { jsx, jsxs } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema, useForm } from "@tachybase/schema";
import lodash from "lodash";
import { useCollectionManager_deprecated } from "../../../collection-manager/index.mjs";
import { GeneralSchemaDesigner, SchemaSettingsDivider, SchemaSettingsPopupItem, SchemaSettingsRemove, SchemaSettingsSelectItem } from "../../../schema-settings/index.mjs";
import { useCompile, useDesignable } from "../../hooks/index.mjs";
import { useActionContext } from "../action/index.mjs";
const useLabelFields = (collectionName)=>{
    var _targetFields_filter_map, _targetFields_filter, _targetFields_filter1;
    const compile = useCompile();
    const { getCollectionFields } = useCollectionManager_deprecated();
    if (!collectionName) return [];
    const targetFields = getCollectionFields(collectionName);
    return null == targetFields ? void 0 : null == (_targetFields_filter1 = targetFields.filter) ? void 0 : null == (_targetFields_filter = _targetFields_filter1.call(targetFields, (field)=>!(null == field ? void 0 : field.target) && 'boolean' !== field.type)) ? void 0 : null == (_targetFields_filter_map = _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.title) || field.name)
        };
    });
};
const TableColumnDesigner = (props)=>{
    var _collectionField_uiSchema, _fieldSchema_xcomponentprops_fieldNames, _fieldSchema_xcomponentprops;
    const { uiSchema, fieldSchema, collectionField } = props;
    const field = useField();
    const columnSchema = useFieldSchema();
    const { dn } = useDesignable();
    const initialValue = {
        title: null == columnSchema ? void 0 : columnSchema.title
    };
    const options = useLabelFields(null == collectionField ? void 0 : collectionField.target);
    return /*#__PURE__*/ jsxs(GeneralSchemaDesigner, {
        children: [
            /*#__PURE__*/ jsx(SchemaSettingsPopupItem, {
                title: "\u7F16\u8F91",
                schema: {
                    title: "\u7F16\u8F91\u5B57\u6BB5",
                    '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: "\u5B57\u6BB5\u6807\u9898",
                            'x-component-props': {},
                            description: "\u539F\u5B57\u6BB5\u6807\u9898\uFF1A".concat(null == collectionField ? void 0 : null == (_collectionField_uiSchema = 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 = useActionContext();
                                            return {
                                                async run () {
                                                    ctx.setVisible(false);
                                                }
                                            };
                                        }
                                    }
                                },
                                submit: {
                                    type: 'void',
                                    title: 'Submit',
                                    'x-component': 'Action',
                                    'x-component-props': {
                                        type: 'primary',
                                        useAction () {
                                            const form = useForm();
                                            const ctx = 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__*/ jsx(SchemaSettingsSelectItem, {
                title: "\u6807\u9898\u5B57\u6BB5",
                options: options,
                value: null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : null == (_fieldSchema_xcomponentprops_fieldNames = _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['fieldNames'],
                        label
                    };
                    lodash.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__*/ jsx(SchemaSettingsDivider, {}),
            /*#__PURE__*/ jsx(SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
export { TableColumnDesigner };
