import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2022-2025 GoodData Corporation
import { createContext, useCallback, useContext, useRef, useState } from "react";
import { emptyDOMRect } from "../constants.js";
const initState = {
    resizeDirection: "none",
    resizeItemIdentifiers: [],
    heightLimitReached: "none",
    widthState: null,
    initialDashboardDimensions: emptyDOMRect,
    isItemNested: undefined,
};
const LayoutResizeStateContext = createContext({
    resizeDirection: "none",
    resizeItemIdentifiers: [],
    heightLimitReached: "none",
    widthState: null,
    initialDashboardDimensions: emptyDOMRect,
    resizeStart: () => { },
    resizeEnd: () => { },
    setScrollCorrection: () => { },
    getScrollCorrection: () => ({ x: 0, y: 0 }),
    setWidthState: () => { },
    toggleHeightLimitReached: () => { },
    isItemNested: undefined,
});
export function LayoutResizeStateProvider({ children }) {
    const scrollingCorrectionRef = useRef({ x: 0, y: 0 });
    const [resizeState, setResizeState] = useState(initState);
    const resizeStart = useCallback((direction, identifiers, isItemNested, getDashboardDimensions) => {
        setResizeState({
            heightLimitReached: "none",
            initialDashboardDimensions: getDashboardDimensions ? getDashboardDimensions() : emptyDOMRect,
            resizeDirection: direction,
            resizeItemIdentifiers: identifiers,
            widthState: null,
            isItemNested,
        });
    }, []);
    const toggleHeightLimitReached = useCallback((heightLimitReached) => {
        setResizeState((state) => ({
            ...state,
            heightLimitReached,
        }));
    }, []);
    const setWidthState = useCallback((widthState) => {
        setResizeState((state) => ({
            ...state,
            widthState,
        }));
    }, []);
    const setScrollCorrection = useCallback((scrollCorrection) => {
        scrollingCorrectionRef.current = scrollCorrection;
    }, []);
    const getScrollCorrection = useCallback(() => {
        return scrollingCorrectionRef.current;
    }, []);
    const resizeEnd = useCallback(() => {
        setResizeState(initState);
    }, []);
    return (_jsx(LayoutResizeStateContext.Provider, { value: {
            ...resizeState,
            resizeStart,
            resizeEnd,
            setScrollCorrection,
            getScrollCorrection,
            setWidthState,
            toggleHeightLimitReached,
        }, children: children }));
}
export function useResizeContext() {
    return useContext(LayoutResizeStateContext);
}
export function useResizeHandlers() {
    const context = useResizeContext();
    return {
        resizeStart: context.resizeStart,
        toggleHeightLimitReached: context.toggleHeightLimitReached,
        resizeEnd: context.resizeEnd,
        setWidthState: context.setWidthState,
        setScrollCorrection: context.setScrollCorrection,
        getScrollCorrection: context.getScrollCorrection,
    };
}
export function useResizeItemStatus(identifier) {
    const context = useResizeContext();
    return {
        isActive: context.resizeItemIdentifiers.includes(identifier),
        isResizingColumnOrRow: context.resizeDirection !== "none",
        heightLimitReached: context.heightLimitReached,
        widthLimitReached: context.widthState?.limitReached ?? "none",
        initialDashboardDimensions: context.initialDashboardDimensions,
    };
}
export function useResizeWidthItemStatus(identifier) {
    const context = useResizeContext();
    return {
        isWidthResizing: context.resizeDirection === "width",
        isActive: context.resizeItemIdentifiers.includes(identifier),
        widthState: context.widthState,
    };
}
export function useResizeWidthStatus() {
    const context = useResizeContext();
    return {
        isResizingWidth: context.resizeDirection === "width",
        isItemNested: !!context.isItemNested,
        ...context.widthState,
    };
}
//# sourceMappingURL=LayoutResizeContext.js.map