import { jsx } from "react/jsx-runtime";
import react, { useMemo } from "react";
import { uid } from "@tachybase/schema";
import icons, { RightOutlined } from "@ant-design/icons";
import { Menu, theme } from "antd";
import { createStyles } from "antd-style";
import { useCompile } from "../../../schema-component/index.mjs";
import { useSchemaInitializerItem } from "../context/index.mjs";
import { useSchemaInitializerMenuItems } from "../hooks/index.mjs";
import { useSchemaInitializerStyles } from "./style.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      box-shadow: none !important;\n      border-inline-end: 0 !important;\n      .ant-menu-sub {\n        max-height: 50vh !important;\n      }\n      .ant-menu-item {\n        margin-block: 0;\n      }\n      .ant-menu-root {\n        margin: 0 -",
        "px;\n        .ant-menu-submenu-title,\n        .ant-menu-item-only-child {\n          margin-inline: 0;\n          margin-block: 0;\n          width: 100%;\n        }\n      }\n      // associated issue https://github.com/ant-design/ant-design/issues/51361\n      .ant-menu-title-content {\n        display: inline;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css, token } = param;
    return {
        menu: css(_templateObject(), token.margin)
    };
});
const SchemaInitializerSubMenuContext = /*#__PURE__*/ react.createContext({});
const SchemaInitializerMenuProvider = (props)=>/*#__PURE__*/ jsx(SchemaInitializerSubMenuContext.Provider, {
        value: {
            isInMenu: true
        },
        children: props.children
    });
const useSchemaInitializerSubMenuContext = ()=>react.useContext(SchemaInitializerSubMenuContext);
const SchemaInitializerMenu = (props)=>{
    const { componentCls, hashId } = useSchemaInitializerStyles();
    const { items, ...others } = props;
    const { token } = theme.useToken();
    const { styles } = useStyles();
    const itemsWithPopupClass = useMemo(()=>items.map((item)=>({
                ...item,
                popupClassName: "".concat(hashId, " ").concat(componentCls, "-menu-sub")
            })), [
        componentCls,
        hashId,
        items
    ]);
    return /*#__PURE__*/ jsx(SchemaInitializerMenuProvider, {
        children: /*#__PURE__*/ jsx(Menu, {
            selectable: false,
            expandIcon: /*#__PURE__*/ jsx(RightOutlined, {
                style: {
                    fontSize: token.fontSizeSM,
                    color: token.colorTextDescription
                }
            }),
            rootClassName: styles.menu,
            items: itemsWithPopupClass,
            ...others
        })
    });
};
const SchemaInitializerSubMenu = (props)=>{
    const { children, title, name = uid(), onOpenChange, icon, ...others } = props;
    const compile = useCompile();
    const validChildren = null == children ? void 0 : children.filter((item)=>item.useVisible ? item.useVisible() : true);
    const childrenItems = useSchemaInitializerMenuItems(validChildren, name);
    const items = useMemo(()=>[
            {
                ...others,
                key: name,
                label: compile(title),
                icon: 'string' == typeof icon ? /*#__PURE__*/ jsx(icons, {
                    type: icon
                }) : icon,
                children: childrenItems
            }
        ], [
        childrenItems,
        compile,
        icon,
        name,
        others,
        title
    ]);
    return /*#__PURE__*/ jsx(SchemaInitializerMenu, {
        onOpenChange: onOpenChange,
        items: items
    });
};
const SchemaInitializerSubMenuInternal = ()=>{
    const itemConfig = useSchemaInitializerItem();
    return /*#__PURE__*/ jsx(SchemaInitializerSubMenu, {
        ...itemConfig
    });
};
export { SchemaInitializerMenu, SchemaInitializerSubMenu, SchemaInitializerSubMenuInternal, useSchemaInitializerSubMenuContext };
