"use client";

import { useResponsiveValue } from "@/hooks/useViewportWidth";

/**
 * The sponsor carousel's breakpoints, and the hook that reports how many
 * items fit at the current width.
 */

export const responsive = {
  superLargeDesktop: { breakpoint: { max: 4000, min: 1200 }, items: 4 },
  desktop: { breakpoint: { max: 1200, min: 992 }, items: 3 },
  tablet: { breakpoint: { max: 992, min: 576 }, items: 3 },
  mobile: { breakpoint: { max: 576, min: 0 }, items: 2 },
};

/**
 * The breakpoint name, widest first, for `useResponsiveValue`.
 *
 * Mirrors the `min` of each `responsive` entry above, so the carousel's own
 * breakpoints and this hook cannot drift apart. The `0` row is both the mobile
 * band and the server snapshot — the same `"mobile"` the replaced `useState`
 * initialised to, so the first paint is unchanged.
 */
const BREAKPOINT_BANDS = [
  { minWidth: 1200, value: "superLargeDesktop" },
  { minWidth: 992, value: "desktop" },
  { minWidth: 576, value: "tablet" },
  { minWidth: 0, value: "mobile" },
] as const satisfies ReadonlyArray<{
  minWidth: number;
  value: keyof typeof responsive;
}>;

/**
 * How many sponsor tiles fit at the current width.
 *
 * Was a `useState` plus a mount-and-resize effect that called `setState` after
 * paint on every load. `useResponsiveValue` reads the width through
 * `useSyncExternalStore`, which is the API for a browser value that has no
 * server equivalent — see `hooks/useViewportWidth.ts`.
 */
export const useBreakpoint = () => {
  const breakpoint = useResponsiveValue<keyof typeof responsive>(
    BREAKPOINT_BANDS,
    "mobile"
  );
  return { breakpoint, items: responsive[breakpoint]?.items };
};
