import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useContext, useState } from "react";
import { observer, useField, useFieldSchema } from "@tachybase/schema";
import { EditOutlined } from "@ant-design/icons";
import { Button, Drawer } from "antd";
import { useTranslation } from "react-i18next";
import { useCollectionManager } from "../../../data-source/index.mjs";
import { FlagProvider } from "../../../flag-provider/index.mjs";
import { ActionContext, ActionContextProvider, OpenMode } from "../action/context.mjs";
import { SubFormProvider, useAssociationFieldContext } from "./hooks.mjs";
import { InternalSubTable } from "./InternalSubTable.mjs";
import { ReadPrettyInternalViewer } from "./InternalViewer.mjs";
const InternaDrawerSubTable = observer((props)=>{
    var _options_uiSchema;
    const { options } = useAssociationFieldContext();
    const [visible, setVisible] = useState(false);
    const { t } = useTranslation();
    const nesterProps = {
        ...props,
        shouldMountElement: true
    };
    const titleProps = {
        ...props,
        enableLink: true
    };
    const field = useField();
    const fieldSchema = useFieldSchema();
    const cm = useCollectionManager();
    const ctx = useContext(ActionContext);
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            /*#__PURE__*/ jsxs("span", {
                style: {
                    cursor: 'pointer',
                    display: 'flex'
                },
                onClick: ()=>{
                    setVisible(true);
                },
                children: [
                    /*#__PURE__*/ jsx("div", {
                        style: {
                            maxWidth: '95%'
                        },
                        children: /*#__PURE__*/ jsx(ReadPrettyInternalViewer, {
                            ...titleProps
                        })
                    }),
                    /*#__PURE__*/ jsx(EditOutlined, {
                        style: {
                            display: 'inline-flex',
                            marginLeft: '5px'
                        }
                    })
                ]
            }),
            /*#__PURE__*/ jsx(ActionContextProvider, {
                value: {
                    ...ctx,
                    visible,
                    setVisible,
                    openMode: OpenMode.DEFAULT
                },
                children: /*#__PURE__*/ jsx(Drawer, {
                    title: t(null == options ? void 0 : null == (_options_uiSchema = options.uiSchema) ? void 0 : _options_uiSchema.rawTitle),
                    open: visible,
                    onClose: ()=>{
                        setVisible(false);
                    },
                    style: {
                        backgroundColor: '#f3f3f3'
                    },
                    width: '70%',
                    destroyOnClose: true,
                    extra: /*#__PURE__*/ jsx(Button, {
                        type: "primary",
                        onClick: ()=>setVisible(false),
                        children: "\u786E\u8BA4"
                    }),
                    children: /*#__PURE__*/ jsx(FlagProvider, {
                        isInSubForm: true,
                        children: /*#__PURE__*/ jsx(SubFormProvider, {
                            value: {
                                value: field.value,
                                collection: cm.getCollection(fieldSchema['x-collection-field'])
                            },
                            children: /*#__PURE__*/ jsx(InternalSubTable, {
                                ...nesterProps
                            })
                        })
                    })
                })
            })
        ]
    });
}, {
    displayName: 'InternaDrawerSubTable'
});
export { InternaDrawerSubTable };
