import { jsx, jsxs } from "react/jsx-runtime";
import { useContext, useRef, useState } from "react";
import { observer, useFieldSchema } from "@tachybase/schema";
import { EditOutlined } from "@ant-design/icons";
import { createStyles } from "antd-style";
import { useTranslation } from "react-i18next";
import { ActionContext, ActionContextProvider } from "../action/context.mjs";
import { useGetAriaLabelOfPopover } from "../action/hooks/useGetAriaLabelOfPopover.mjs";
import "../action/hooks/useSetAriaLabelForPopover.mjs";
import { StablePopover } from "../popover/index.mjs";
import { useAssociationFieldContext } from "./hooks.mjs";
import { InternalNester } from "./InternalNester.mjs";
import { ReadPrettyInternalViewer } from "./InternalViewer.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      max-width: 95%;\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      position: fixed;\n      top: 0;\n      left: 0;\n      width: 100%;\n      height: 100%;\n      background-color: transparent;\n      z-index: 9999;\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        "\n      min-width: 600px;\n      max-width: 800px;\n      max-height: 440px;\n      overflow: auto;\n      .ant-card {\n        border: 0px;\n      }\n    "
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        pretty: css(_templateObject()),
        mask: css(_templateObject1()),
        container: css(_templateObject2())
    };
});
const InternaPopoverNester = observer((props)=>{
    var _options_uiSchema;
    const { styles } = useStyles();
    const { options } = useAssociationFieldContext();
    const [visible, setVisible] = useState(false);
    const { t } = useTranslation();
    const schema = useFieldSchema();
    schema['x-component-props'].enableLink = false;
    const ref = useRef();
    const nesterProps = {
        ...props,
        shouldMountElement: true
    };
    const content = /*#__PURE__*/ jsx("div", {
        ref: ref,
        className: styles.container,
        children: /*#__PURE__*/ jsx(InternalNester, {
            ...nesterProps
        })
    });
    const getContainer = ()=>ref.current;
    const ctx = useContext(ActionContext);
    const modalProps = {
        getContainer: getContainer
    };
    const { getAriaLabel } = useGetAriaLabelOfPopover();
    return /*#__PURE__*/ jsxs(ActionContextProvider, {
        value: {
            ...ctx,
            modalProps
        },
        children: [
            /*#__PURE__*/ jsx(StablePopover, {
                overlayStyle: {
                    padding: '0px'
                },
                content: content,
                trigger: "click",
                placement: "topLeft",
                open: visible,
                onOpenChange: (open)=>setVisible(open),
                title: t(null == options ? void 0 : null == (_options_uiSchema = options.uiSchema) ? void 0 : _options_uiSchema.rawTitle),
                children: /*#__PURE__*/ jsxs("span", {
                    style: {
                        cursor: 'pointer',
                        display: 'flex'
                    },
                    children: [
                        /*#__PURE__*/ jsx("div", {
                            className: styles.pretty,
                            children: /*#__PURE__*/ jsx(ReadPrettyInternalViewer, {
                                ...props
                            })
                        }),
                        /*#__PURE__*/ jsx(EditOutlined, {
                            style: {
                                display: 'inline-flex',
                                margin: '5px'
                            }
                        })
                    ]
                })
            }),
            visible && /*#__PURE__*/ jsx("div", {
                role: "button",
                "aria-label": getAriaLabel('mask'),
                onClick: ()=>setVisible(false),
                className: styles.mask
            })
        ]
    });
}, {
    displayName: 'InternaPopoverNester'
});
export { InternaPopoverNester };
