import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { uid, useForm } from "@tachybase/schema";
import { ArrayTable } from "@tego/client";
import cloneDeep from "lodash/cloneDeep";
import { useTranslation } from "react-i18next";
import { useAPIClient, useRequest } from "../../api-client/index.mjs";
import { useRecord } from "../../record-provider/index.mjs";
import { ActionContextProvider, SchemaComponent } from "../../schema-component/index.mjs";
import { useUpdateAction } from "../action-hooks.mjs";
import { useCollectionManager_deprecated } from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs_99c14d58__ from "./components/index.mjs";
const getSchema = (schema)=>{
    if (!schema) return;
    const properties = cloneDeep(schema.properties);
    properties.name['x-disabled'] = true;
    return {
        type: 'object',
        properties: {
            [uid()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return useRequest(()=>Promise.resolve({
                                data: cloneDeep(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 = useForm();
    const { run } = useUpdateAction();
    const { refreshCM } = 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 : null == (_form_values_uiSchema = _form_values.uiSchema) ? void 0 : null == (_form_values_uiSchema_enum = _form_values_uiSchema.enum) ? void 0 : _form_values_uiSchema_enum.slice()) || [];
            form.setValuesIn('uiSchema.enum', options.map((option)=>({
                    value: uid(),
                    ...option
                })));
            await run();
            await refreshCM();
        }
    };
};
const EditSubFieldAction = (props)=>{
    const record = useRecord();
    const { getInterface } = useCollectionManager_deprecated();
    const [visible, setVisible] = useState(false);
    const [schema, setSchema] = useState({});
    useAPIClient();
    const { t } = useTranslation();
    return /*#__PURE__*/ jsxs(ActionContextProvider, {
        value: {
            visible,
            setVisible
        },
        children: [
            /*#__PURE__*/ jsx("a", {
                onClick: async ()=>{
                    const schema = getSchema({
                        ...getInterface(record.interface),
                        default: record
                    });
                    setSchema(schema);
                    setVisible(true);
                },
                children: t('Edit')
            }),
            /*#__PURE__*/ jsx(SchemaComponent, {
                schema: schema,
                components: {
                    ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs_99c14d58__,
                    ArrayTable: ArrayTable
                },
                scope: {
                    useUpdateCollectionField
                }
            })
        ]
    });
};
export { EditSubFieldAction };
