/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-f1f3d22 | Cube Dev Team */
import { useEvent } from "../../../_internal/hooks/use-event.js";
import { createContext, useContext, useMemo, useRef, useState } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/components/content/Layout/LayoutContext.tsx
const LayoutRefsContext = createContext(null);
function useLayoutRefsContext() {
	return useContext(LayoutRefsContext);
}
const LayoutActionsContext = createContext(null);
const LayoutStateContext = createContext(null);
function useLayoutActionsContext() {
	return useContext(LayoutActionsContext);
}
function useLayoutStateContext() {
	return useContext(LayoutStateContext);
}
function LayoutProvider({ children, hasTransition = false }) {
	const registeredPanels = useRef(/* @__PURE__ */ new Set());
	const overlayPanelCallbacks = useRef(/* @__PURE__ */ new Set());
	const panelContainerRef = useRef(null);
	const [isPanelContainerReady, setIsPanelContainerReady] = useState(false);
	const setPanelContainer = useEvent((element) => {
		panelContainerRef.current = element;
		setIsPanelContainerReady(element !== null);
	});
	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 updatePanelSize = useEvent((side, size) => {
		setPanelSizes((prev) => {
			if (prev[side] === size) return prev;
			return {
				...prev,
				[side]: size
			};
		});
	});
	const registerPanel = useEvent((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.`);
		const conflictingSides = side === "left" || side === "right" ? ["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);
		updatePanelSize(side, size);
	});
	const unregisterPanel = useEvent((side) => {
		registeredPanels.current.delete(side);
		updatePanelSize(side, 0);
	});
	const setDragging = useEvent((dragging) => {
		setIsDragging(dragging);
	});
	const markReady = useEvent(() => {
		setIsReady(true);
	});
	const registerOverlayPanel = useEvent((dismiss) => {
		overlayPanelCallbacks.current.add(dismiss);
		setHasOverlayPanels(true);
		return () => {
			overlayPanelCallbacks.current.delete(dismiss);
			setHasOverlayPanels(overlayPanelCallbacks.current.size > 0);
		};
	});
	const dismissOverlayPanels = useEvent(() => {
		overlayPanelCallbacks.current.forEach((dismiss) => dismiss());
	});
	const actionsValue = useMemo(() => ({
		registerPanel,
		unregisterPanel,
		updatePanelSize,
		setDragging,
		markReady,
		hasTransition,
		registerOverlayPanel,
		dismissOverlayPanels
	}), [hasTransition]);
	const stateValue = useMemo(() => ({
		panelSizes,
		isDragging,
		isReady,
		hasOverlayPanels
	}), [
		panelSizes,
		isDragging,
		isReady,
		hasOverlayPanels
	]);
	const refsValue = useMemo(() => ({
		panelContainerRef,
		isPanelContainerReady,
		setPanelContainer
	}), [isPanelContainerReady]);
	return /* @__PURE__ */ jsx(LayoutRefsContext.Provider, {
		value: refsValue,
		children: /* @__PURE__ */ jsx(LayoutActionsContext.Provider, {
			value: actionsValue,
			children: /* @__PURE__ */ jsx(LayoutStateContext.Provider, {
				value: stateValue,
				children
			})
		})
	});
}
/**
* Provider that resets the layout context for sub-components.
* Used to prevent panels in nested Layouts from affecting parent layouts.
*/
function LayoutContextReset({ children }) {
	return /* @__PURE__ */ jsx(LayoutRefsContext.Provider, {
		value: null,
		children: /* @__PURE__ */ jsx(LayoutActionsContext.Provider, {
			value: null,
			children: /* @__PURE__ */ jsx(LayoutStateContext.Provider, {
				value: null,
				children
			})
		})
	});
}
const LayoutPanelContext = createContext(null);
function useLayoutPanelContext() {
	return useContext(LayoutPanelContext);
}

//#endregion
export { LayoutActionsContext, LayoutContextReset, LayoutPanelContext, LayoutProvider, LayoutRefsContext, LayoutStateContext, useLayoutActionsContext, useLayoutPanelContext, useLayoutRefsContext, useLayoutStateContext };
//# sourceMappingURL=LayoutContext.js.map