import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__ from "lodash/cloneDeep";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__ from "../action-hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "./components/index.mjs";
const getSchema = (schema)=>{
    if (!schema) return;
    const properties = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(schema.properties);
    properties.name['x-disabled'] = true;
    return {
        type: 'object',
        properties: {
            [(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>Promise.resolve({
                                data: (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(schema.default)
                            }), options);
                    }
                },
                title: '{{ t("Edit field") }}',
                properties: {
                    ...properties,
                    footer: {
                        type: 'void',
                        'x-component': 'Action.Drawer.Footer',
                        properties: {
                            action1: {
                                title: '{{ t("Cancel") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    useAction: '{{ cm.useCancelAction }}'
                                }
                            },
                            action2: {
                                title: '{{ t("Submit") }}',
                                'x-component': 'Action',
                                'x-component-props': {
                                    type: 'primary',
                                    useAction: '{{ ds.useUpdateAction }}'
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useUpdateCollectionField = ()=>{
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const { run } = (0, __WEBPACK_EXTERNAL_MODULE__action_hooks_mjs__.useUpdateAction)();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    return {
        async run () {
            var _form_values_uiSchema_enum, _form_values_uiSchema, _form_values;
            await form.submit();
            const options = (null == form ? void 0 : null === (_form_values = form.values) || void 0 === _form_values ? void 0 : null === (_form_values_uiSchema = _form_values.uiSchema) || void 0 === _form_values_uiSchema ? void 0 : null === (_form_values_uiSchema_enum = _form_values_uiSchema.enum) || void 0 === _form_values_uiSchema_enum ? void 0 : _form_values_uiSchema_enum.slice()) || [];
            form.setValuesIn('uiSchema.enum', options.map((option)=>({
                    value: (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)(),
                    ...option
                })));
            await run();
            await refreshCM();
        }
    };
};
const EditSubFieldAction = (props)=>{
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { getInterface } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [schema, setSchema] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.ActionContextProvider, {
        value: {
            visible,
            setVisible
        },
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                onClick: async ()=>{
                    const schema = getSchema({
                        ...getInterface(record.interface),
                        default: record
                    });
                    setSchema(schema);
                    setVisible(true);
                },
                children: t('Edit')
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
                schema: schema,
                components: {
                    ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs__,
                    ArrayTable: __WEBPACK_EXTERNAL_MODULE__tachybase_components__.ArrayTable
                },
                scope: {
                    useUpdateCollectionField
                }
            })
        ]
    });
};
export { EditSubFieldAction };
