import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__floating_ui_react__ from "@floating-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__BlockToolbar_mjs__ from "./BlockToolbar.mjs";
const ToolbarContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
const ToolbarProvider = (param)=>{
    let { children, onVisibilityChange } = param;
    const [childStates, setChildStates] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const registerChild = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((id, isVisible)=>{
        setChildStates((prev)=>({
                ...prev,
                [id]: isVisible
            }));
        return ()=>{
            setChildStates((prev)=>{
                const newState = {
                    ...prev
                };
                delete newState[id];
                return newState;
            });
        };
    }, []);
    const isVisible = Object.values(childStates).some((visible)=>visible);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (onVisibilityChange) onVisibilityChange(!isVisible);
    }, [
        isVisible,
        onVisibilityChange
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ToolbarContext.Provider, {
        value: {
            registerChild
        },
        children: children
    });
};
const useToolbar = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(ToolbarContext);
const useBlockToolbar = ()=>{
    const [open, setOpen] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { designable } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const { refs, floatingStyles, context } = (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.useFloating)({
        placement: 'top',
        middleware: [
            (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.offset)(10),
            (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.shift)({
                crossAxis: true,
                padding: 10
            })
        ],
        open,
        onOpenChange: setOpen,
        whileElementsMounted: __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.autoUpdate
    });
    const { getReferenceProps, getFloatingProps } = (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.useInteractions)([
        (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.useHover)(context, {
            handleClose: (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.safePolygon)(),
            delay: {
                open: 0,
                close: 100
            }
        }),
        (0, __WEBPACK_EXTERNAL_MODULE__floating_ui_react__.useDismiss)(context)
    ]);
    const toolbar = open && designable && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockToolbar_mjs__["default"], {
        ref: refs.setFloating,
        style: {
            ...floatingStyles,
            zIndex: 9999
        },
        ...getFloatingProps()
    });
    return {
        open,
        ref: refs.setReference,
        props: getReferenceProps(),
        toolbar
    };
};
export { ToolbarContext, ToolbarProvider, useBlockToolbar, useToolbar };
