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, Drawer } from "antd";
import classnames from "classnames";
import { useStyles } from "./Action.Drawer.style.mjs";
import { useActionContext } from "./hooks.mjs";
import "./hooks/useSetAriaLabelForDrawer.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',
        '30%'
    ],
    [
        'middle',
        '50%'
    ],
    [
        'large',
        '70%'
    ]
]);
const ActionDrawer = observer((props)=>{
    const { footerNodeName = 'Action.Drawer.Footer', ...others } = props;
    const { visible, setVisible, openSize = 'middle', drawerProps } = useActionContext();
    const schema = useFieldSchema();
    const field = useField();
    const { componentCls, hashId } = useStyles();
    const footerSchema = schema.reduceProperties((buf, s)=>{
        if (s['x-component'] === footerNodeName) return s;
        return buf;
    });
    return /*#__PURE__*/ jsx(Drawer, {
        width: openSizeWidthMap.get(openSize),
        extra: /*#__PURE__*/ jsxs(Fragment, {
            children: [
                /*#__PURE__*/ jsx(Amplifier, {}),
                /*#__PURE__*/ jsx(Button, {
                    type: "text",
                    icon: /*#__PURE__*/ jsx(CloseOutlined, {}),
                    className: css(_templateObject()),
                    onClick: ()=>setVisible(false, true)
                })
            ]
        }),
        title: field.title,
        ...others,
        ...drawerProps,
        rootStyle: {
            ...null == drawerProps ? void 0 : drawerProps.style,
            ...null == others ? void 0 : others.style
        },
        destroyOnClose: true,
        closable: false,
        open: visible,
        onClose: ()=>setVisible(false, true),
        rootClassName: classnames(componentCls, hashId, null == drawerProps ? void 0 : drawerProps.className, others.className, 'reset'),
        footer: footerSchema && /*#__PURE__*/ jsx("div", {
            className: 'footer',
            children: /*#__PURE__*/ jsx(RecursionField, {
                basePath: field.address,
                schema: schema,
                onlyRenderProperties: true,
                filterProperties: (s)=>s['x-component'] === footerNodeName
            })
        }),
        className: "".concat(props.className, " amplifier-block"),
        children: /*#__PURE__*/ jsx(RecursionField, {
            basePath: field.address,
            schema: schema,
            onlyRenderProperties: true,
            filterProperties: (s)=>s['x-component'] !== footerNodeName
        })
    });
}, {
    displayName: 'ActionDrawer'
});
ActionDrawer.Footer = observer(()=>{
    const field = useField();
    const schema = useFieldSchema();
    return /*#__PURE__*/ jsx(RecursionField, {
        basePath: field.address,
        schema: schema,
        onlyRenderProperties: true
    });
}, {
    displayName: 'ActionDrawer.Footer'
});
const Action_Drawer = ActionDrawer;
const Amplifier = ()=>{
    const [isAmplifier, setIsAmplifier] = useState(false);
    const [blockWidth, setBlockWidth] = useState('');
    return /*#__PURE__*/ jsx(Button, {
        type: "text",
        icon: isAmplifier ? /*#__PURE__*/ jsx(FullscreenExitOutlined, {}) : /*#__PURE__*/ jsx(FullscreenOutlined, {}),
        className: css(_templateObject1()),
        onClick: (e)=>{
            const element = e.target.closest('.ant-drawer-content-wrapper');
            if (isAmplifier) element.style.width = blockWidth;
            else {
                setBlockWidth(element.getBoundingClientRect().width + 'px');
                element.style.width = '100%';
            }
            setIsAmplifier(!isAmplifier);
        }
    });
};
export { ActionDrawer, Amplifier, Action_Drawer as default };
