import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { createContext, useContext, useEffect, useState } from "react";
/**
 * @internal
 * The threshold for determining if the window is zoomed enough to handle it explicitly in components styles
 */
export const ZOOM_THRESHOLD = 1.2;
// Default context values
const defaultZoomState = {
    zoomLevel: 1,
    viewportWidth: typeof window === "undefined" ? 0 : window.innerWidth,
    viewportHeight: typeof window === "undefined" ? 0 : window.innerHeight,
    hasVisualViewport: typeof window !== "undefined" && "visualViewport" in window,
};
/**
 * @internal
 * Context for zoom level and viewport info
 */
export const ZoomContext = createContext(defaultZoomState);
/**
 * @internal
 * Provider component that monitors zoom level and viewport dimensions
 */
export function ZoomProvider({ children }) {
    const [zoomState, setZoomState] = useState(defaultZoomState);
    useEffect(() => {
        // Function to update zoom state
        const updateZoomState = () => {
            // Current zoom level
            const zoomLevel = window.devicePixelRatio || 1;
            // Get viewport dimensions
            let viewportWidth;
            let viewportHeight;
            if (window.visualViewport) {
                // If visual viewport API is supported, use it (more accurate for zoomed content)
                viewportWidth = typeof window === "undefined" ? 0 : window.visualViewport.width;
                viewportHeight = typeof window === "undefined" ? 0 : window.visualViewport.height;
            }
            else {
                // Fallback to standard viewport
                viewportWidth = typeof window === "undefined" ? 0 : window.innerWidth;
                viewportHeight = typeof window === "undefined" ? 0 : window.innerHeight;
            }
            setZoomState({
                zoomLevel,
                viewportWidth,
                viewportHeight,
                hasVisualViewport: !!window.visualViewport,
            });
        };
        // Initial update
        updateZoomState();
        // Add event listeners
        if (window.visualViewport) {
            // Modern API - more accurate for zoom changes
            window.visualViewport.addEventListener("resize", updateZoomState);
            window.visualViewport.addEventListener("scroll", updateZoomState);
        }
        else {
            // Fallback
            window.addEventListener("resize", updateZoomState);
        }
        // Cleanup
        return () => {
            if (window.visualViewport) {
                window.visualViewport.removeEventListener("resize", updateZoomState);
                window.visualViewport.removeEventListener("scroll", updateZoomState);
            }
            else {
                window.removeEventListener("resize", updateZoomState);
            }
        };
    }, []);
    return _jsx(ZoomContext.Provider, { value: zoomState, children: children });
}
/**
 * @internal
 * Hook to access zoom level and viewport info
 */
export const useZoom = () => {
    const context = useContext(ZoomContext);
    if (context === undefined) {
        throw new Error("useZoom must be used within a ZoomProvider");
    }
    return context;
};
/**
 * @internal
 * Helper hook info it window is zoomed
 * @param baseZoomLevel - The zoom level the component was designed for (default 1)
 * @returns true if window is zoomed, false otherwise
 */
export const useIsZoomed = (baseZoomLevel = 1) => {
    const { zoomLevel } = useZoom();
    return zoomLevel > baseZoomLevel;
};
