import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { RecursionField, observer, useField, useFieldSchema } from "@tachybase/schema";
import { CloseOutlined, FullscreenExitOutlined, FullscreenOutlined } from "@ant-design/icons";
import { css } from "@emotion/css";
import { Button, Modal } from "antd";
import classnames from "classnames";
import { useActionContext } from "./index.mjs";
import { useStyles } from "./Action.Modal.style.mjs";
import "./hooks/useSetAriaLabelForModal.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                  background: none;\n                  border: none;\n                "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n        background: none;\n        border: none;\n      "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
const openSizeWidthMap = new Map([
    [
        'small',
        '40%'
    ],
    [
        'middle',
        '60%'
    ],
    [
        'large',
        '80%'
    ]
]);
const ActionModal = observer((props)=>{
    const { footerNodeName = 'Action.Modal.Footer', width, ...others } = props;
    const { styles } = useStyles();
    const { visible, setVisible, openSize = 'middle', modalProps } = useActionContext();
    const actualWidth = null != width ? width : openSizeWidthMap.get(openSize);
    const schema = useFieldSchema();
    const field = useField();
    const footerSchema = schema.reduceProperties((buf, s)=>{
        if (s['x-component'] === footerNodeName) return s;
        return buf;
    });
    return /*#__PURE__*/ jsx(Modal, {
        className: classnames(others.className, null == modalProps ? void 0 : modalProps.className, styles.container, 'amplifier-block'),
        style: {
            ...null == modalProps ? void 0 : modalProps.style,
            ...null == others ? void 0 : others.style
        },
        width: actualWidth,
        title: /*#__PURE__*/ jsxs("div", {
            style: {
                display: 'flex'
            },
            children: [
                /*#__PURE__*/ jsx("span", {
                    style: {
                        flex: 1
                    },
                    children: field.title
                }),
                /*#__PURE__*/ jsxs(Fragment, {
                    children: [
                        /*#__PURE__*/ jsx(Amplifier, {}),
                        /*#__PURE__*/ jsx(Button, {
                            type: "text",
                            icon: /*#__PURE__*/ jsx(CloseOutlined, {}),
                            className: css(_templateObject()),
                            onClick: ()=>setVisible(false, true)
                        })
                    ]
                })
            ]
        }),
        ...others,
        ...modalProps,
        destroyOnClose: true,
        open: visible,
        closable: false,
        footer: footerSchema ? /*#__PURE__*/ jsx(RecursionField, {
            basePath: field.address,
            schema: schema,
            onlyRenderProperties: true,
            filterProperties: (s)=>s['x-component'] === footerNodeName
        }) : false,
        children: /*#__PURE__*/ jsx("div", {
            className: styles.modalClassName,
            children: /*#__PURE__*/ jsx(RecursionField, {
                basePath: field.address,
                schema: schema,
                onlyRenderProperties: true,
                filterProperties: (s)=>s['x-component'] !== footerNodeName
            })
        })
    });
}, {
    displayName: 'ActionModal'
});
ActionModal.Footer = observer(()=>{
    const field = useField();
    const schema = useFieldSchema();
    return /*#__PURE__*/ jsx(RecursionField, {
        basePath: field.address,
        schema: schema,
        onlyRenderProperties: true
    });
}, {
    displayName: 'ActionModal.Footer'
});
const Action_Modal = ActionModal;
const Amplifier = ()=>{
    const [isAmplifier, setIsAmplifier] = useState(false);
    const [blockWidth, setBlockWidth] = useState('');
    return /*#__PURE__*/ jsx(Button, {
        icon: isAmplifier ? /*#__PURE__*/ jsx(FullscreenExitOutlined, {}) : /*#__PURE__*/ jsx(FullscreenOutlined, {}),
        type: "text",
        className: css(_templateObject1()),
        onClick: (e)=>{
            const element = e.target.closest('.ant-modal-content');
            const modal = document.querySelector('.ant-modal');
            const mask = document.querySelector('.ant-modal-mask');
            if (isAmplifier) {
                modal.style.width = blockWidth;
                element.style.width = '';
                modal.style.top = '';
                modal.style.padding = '';
                modal.style.margin = '';
                mask.style.backgroundColor = '';
            } else {
                setBlockWidth(modal.getBoundingClientRect().width + 'px');
                element.style.width = '100vw';
                modal.style.width = '100%';
                modal.style.top = '0';
                modal.style.padding = '0';
                modal.style.margin = '0';
                mask.style.backgroundColor = '#f3f3f3';
            }
            setIsAmplifier(!isAmplifier);
        }
    });
};
export { ActionModal, Amplifier, Action_Modal as default };
