{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "credit-pack-picker",
  "title": "Credit Pack Picker",
  "description": "A credit top-up picker: packs that get cheaper per credit, a balance that counts up as you choose, what the pack is worth in images or minutes, and an auto top-up switch.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/credit-pack-picker/credit-pack-picker.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type CreditPackTheme = \"dark\" | \"light\";\n\nexport interface CreditPack {\n  credits: number;\n  price: number;\n  /** Short label on the pack, e.g. \"Most popular\". */\n  badge?: string;\n}\n\nexport interface CreditUse {\n  /** What one unit is called, plural, e.g. \"images\". */\n  label: string;\n  /** Credits one unit costs. */\n  cost: number;\n}\n\nexport interface CreditPackPickerProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: CreditPackTheme;\n  title?: string;\n  subtitle?: string;\n  packs?: CreditPack[];\n  /** Translates credits into things people can do with them. */\n  uses?: CreditUse[];\n  /** Credits already on the account. */\n  balance?: number;\n  /** Index of the pack selected first. */\n  defaultPack?: number;\n  unit?: string;\n  currency?: string;\n  onBuy?: (pack: CreditPack, autoTopUp: boolean) => void;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#0E0E0E\",\n    panel: \"#141416\",\n    border: \"rgba(255,255,255,0.09)\",\n    text: \"#F4F5F7\",\n    sub: \"rgba(255,255,255,0.55)\",\n    faint: \"rgba(255,255,255,0.36)\",\n    chip: \"rgba(255,255,255,0.06)\",\n    accent: \"#B69CFF\",\n    accentSoft: \"rgba(182,156,255,0.12)\",\n    accentText: \"#160B33\",\n    good: \"#87FFE3\",\n    btn: \"#F4F5F7\",\n    btnText: \"#0E0E0E\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    panel: \"#F6F6F4\",\n    border: \"rgba(0,0,0,0.09)\",\n    text: \"#111317\",\n    sub: \"rgba(17,19,23,0.58)\",\n    faint: \"rgba(17,19,23,0.4)\",\n    chip: \"rgba(17,19,23,0.05)\",\n    accent: \"#7B5CE0\",\n    accentSoft: \"rgba(123,92,224,0.08)\",\n    accentText: \"#FFFFFF\",\n    good: \"#0E9F80\",\n    btn: \"#111317\",\n    btnText: \"#FFFFFF\",\n  },\n};\n\nconst DEFAULT_PACKS: CreditPack[] = [\n  { credits: 500, price: 9 },\n  { credits: 2000, price: 29, badge: \"Most popular\" },\n  { credits: 5000, price: 59 },\n  { credits: 20000, price: 179, badge: \"Best value\" },\n];\nconst DEFAULT_USES: CreditUse[] = [\n  { label: \"images\", cost: 4 },\n  { label: \"minutes of voice\", cost: 10 },\n  { label: \"video clips\", cost: 80 },\n];\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nfunction useEased(target: number, reduce: boolean) {\n  const [value, setValue] = React.useState(target);\n  const current = React.useRef(target);\n  React.useEffect(() => {\n    let raf = 0;\n    const tick = () => {\n      const next = reduce\n        ? target\n        : current.current + (target - current.current) * 0.2;\n      const done = Math.abs(next - target) < 0.5;\n      current.current = done ? target : next;\n      setValue(current.current);\n      if (!done) raf = requestAnimationFrame(tick);\n    };\n    raf = requestAnimationFrame(tick);\n    return () => cancelAnimationFrame(raf);\n  }, [target, reduce]);\n  return value;\n}\n\nconst n = (v: number) => Math.round(v).toLocaleString(\"en-US\");\n\nexport function CreditPackPicker({\n  theme = \"dark\",\n  title = \"Top up credits\",\n  subtitle = \"Credits never expire. Bigger packs cost less per credit.\",\n  packs = DEFAULT_PACKS,\n  uses = DEFAULT_USES,\n  balance = 340,\n  defaultPack = 1,\n  unit = \"credits\",\n  currency = \"$\",\n  onBuy,\n  className,\n  style,\n  ...props\n}: CreditPackPickerProps) {\n  const pal = PALETTES[theme];\n  const reduce = useReducedMotion();\n  const groupRef = React.useRef<HTMLDivElement>(null);\n  const [index, setIndex] = React.useState(defaultPack);\n  const [auto, setAuto] = React.useState(false);\n  const pack = packs[index] ?? packs[0];\n  const baseRate = packs[0].price / packs[0].credits;\n  const rate = (p: CreditPack) => p.price / p.credits;\n  const after = balance + pack.credits;\n  const shown = useEased(after, reduce);\n  const savedPct = Math.round((1 - rate(pack) / baseRate) * 100);\n  const biggest = Math.max(...packs.map((p) => p.credits)) + balance;\n\n  const onKey = (e: React.KeyboardEvent) => {\n    const dir =\n      e.key === \"ArrowRight\" || e.key === \"ArrowDown\"\n        ? 1\n        : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\"\n          ? -1\n          : 0;\n    if (!dir) return;\n    e.preventDefault();\n    const next = (index + dir + packs.length) % packs.length;\n    setIndex(next);\n    groupRef.current\n      ?.querySelectorAll<HTMLElement>(\"[role=radio]\")\n      [next]?.focus();\n  };\n\n  return (\n    <div\n      className={cn(\n        \"@container w-full overflow-hidden rounded-[20px]\",\n        className,\n      )}\n      style={{\n        background: pal.card,\n        border: `1px solid ${pal.border}`,\n        color: pal.text,\n        fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\",\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"grid @[680px]:grid-cols-[1.3fr_1fr]\">\n        <div className=\"p-5 sm:p-7\">\n          <p className=\"m-0 text-[17px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          <p className=\"m-0 mt-1 text-[13px]\" style={{ color: pal.sub }}>\n            {subtitle}\n          </p>\n          <div\n            ref={groupRef}\n            role=\"radiogroup\"\n            aria-label=\"Credit pack\"\n            onKeyDown={onKey}\n            className=\"mt-5 grid grid-cols-2 gap-2.5\"\n          >\n            {packs.map((p, i) => {\n              const on = i === index;\n              const save = Math.round((1 - rate(p) / baseRate) * 100);\n              return (\n                <button\n                  key={p.credits}\n                  type=\"button\"\n                  role=\"radio\"\n                  aria-checked={on}\n                  tabIndex={on ? 0 : -1}\n                  onClick={() => setIndex(i)}\n                  className=\"relative cursor-pointer rounded-2xl p-4 pt-9 text-left transition-[background,box-shadow,transform] duration-200 motion-safe:active:scale-[0.98]\"\n                  style={{\n                    background: on ? pal.accentSoft : \"transparent\",\n                    boxShadow: `inset 0 0 0 ${on ? 1.5 : 1}px ${on ? pal.accent : pal.border}`,\n                  }}\n                >\n                  {p.badge && (\n                    <span\n                      className=\"absolute top-3 left-4 rounded-full px-2 py-0.5 text-[10.5px] font-semibold\"\n                      style={{\n                        background: on ? pal.accent : pal.chip,\n                        color: on ? pal.accentText : pal.sub,\n                      }}\n                    >\n                      {p.badge}\n                    </span>\n                  )}\n                  <span className=\"block text-[24px] leading-none font-semibold tracking-[-0.03em] tabular-nums\">\n                    {n(p.credits)}\n                  </span>\n                  <span\n                    className=\"mt-1 block text-[11.5px]\"\n                    style={{ color: pal.sub }}\n                  >\n                    {unit}\n                  </span>\n                  <span className=\"mt-3.5 flex items-baseline justify-between gap-2\">\n                    <span className=\"text-[15px] font-semibold tabular-nums\">\n                      {currency}\n                      {n(p.price)}\n                    </span>\n                    <span\n                      className=\"text-[11.5px] tabular-nums\"\n                      style={{ color: save > 0 ? pal.good : pal.faint }}\n                    >\n                      {currency}\n                      {(rate(p) * 100).toFixed(1)} / 100\n                      {save > 0 ? ` · −${save}%` : \"\"}\n                    </span>\n                  </span>\n                </button>\n              );\n            })}\n          </div>\n        </div>\n\n        <div\n          className=\"flex flex-col border-t p-5 sm:p-7 @[680px]:border-t-0 @[680px]:border-l\"\n          style={{ background: pal.panel, borderColor: pal.border }}\n        >\n          <p className=\"m-0 text-[12px] font-medium\" style={{ color: pal.sub }}>\n            Balance after top-up\n          </p>\n          <p\n            className=\"m-0 mt-1.5 text-[38px] leading-none font-semibold tracking-[-0.035em] tabular-nums\"\n            aria-live=\"polite\"\n          >\n            {n(shown)}\n          </p>\n          <div\n            className=\"mt-3 flex h-1.5 overflow-hidden rounded-full\"\n            style={{ background: pal.chip }}\n            aria-hidden=\"true\"\n          >\n            <span\n              className=\"h-full\"\n              style={{\n                width: `${(balance / biggest) * 100}%`,\n                background: pal.sub,\n              }}\n            />\n            <span\n              className=\"h-full motion-safe:transition-[width] motion-safe:duration-300\"\n              style={{\n                width: `${(pack.credits / biggest) * 100}%`,\n                background: pal.accent,\n              }}\n            />\n          </div>\n          <p\n            className=\"m-0 mt-1.5 text-[11.5px] tabular-nums\"\n            style={{ color: pal.faint }}\n          >\n            {n(balance)} now + {n(pack.credits)} new\n            {savedPct > 0 ? `, ${savedPct}% cheaper per credit` : \"\"}\n          </p>\n\n          {uses.length > 0 && (\n            <ul\n              className=\"m-0 mt-5 flex list-none flex-col gap-2 border-t p-0 pt-4 text-[13px]\"\n              style={{ borderColor: pal.border }}\n            >\n              <li className=\"text-[11.5px]\" style={{ color: pal.faint }}>\n                This pack gets you about\n              </li>\n              {uses.map((u) => (\n                <li\n                  key={u.label}\n                  className=\"flex items-baseline justify-between gap-3\"\n                >\n                  <span style={{ color: pal.sub }}>{u.label}</span>\n                  <b className=\"font-semibold tabular-nums\">\n                    {n(pack.credits / u.cost)}\n                  </b>\n                </li>\n              ))}\n            </ul>\n          )}\n\n          <button\n            type=\"button\"\n            role=\"switch\"\n            aria-checked={auto}\n            onClick={() => setAuto((a) => !a)}\n            className=\"mt-5 flex cursor-pointer items-center justify-between gap-3 text-left text-[12.5px]\"\n            style={{ color: pal.sub }}\n          >\n            Top up again when I drop below 100\n            <span\n              className=\"relative h-[18px] w-8 shrink-0 rounded-full transition-colors\"\n              style={{ background: auto ? pal.accent : pal.chip }}\n            >\n              <span\n                className=\"absolute top-0.5 left-0.5 size-3.5 rounded-full bg-white shadow transition-transform\"\n                style={{ transform: auto ? \"translateX(14px)\" : \"none\" }}\n              />\n            </span>\n          </button>\n          <button\n            type=\"button\"\n            onClick={() => onBuy?.(pack, auto)}\n            className=\"mt-4 w-full cursor-pointer rounded-xl py-2.5 text-[13.5px] font-semibold transition-opacity hover:opacity-90\"\n            style={{ background: pal.btn, color: pal.btnText }}\n          >\n            Buy {n(pack.credits)} {unit} for {currency}\n            {n(pack.price)}\n          </button>\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}