// src/hooks/useBreakpoint/useBreakpoint.tsx
import {
  useDebouncedValue,
  useIsClient,
  useWindowEvent
} from "@simplybusiness/mobius-hooks";
import { useCallback, useMemo, useState } from "react";
var DEFAULT_BREAKPOINTS = [
  { size: "xs", value: 320 },
  { size: "sm", value: 480 },
  { size: "md", value: 670 },
  { size: "lg", value: 960 },
  { size: "xl", value: 1200 },
  { size: "xxl", value: 1320 }
];
var PASSIVE = { passive: true };
var getBreakpoint = (breakpoints, windowWidth) => {
  const closest = breakpoints.findIndex(
    (breakpoint) => breakpoint.value >= windowWidth + 1
  );
  const smallest = breakpoints[0];
  const largest = breakpoints[breakpoints.length - 1];
  const match = breakpoints[closest - 1];
  if (closest === 0) return smallest;
  if (closest === -1) return largest;
  return match;
};
var useBreakpoint = (customBreakpoints) => {
  const isClientSide = useIsClient();
  const breakpoints = customBreakpoints || DEFAULT_BREAKPOINTS;
  const defaultBreakpoint = breakpoints[0];
  const [windowWidth, setWindowWidth] = useState(
    typeof globalThis?.window !== "undefined" ? window.innerWidth : defaultBreakpoint.value
  );
  useWindowEvent("resize", () => setWindowWidth(window.innerWidth), PASSIVE);
  const debouncedWidth = useDebouncedValue(windowWidth, 200);
  const currentBreakpoint = useMemo(
    () => !isClientSide || breakpoints.length === 0 ? defaultBreakpoint : getBreakpoint(breakpoints, debouncedWidth),
    [isClientSide, breakpoints, defaultBreakpoint, debouncedWidth]
  );
  const up = useCallback(
    (size) => {
      const sizeIndex = breakpoints.findIndex((item) => item.size === size);
      if (sizeIndex === -1) {
        return false;
      }
      const currentIndex = breakpoints.findIndex(
        (item) => item.size === currentBreakpoint.size
      );
      return currentIndex >= sizeIndex;
    },
    [currentBreakpoint, breakpoints]
  );
  const down = useCallback(
    (size) => {
      const sizeIndex = breakpoints.findIndex((item) => item.size === size);
      if (sizeIndex === -1) {
        return true;
      }
      const currentIndex = breakpoints.findIndex(
        (item) => item.size === currentBreakpoint.size
      );
      return currentIndex <= sizeIndex;
    },
    [currentBreakpoint, breakpoints]
  );
  return {
    breakpoint: currentBreakpoint,
    up,
    down
  };
};

export {
  DEFAULT_BREAKPOINTS,
  useBreakpoint
};
//# sourceMappingURL=chunk-7FQ7SXK4.js.map
