import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { ArrayTable } from "@tachybase/components";
import { uid } from "@tachybase/schema";
import { PlusOutlined } from "@ant-design/icons";
import { Button, Dropdown } from "antd";
import { cloneDeep } from "lodash";
import { useTranslation } from "react-i18next";
import { useRequest } from "../../api-client/index.mjs";
import { useCollectionRecordData } from "../../data-source/index.mjs";
import { RecordProvider } from "../../record-provider/index.mjs";
import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from "../../schema-component/index.mjs";
import { useCollectionManager_deprecated } from "../hooks/index.mjs";
import { useFieldInterfaceOptions } from "./interfaces.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);
    const initialValue = {
        name: "f_".concat(uid()),
        ...cloneDeep(schema.default),
        interface: schema.name
    };
    return {
        type: 'object',
        properties: {
            [uid()]: {
                type: 'void',
                'x-component': 'Action.Drawer',
                'x-decorator': 'Form',
                'x-decorator-props': {
                    useValues (options) {
                        return useRequest(()=>Promise.resolve({
                                data: initialValue
                            }), options);
                    }
                },
                title: '{{ t("Add 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.useCreateAction }}'
                                }
                            }
                        }
                    }
                }
            }
        }
    };
};
const useCreateSubField = ()=>{
    useActionContext();
    return {
        async run () {}
    };
};
const AddSubFieldAction = ()=>{
    const { getInterface } = useCollectionManager_deprecated();
    const [visible, setVisible] = useState(false);
    const [schema, setSchema] = useState({});
    const compile = useCompile();
    const options = useFieldInterfaceOptions();
    const { t } = useTranslation();
    const items = useMemo(()=>options.map((option)=>{
            const children = option.children.map((child)=>({
                    label: compile(child.title),
                    key: child.name
                }));
            return {
                label: compile(option.label),
                key: option.key,
                children
            };
        }), [
        options
    ]);
    const menu = useMemo(()=>({
            style: {
                maxHeight: '60vh',
                overflow: 'auto'
            },
            onClick: (info)=>{
                const schema = getSchema(getInterface(info.key));
                setSchema(schema);
                setVisible(true);
            },
            items
        }), [
        items
    ]);
    const recordData = useCollectionRecordData();
    return /*#__PURE__*/ jsxs(ActionContextProvider, {
        value: {
            visible,
            setVisible
        },
        children: [
            /*#__PURE__*/ jsx(Dropdown, {
                menu: menu,
                children: /*#__PURE__*/ jsx(Button, {
                    icon: /*#__PURE__*/ jsx(PlusOutlined, {}),
                    type: 'primary',
                    children: t('Add field')
                })
            }),
            /*#__PURE__*/ jsx(RecordProvider, {
                record: {},
                parent: recordData,
                children: /*#__PURE__*/ jsx(SchemaComponent, {
                    schema: schema,
                    components: {
                        ...__WEBPACK_EXTERNAL_MODULE__components_index_mjs_99c14d58__,
                        ArrayTable: ArrayTable
                    },
                    scope: {
                        createOnly: true,
                        useCreateSubField
                    }
                })
            })
        ]
    });
};
export { AddSubFieldAction };
