/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-d4047d7
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useCallback, useContext, useMemo, useRef, useState, } from 'react';
export const LayoutContext = createContext(null);
export function useLayoutContext() {
    return useContext(LayoutContext);
}
export function LayoutProvider({ children, hasTransition = false, }) {
    const registeredPanels = useRef(new Set());
    const overlayPanelCallbacks = useRef(new Set());
    const [panelSizes, setPanelSizes] = useState({
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
    });
    const [isDragging, setIsDragging] = useState(false);
    const [isReady, setIsReady] = useState(false);
    const [hasOverlayPanels, setHasOverlayPanels] = useState(false);
    const registerPanel = useCallback((side, size) => {
        if (registeredPanels.current.has(side)) {
            throw new Error(`Layout: Only one panel per side is allowed. ` +
                `A panel is already registered on the "${side}" side.`);
        }
        // Check for axis conflict
        const isHorizontal = side === 'left' || side === 'right';
        const conflictingSides = isHorizontal
            ? ['top', 'bottom']
            : ['left', 'right'];
        for (const conflictSide of conflictingSides) {
            if (registeredPanels.current.has(conflictSide)) {
                throw new Error(`Layout: Panels from different axes cannot be combined. ` +
                    `Cannot register "${side}" panel when "${conflictSide}" panel exists. ` +
                    `Use either horizontal (left/right) or vertical (top/bottom) panels.`);
            }
        }
        registeredPanels.current.add(side);
        setPanelSizes((prev) => {
            if (prev[side] === size)
                return prev;
            return { ...prev, [side]: size };
        });
    }, []);
    const unregisterPanel = useCallback((side) => {
        registeredPanels.current.delete(side);
        setPanelSizes((prev) => {
            if (prev[side] === 0)
                return prev;
            return { ...prev, [side]: 0 };
        });
    }, []);
    const updatePanelSize = useCallback((side, size) => {
        setPanelSizes((prev) => {
            // Only update if the size actually changed
            if (prev[side] === size)
                return prev;
            return { ...prev, [side]: size };
        });
    }, []);
    const setDragging = useCallback((dragging) => {
        setIsDragging(dragging);
    }, []);
    const markReady = useCallback(() => {
        setIsReady(true);
    }, []);
    // Register an overlay panel's dismiss callback
    const registerOverlayPanel = useCallback((dismiss) => {
        overlayPanelCallbacks.current.add(dismiss);
        setHasOverlayPanels(true);
        // Return unregister function
        return () => {
            overlayPanelCallbacks.current.delete(dismiss);
            setHasOverlayPanels(overlayPanelCallbacks.current.size > 0);
        };
    }, []);
    // Dismiss all overlay panels
    const dismissOverlayPanels = useCallback(() => {
        overlayPanelCallbacks.current.forEach((dismiss) => dismiss());
    }, []);
    const value = useMemo(() => ({
        registerPanel,
        unregisterPanel,
        updatePanelSize,
        setDragging,
        markReady,
        panelSizes,
        isDragging,
        isReady,
        hasTransition,
        registerOverlayPanel,
        dismissOverlayPanels,
        hasOverlayPanels,
    }), [
        registerPanel,
        unregisterPanel,
        updatePanelSize,
        setDragging,
        markReady,
        panelSizes,
        isDragging,
        isReady,
        hasTransition,
        registerOverlayPanel,
        dismissOverlayPanels,
        hasOverlayPanels,
    ]);
    return (_jsx(LayoutContext.Provider, { value: value, children: children }));
}
/**
 * Provider that resets the layout context for sub-components.
 * Used to prevent panels in nested Layouts from affecting parent layouts.
 */
export function LayoutContextReset({ children }) {
    return (_jsx(LayoutContext.Provider, { value: null, children: children }));
}
export const LayoutPanelContext = createContext(null);
export function useLayoutPanelContext() {
    return useContext(LayoutPanelContext);
}

        