import { jsx } from "react/jsx-runtime";
import { useState } from "react";
import { ToolOutlined } from "@ant-design/icons";
import { useLocalStorageState } from "ahooks";
import { Dropdown, FloatButton } from "antd";
import { useApp } from "../../application/index.mjs";
import { SchemaComponentOptions } from "../../schema-component/index.mjs";
import { PinnedPluginListProvider } from "../pinned-list/index.mjs";
import { ContextMenuContext, useContextMenu } from "./useContextMenu.mjs";
const STORAGE_KEYS = {
    SHOW_SCROLL_AREA: 'show-scroll-area',
    CONTEXT_MENU_ENABLED: 'context-menu-enabled'
};
const ContextMenuProvider = (param)=>{
    let { children } = param;
    const [enable, setEnable] = useLocalStorageState(STORAGE_KEYS.CONTEXT_MENU_ENABLED, {
        defaultValue: true
    });
    const [showScrollArea, setShowScrollArea] = useLocalStorageState(STORAGE_KEYS.SHOW_SCROLL_AREA, {
        defaultValue: false
    });
    const contextItems = useApp().pluginContextMenu.get();
    const [position, setPosition] = useState({
        x: 0,
        y: 0
    });
    const items = [];
    const contextItemsSorted = Object.values(contextItems).sort((a, b)=>(a.sort || 0) - (b.sort || 0));
    contextItemsSorted.forEach((item)=>{
        const { actionProps, title, icon } = item.useLoadMethod({
            enable,
            setEnable,
            showScrollArea,
            setShowScrollArea,
            position
        });
        items.push({
            label: title,
            key: title,
            icon: icon,
            onClick: actionProps.onClick
        });
    });
    return /*#__PURE__*/ jsx(ContextMenuContext.Provider, {
        value: {
            contextMenuEnabled: enable,
            setContextMenuEnable: setEnable,
            showScrollArea,
            setShowScrollArea,
            position
        },
        children: /*#__PURE__*/ jsx(Dropdown, {
            menu: {
                items
            },
            trigger: enable ? [
                'contextMenu'
            ] : [],
            children: /*#__PURE__*/ jsx("div", {
                onContextMenu: (e)=>{
                    setPosition({
                        x: e.clientX,
                        y: e.clientY
                    });
                },
                children: children
            })
        })
    });
};
const ContextMenuButton = ()=>{
    const { contextMenuEnabled, setContextMenuEnable } = useContextMenu();
    return /*#__PURE__*/ jsx(FloatButton, {
        type: contextMenuEnabled ? 'primary' : 'default',
        icon: /*#__PURE__*/ jsx(ToolOutlined, {}),
        onClick: ()=>setContextMenuEnable(!contextMenuEnabled)
    });
};
const ContextMenuButtonProvider = (props)=>/*#__PURE__*/ jsx(PinnedPluginListProvider, {
        items: {
            cm: {
                order: 90,
                component: 'ContextMenuButton',
                pin: true,
                isPublic: true,
                belongTo: 'hoverbutton'
            }
        },
        children: /*#__PURE__*/ jsx(SchemaComponentOptions, {
            components: {
                ContextMenuButton
            },
            children: props.children
        })
    });
export { ContextMenuButtonProvider, ContextMenuProvider };
