@reblu/site-next 1.20.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @reblu/site-next
2
2
 
3
+ ## 1.21.0
4
+
5
+ ### Minor Changes
6
+
7
+ - fff0bf5: REB-981: `useCartValidation()` / `validateCart()` in `@reblu/site-next/cart` validate the persisted cart against live product, stock and price data and apply the result to the store — unavailable lines (`not_found`, `inactive`, `out_of_stock`) are removed and reported with their persisted name, `insufficient_stock` lines are clamped to the stock left, and changed prices are refreshed and reported. Per-line errors are exposed through `getLineError(productId, variantId)`. Waits for `CartProvider`'s hydration, runs once on mount (no loops), and a failed request leaves the cart untouched. Replaces the README's `errors[0].message` guidance.
8
+
9
+ ## 1.20.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 146c45a: ESM build imports `next/link.js`, `next/navigation.js` and `next/script.js`, so plain Node ESM (a SITE's vitest) resolves them; the `server.deps.inline` workaround can be removed (REB-929). `CtaButtons` renders an element `icon` as a direct, `aria-hidden` child of the button again, so shadcn's `has-[>svg]` icon padding applies (REB-930).
14
+ - Updated dependencies [146c45a]
15
+ - @reblu/site-ui@0.7.1
16
+
3
17
  ## 1.20.0
4
18
 
5
19
  ### Minor Changes
package/README.md CHANGED
@@ -45,6 +45,15 @@ to preserve the `'use client'` directive — R4), so importing anything from
45
45
  `@reblu/site-next/auth` pulls in `AuthLogo`'s `next-themes` import at module
46
46
  resolution time, even if your code never renders `<AuthLogo/>`.
47
47
 
48
+ ### Testing a SITE with plain Node ESM (vitest)
49
+
50
+ Since 1.20.1 the ESM build imports `next/link.js`, `next/navigation.js` and
51
+ `next/script.js` (Next ships no `exports` map, so plain Node ESM needs the
52
+ extension). A SITE's vitest config no longer needs
53
+ `test.server.deps.inline: [/@reblu\/site-next/]` to render SDK components; remove
54
+ that workaround when you upgrade. The same fix ships in `@reblu/site-ui` for
55
+ `./next` and `./pagination`.
56
+
48
57
  ## ISR caching
49
58
 
50
59
  Wire the adapter once when you build the SDK client — this restores the exact ISR
@@ -105,7 +114,7 @@ import { CalendarIcon } from 'lucide-react'
105
114
  | `className` | — | Wrapper class (layout of the button group) |
106
115
  | `buttonClassName` | — | Class on every button |
107
116
  | `size` | `'lg'` | `@reblu/site-ui` Button size |
108
- | `icon` | — | Leading icon on every CTA, wrapped in `aria-hidden` |
117
+ | `icon` | — | Leading icon on every CTA, `aria-hidden`; an element icon is a direct child, so `has-[>svg]` padding applies |
109
118
  | `primaryVariant` | `'default'` | `@reblu/site-ui` Button variant of the first CTA |
110
119
  | `secondaryVariant` | `'default'` | Button variant of every other CTA |
111
120
  | `primaryClassName` | — | Extra class on the first CTA, after `buttonClassName` |
@@ -359,6 +368,7 @@ its own Client Component.
359
368
  | Export | Subpath | Needs `'use client'`? |
360
369
  | ------ | ------- | ---------------------- |
361
370
  | `CartProvider`, `useCart()`, `useCartStore`, `CartItem` | `./cart` | **Yes** |
371
+ | `useCartValidation(opts?)`, `validateCart(opts?)` | `./cart` | **Yes** |
362
372
  | `createShopCheckoutRoute(opts?)` | `./shop-route-handlers` | No |
363
373
  | `createShopValidateCartRoute()` | `./shop-route-handlers` | No |
364
374
  | `createShopApplyDiscountRoute()` | `./shop-route-handlers` | No |
@@ -533,6 +543,72 @@ Existing callers that ignore the return value keep working unchanged.
533
543
  Use this to drive an `AddToCartButton`/`AddedToCartToast` pair's copy — see
534
544
  `@reblu/site-blocks`'s README "Add-to-cart feedback" for the full recipe.
535
545
 
546
+ ### Validating the persisted cart — `useCartValidation` (REB-981)
547
+
548
+ A cart persisted in `localStorage` outlives the catalog it was filled from:
549
+ between visits a product can be deleted, deactivated, sold out or repriced.
550
+ Never read only `errors[0].message` from validate-cart — it does not say
551
+ which line failed, and for a deleted product the API can only name it
552
+ «Producto desconocido». Use `useCartValidation()` instead, on the cart page
553
+ AND the checkout page. It runs once when the cart has hydrated (never
554
+ against the empty pre-hydration cart, never again on later cart edits) and
555
+ applies the answer of your `createShopValidateCartRoute()` handler to the
556
+ store:
557
+
558
+ | Validation error | What happens to the line |
559
+ | ---------------- | ------------------------ |
560
+ | `not_found`, `inactive`, `out_of_stock` (`suggestedAction: 'remove'`) | removed; reported in `removed` with its **persisted** `name`/`variantName` |
561
+ | `insufficient_stock` (`suggestedAction: 'update_quantity'`) | quantity clamped to `newQuantity`; reported in `quantityChanges` |
562
+ | live `unitPrice` differs from the persisted one | price refreshed; reported in `priceChanges` (`from`/`to` in cents) |
563
+ | an error matching no line (e.g. `empty_cart`) | nothing; reported in `unresolvedErrors` |
564
+
565
+ ```tsx
566
+ 'use client'
567
+ import { useCart, useCartValidation } from '@reblu/site-next/cart'
568
+ import { CartAdjustmentsNotice, CartLineList } from '@reblu/site-blocks'
569
+
570
+ const { items, updateQuantity, removeItem } = useCart()
571
+ const validation = useCartValidation()
572
+
573
+ <CartAdjustmentsNotice
574
+ removed={validation.removed}
575
+ quantityChanges={validation.quantityChanges}
576
+ priceChanges={validation.priceChanges}
577
+ />
578
+ <CartLineList
579
+ items={items}
580
+ onQuantityChange={updateQuantity}
581
+ onRemove={removeItem}
582
+ lineMessage={(item) => validation.getLineError(item.productId, item.variantId)?.message}
583
+ />
584
+ ```
585
+
586
+ Before creating the checkout session, call `revalidate()` and only continue
587
+ on `'valid'`:
588
+
589
+ ```tsx
590
+ const outcome = await validation.revalidate()
591
+ if (outcome.status !== 'valid') return // the notice now shows what changed
592
+ // …POST /api/shop/checkout
593
+ ```
594
+
595
+ | `status` | Meaning |
596
+ | -------- | ------- |
597
+ | `'idle'` / `'validating'` | hook only: not run yet / request in flight |
598
+ | `'valid'` | every line is buyable as it stands; nothing changed |
599
+ | `'adjusted'` | the cart was changed — show the notice and let the visitor confirm again |
600
+ | `'invalid'` | problems remain that were not applied (see `unresolvedErrors`) |
601
+ | `'empty'` | no lines; nothing was sent |
602
+ | `'failed'` | the request failed (`error` holds the reason); **the cart was left untouched** |
603
+
604
+ Options (hook and function): `endpoint` (default `'/api/shop/validate-cart'`),
605
+ `validate` (replace the default `fetch`-based validator), and
606
+ `removeUnavailable: false` to keep unavailable lines and only flag them
607
+ through `getLineError` / `lineErrors` (status `'invalid'`). The hook also
608
+ takes `auto: false` to skip the mount run. `validateCart(opts?)` is the same
609
+ operation outside React; it waits for `CartProvider`'s hydration and never
610
+ throws. Running it again on a clean cart changes nothing.
611
+
536
612
  ### `useShopOrderStatus` — the order-confirmation flow (REB-837)
537
613
 
538
614
  ```tsx
package/dist/analytics.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/analytics.tsx
2
- import Script from "next/script";
2
+ import Script from "next/script.js";
3
3
 
4
4
  // src/json-ld.tsx
5
5
  import { jsx } from "react/jsx-runtime";
package/dist/auth.js CHANGED
@@ -726,7 +726,7 @@ function ResetPasswordRequest(props) {
726
726
 
727
727
  // src/auth/reset-password-confirm.tsx
728
728
  import * as React5 from "react";
729
- import { useRouter, useSearchParams } from "next/navigation";
729
+ import { useRouter, useSearchParams } from "next/navigation.js";
730
730
  import { Button as Button4, Input as Input3, cn as cn5 } from "@reblu/site-ui";
731
731
  import { Label as Label3 } from "@reblu/site-ui/label";
732
732
  import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
@@ -909,7 +909,7 @@ function ResetPasswordConfirm(props) {
909
909
 
910
910
  // src/auth/set-password.tsx
911
911
  import * as React6 from "react";
912
- import { useSearchParams as useSearchParams2 } from "next/navigation";
912
+ import { useSearchParams as useSearchParams2 } from "next/navigation.js";
913
913
  import { Button as Button5, Input as Input4, cn as cn6 } from "@reblu/site-ui";
914
914
  import { Label as Label4 } from "@reblu/site-ui/label";
915
915
  import { jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
@@ -1042,7 +1042,7 @@ function SetPassword(props) {
1042
1042
 
1043
1043
  // src/auth/auth-confirm.tsx
1044
1044
  import * as React7 from "react";
1045
- import { useSearchParams as useSearchParams3 } from "next/navigation";
1045
+ import { useSearchParams as useSearchParams3 } from "next/navigation.js";
1046
1046
  import { cn as cn7 } from "@reblu/site-ui";
1047
1047
  import { jsx as jsx7, jsxs as jsxs6 } from "react/jsx-runtime";
1048
1048
  var DEFAULT_COPY8 = {
package/dist/cart.cjs CHANGED
@@ -24,10 +24,15 @@ __export(cart_exports, {
24
24
  CART_STORAGE_KEY: () => CART_STORAGE_KEY,
25
25
  CART_STORE_PERSIST_VERSION: () => CART_STORE_PERSIST_VERSION,
26
26
  CartProvider: () => CartProvider,
27
+ DEFAULT_VALIDATE_CART_ENDPOINT: () => DEFAULT_VALIDATE_CART_ENDPOINT,
27
28
  __resetCartStoreForTests: () => __resetCartStoreForTests,
29
+ applyCartValidation: () => applyCartValidation,
30
+ cartLineKey: () => cartLineKey,
28
31
  useCart: () => useCart,
29
32
  useCartHydrated: () => useCartHydrated,
30
- useCartStore: () => useCartStore
33
+ useCartStore: () => useCartStore,
34
+ useCartValidation: () => useCartValidation,
35
+ validateCart: () => validateCart
31
36
  });
32
37
  module.exports = __toCommonJS(cart_exports);
33
38
 
@@ -167,14 +172,227 @@ function useCart() {
167
172
  const itemCount = items.reduce((sum, item) => sum + item.quantity, 0);
168
173
  return { items, total, itemCount, hydrated, addItem, removeItem, updateQuantity, clearCart };
169
174
  }
175
+
176
+ // src/cart/validate.ts
177
+ var DEFAULT_VALIDATE_CART_ENDPOINT = "/api/shop/validate-cart";
178
+ function cartLineKey(productId, variantId) {
179
+ return JSON.stringify([productId, variantId ?? null]);
180
+ }
181
+ function emptyOutcome(status) {
182
+ return {
183
+ status,
184
+ removed: [],
185
+ quantityChanges: [],
186
+ priceChanges: [],
187
+ lineErrors: {},
188
+ unresolvedErrors: []
189
+ };
190
+ }
191
+ function isValidation(value) {
192
+ if (!value || typeof value !== "object") return false;
193
+ const v = value;
194
+ return typeof v.valid === "boolean" && Array.isArray(v.errors) && Array.isArray(v.items);
195
+ }
196
+ function defaultValidator(endpoint) {
197
+ return async (input) => {
198
+ const res = await fetch(endpoint, {
199
+ method: "POST",
200
+ headers: { "Content-Type": "application/json" },
201
+ body: JSON.stringify(input)
202
+ });
203
+ if (!res.ok) {
204
+ throw new Error(`validate-cart failed with HTTP ${res.status}`);
205
+ }
206
+ const json = await res.json();
207
+ const data = json && typeof json === "object" ? json.data : void 0;
208
+ if (!isValidation(data)) {
209
+ throw new Error("validate-cart returned an unexpected body");
210
+ }
211
+ return data;
212
+ };
213
+ }
214
+ function waitForHydration() {
215
+ if (useCartStore.persist.hasHydrated()) return Promise.resolve();
216
+ return new Promise((resolve) => {
217
+ const unsubscribe = useCartStore.persist.onFinishHydration(() => {
218
+ unsubscribe();
219
+ resolve();
220
+ });
221
+ });
222
+ }
223
+ function applyCartValidation(items, validation, { removeUnavailable = true } = {}) {
224
+ const removed = [];
225
+ const quantityChanges = [];
226
+ const priceChanges = [];
227
+ const lineErrors = {};
228
+ const unresolvedErrors = [];
229
+ const byKey = new Map(items.map((item) => [cartLineKey(item.productId, item.variantId), item]));
230
+ const removeKeys = /* @__PURE__ */ new Set();
231
+ const newQuantity = /* @__PURE__ */ new Map();
232
+ for (const error of validation.errors) {
233
+ const key = cartLineKey(error.productId, error.variantId);
234
+ const line = byKey.get(key);
235
+ if (!line) {
236
+ unresolvedErrors.push(error);
237
+ continue;
238
+ }
239
+ lineErrors[key] = error;
240
+ if (error.suggestedAction === "remove" && removeUnavailable) {
241
+ removeKeys.add(key);
242
+ removed.push({
243
+ productId: line.productId,
244
+ variantId: line.variantId ?? null,
245
+ name: line.name,
246
+ ...line.variantName !== void 0 && { variantName: line.variantName },
247
+ type: error.type,
248
+ message: error.message
249
+ });
250
+ } else if (error.suggestedAction === "update_quantity" && typeof error.newQuantity === "number") {
251
+ newQuantity.set(key, error.newQuantity);
252
+ } else {
253
+ unresolvedErrors.push(error);
254
+ }
255
+ }
256
+ const live = new Map(
257
+ validation.items.map((item) => [cartLineKey(item.productId, item.variantId), item])
258
+ );
259
+ const next = [];
260
+ for (const line of items) {
261
+ const key = cartLineKey(line.productId, line.variantId);
262
+ if (removeKeys.has(key)) continue;
263
+ let updated = line;
264
+ const liveItem = live.get(key);
265
+ if (liveItem) {
266
+ if (liveItem.unitPrice !== line.unitPrice) {
267
+ priceChanges.push({
268
+ productId: line.productId,
269
+ variantId: line.variantId ?? null,
270
+ name: line.name,
271
+ ...line.variantName !== void 0 && { variantName: line.variantName },
272
+ from: line.unitPrice,
273
+ to: liveItem.unitPrice
274
+ });
275
+ updated = { ...updated, unitPrice: liveItem.unitPrice };
276
+ }
277
+ if (liveItem.maxStock !== line.maxStock) {
278
+ updated = { ...updated, maxStock: liveItem.maxStock };
279
+ }
280
+ }
281
+ const clampTo = newQuantity.get(key);
282
+ if (clampTo !== void 0) {
283
+ const to = Math.trunc(clampTo);
284
+ if (to < 1) {
285
+ removed.push({
286
+ productId: line.productId,
287
+ variantId: line.variantId ?? null,
288
+ name: line.name,
289
+ ...line.variantName !== void 0 && { variantName: line.variantName },
290
+ type: "out_of_stock",
291
+ message: lineErrors[key]?.message ?? ""
292
+ });
293
+ continue;
294
+ }
295
+ if (to < line.quantity) {
296
+ quantityChanges.push({
297
+ productId: line.productId,
298
+ variantId: line.variantId ?? null,
299
+ name: line.name,
300
+ ...line.variantName !== void 0 && { variantName: line.variantName },
301
+ from: line.quantity,
302
+ to
303
+ });
304
+ updated = { ...updated, quantity: to };
305
+ }
306
+ }
307
+ next.push(updated);
308
+ }
309
+ return { items: next, removed, quantityChanges, priceChanges, lineErrors, unresolvedErrors };
310
+ }
311
+ async function validateCart(options = {}) {
312
+ await waitForHydration();
313
+ const items = useCartStore.getState().items;
314
+ if (items.length === 0) return emptyOutcome("empty");
315
+ const validate = options.validate ?? defaultValidator(options.endpoint ?? DEFAULT_VALIDATE_CART_ENDPOINT);
316
+ let validation;
317
+ try {
318
+ validation = await validate({
319
+ items: items.map((i) => ({
320
+ productId: i.productId,
321
+ variantId: i.variantId,
322
+ quantity: i.quantity
323
+ }))
324
+ });
325
+ } catch (error) {
326
+ return { ...emptyOutcome("failed"), error };
327
+ }
328
+ const applied = applyCartValidation(useCartStore.getState().items, validation, options);
329
+ const { items: nextItems, ...report } = applied;
330
+ const changed = report.removed.length > 0 || report.quantityChanges.length > 0 || report.priceChanges.length > 0;
331
+ const current = useCartStore.getState().items;
332
+ if (nextItems.length !== current.length || nextItems.some((item, i) => item !== current[i])) {
333
+ useCartStore.setState({ items: nextItems });
334
+ }
335
+ const status = report.unresolvedErrors.length > 0 ? "invalid" : changed ? "adjusted" : "valid";
336
+ return { status, ...report };
337
+ }
338
+
339
+ // src/cart/use-cart-validation.ts
340
+ var import_react3 = require("react");
341
+ var IDLE = {
342
+ removed: [],
343
+ quantityChanges: [],
344
+ priceChanges: [],
345
+ lineErrors: {},
346
+ unresolvedErrors: []
347
+ };
348
+ function useCartValidation(options = {}) {
349
+ const { auto = true } = options;
350
+ const hydrated = useCartHydrated();
351
+ const [status, setStatus] = (0, import_react3.useState)("idle");
352
+ const [report, setReport] = (0, import_react3.useState)(IDLE);
353
+ const optionsRef = (0, import_react3.useRef)(options);
354
+ (0, import_react3.useEffect)(() => {
355
+ optionsRef.current = options;
356
+ });
357
+ const inFlight = (0, import_react3.useRef)(null);
358
+ const autoRan = (0, import_react3.useRef)(false);
359
+ const revalidate = (0, import_react3.useCallback)(() => {
360
+ if (inFlight.current) return inFlight.current;
361
+ setStatus("validating");
362
+ const run = validateCart(optionsRef.current).then((outcome) => {
363
+ inFlight.current = null;
364
+ const { status: nextStatus, ...rest } = outcome;
365
+ setReport(rest);
366
+ setStatus(nextStatus);
367
+ return outcome;
368
+ });
369
+ inFlight.current = run;
370
+ return run;
371
+ }, []);
372
+ (0, import_react3.useEffect)(() => {
373
+ if (!auto || !hydrated || autoRan.current) return;
374
+ autoRan.current = true;
375
+ void revalidate();
376
+ }, [auto, hydrated, revalidate]);
377
+ const getLineError = (0, import_react3.useCallback)(
378
+ (productId, variantId) => report.lineErrors[cartLineKey(productId, variantId)],
379
+ [report]
380
+ );
381
+ return { status, ...report, getLineError, revalidate };
382
+ }
170
383
  // Annotate the CommonJS export names for ESM import in node:
171
384
  0 && (module.exports = {
172
385
  CART_STORAGE_KEY,
173
386
  CART_STORE_PERSIST_VERSION,
174
387
  CartProvider,
388
+ DEFAULT_VALIDATE_CART_ENDPOINT,
175
389
  __resetCartStoreForTests,
390
+ applyCartValidation,
391
+ cartLineKey,
176
392
  useCart,
177
393
  useCartHydrated,
178
- useCartStore
394
+ useCartStore,
395
+ useCartValidation,
396
+ validateCart
179
397
  });
180
398
  //# sourceMappingURL=cart.cjs.map
package/dist/cart.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cart/index.ts","../src/cart/provider.tsx","../src/cart/store.ts","../src/cart/use-cart.ts"],"sourcesContent":["'use client'\n\n/**\n * `@reblu/site-next/cart` (REB-829) — the SDK's cart client surface. This\n * barrel MUST begin with its OWN leading `'use client'` directive — same R4\n * rule as `./auth`/`./academy`: tsup only preserves a directive prologue\n * declared by the entry file itself (see `tsup.config.ts`).\n *\n * Mount `CartProvider` once near the root layout, then read/mutate the cart\n * from any Client Component via `useCart()` (derived `total`/`itemCount`) or\n * the raw `useCartStore` (per-field selectors, zustand-idiomatic). Both\n * share the SAME store instance and the SAME `shop-cart` localStorage key a\n * SITE migrating off a hand-rolled store already used.\n */\nexport { CartProvider } from './provider'\nexport { useCart, useCartHydrated, type UseCartResult } from './use-cart'\nexport {\n useCartStore,\n CART_STORAGE_KEY,\n CART_STORE_PERSIST_VERSION,\n __resetCartStoreForTests,\n type CartItem,\n type CartStore,\n type AddItemOutcome,\n type AddItemStatus,\n} from './store'\n","'use client'\n\nimport { useEffect } from 'react'\nimport { useCartStore } from './store'\n\n/**\n * `CartProvider` — mount this once near the root layout (inside a Client\n * Component boundary). It owns the ONE explicit-hydration call\n * `useCartStore.persist.rehydrate()` requires (`store.ts`'s `skipHydration`\n * doc) — a SITE that forgets to mount it gets an always-empty cart that never\n * reads back whatever a previous visit (or page reload) persisted.\n */\nexport function CartProvider({ children }: { children: React.ReactNode }) {\n useEffect(() => {\n useCartStore.persist.rehydrate()\n }, [])\n\n return <>{children}</>\n}\n","import { create } from 'zustand'\nimport { persist } from 'zustand/middleware'\n\n/**\n * `useCartStore` — the SDK's cart state (REB-829). Ported verbatim (same\n * shape, same quantity-clamping rules) from demo-site's local\n * `lib/store/cart-store.ts`, which every SITE was otherwise expected to\n * hand-copy. `CART_STORAGE_KEY` stays exactly `'shop-cart'` on purpose: a\n * SITE migrating from the local copy keeps its visitors' existing carts.\n *\n * `skipHydration: true` (zustand persist) is load-bearing for SSR: without\n * it, zustand's persist middleware reads `localStorage` synchronously during\n * module evaluation, which throws (or silently desyncs) on the server. The\n * `CartProvider` (`./provider`) calls `useCartStore.persist.rehydrate()`\n * inside a `useEffect`, so hydration only ever happens client-side, after\n * mount — the same explicit-hydration contract the SITE README documents.\n */\nexport const CART_STORAGE_KEY = 'shop-cart'\n\n/**\n * The zustand `persist` schema version for the cart's PERSISTED shape (the\n * `CartItem[]` written to `localStorage` under `CART_STORAGE_KEY`) — bump\n * this (with a `migrate` option) if that shape ever changes incompatibly.\n * Also folded into {@link CART_STORE_GLOBAL_KEY} (REB-837 review fix): a\n * future incompatible shape change should mint a NEW global singleton\n * rather than let a stale, already-evaluated old-shape instance (e.g. kept\n * alive across an HMR reload in dev) silently satisfy the new one.\n */\nexport const CART_STORE_PERSIST_VERSION = 0\n\nexport interface CartItem {\n productId: string\n variantId?: string | null\n name: string\n variantName?: string\n /** Integer cents — every Reblu shop price travels as cents, never a float. */\n unitPrice: number\n quantity: number\n imageUrl?: string\n /** `null` means untracked/unlimited stock — never clamp against it. */\n maxStock: number | null\n /** Basis points, e.g. `2100` = 21%. `null`/`undefined` = unknown or exempt. */\n taxRateBps?: number | null\n taxRateName?: string | null\n}\n\n/**\n * Outcome of `addItem` (REB-837): a SITE's add-to-cart feedback (toast,\n * `AddToCartButton`'s built-in confirmed/error state) needs to know what\n * actually happened, not just fire-and-forget — `'clamped'` only differs\n * from `'added'` in the RESULTING quantity reaching less than what was\n * requested (capped by `maxStock`) while the line still grew; `'refused'`\n * means nothing was added (invalid quantity, out of stock, or the line was\n * already at `maxStock` — REB-839).\n */\nexport type AddItemStatus = 'added' | 'clamped' | 'refused'\n\nexport interface AddItemOutcome {\n status: AddItemStatus\n /** The resulting line quantity after the call (`0` when refused and no line exists). */\n quantity: number\n}\n\nexport interface CartStore {\n items: CartItem[]\n addItem: (item: CartItem) => AddItemOutcome\n removeItem: (productId: string, variantId?: string | null) => void\n updateQuantity: (\n productId: string,\n variantId: string | null | undefined,\n quantity: number,\n ) => void\n clearCart: () => void\n getTotal: () => number\n getItemCount: () => number\n}\n\nfunction isSameItem(a: CartItem, productId: string, variantId?: string | null): boolean {\n return a.productId === productId && (a.variantId ?? null) === (variantId ?? null)\n}\n\n/**\n * REB-829 review finding: neither `addItem` nor `updateQuantity` validated\n * `quantity` — `0`, a negative number, `NaN` (e.g. a stray `parseInt('')`\n * upstream), a non-integer, or `maxStock` itself being `0` (out of stock)\n * all used to leave an invalid line sitting in the cart, silently reaching\n * checkout.\n *\n * Normalizes to a safe POSITIVE INTEGER clamped to `maxStock`. Returns `0`\n * to signal \"not a valid positive quantity\" — callers decide what that\n * means for them: `addItem` ignores the call (never creates/corrupts a\n * line), `updateQuantity` removes the line (same as its pre-existing\n * `quantity <= 0` → remove contract, just now reached by more inputs).\n *\n * `Math.trunc` rounds toward zero (not `Math.floor`, which would round a\n * negative input further negative before the `< 1` check below ever saw it\n * — not a behavioural difference here since any negative result is\n * rejected either way, but `trunc` is the operation that actually means\n * \"drop the fractional part\").\n */\nfunction normalizeQuantity(quantity: number, maxStock: number | null): number {\n if (!Number.isFinite(quantity)) return 0\n const truncated = Math.trunc(quantity)\n if (truncated < 1) return 0\n if (maxStock === null) return truncated\n const maxTruncated = Math.trunc(maxStock)\n if (maxTruncated < 1) return 0\n return Math.min(truncated, maxTruncated)\n}\n\n/**\n * REB-837 cross-entry singleton guard — the mutation that caught it: a\n * single test rendering `useShopOrderStatus` (the `./shop` tsup entry) and\n * asserting on `useCartStore` (the `./cart` entry) saw the order resolve to\n * `paid` but the cart stay non-empty. Root cause: `splitting: false` (R4,\n * `tsup.config.ts`) bundles each entry standalone, so a plain relative\n * import of this file from `./shop` INLINES a SEPARATE copy of this\n * `create(...)` call — a second, disconnected store instance with its own\n * state, not the one `@reblu/site-next/cart` consumers read. `clearCart()`\n * from inside the hook silently cleared the WRONG store.\n *\n * `globalThis` keyed by a `Symbol.for(...)` (same registry entry regardless\n * of which bundle's copy of this module evaluates it first) makes every\n * entry that imports this file — however many times esbuild inlines it —\n * resolve to the SAME store. Verified by mutation: removing this guard\n * (reverting to a bare `export const useCartStore = create(...)`) turns\n * `use-shop-order-status.test.ts`'s cart-clearing assertions red again once\n * the hook is exercised through its OWN entry rather than imported directly\n * within `cart`'s own test file (which never saw the bug, since it only\n * ever touches ONE copy of this module).\n */\nfunction createCartStore() {\n return create<CartStore>()(\n persist(\n (set, get) => ({\n items: [],\n\n addItem: (item) => {\n // REB-837: `set`'s updater can't return a value to the caller, so the\n // outcome is captured in this closure variable and returned after.\n let outcome: AddItemOutcome = { status: 'refused', quantity: 0 }\n\n set((state) => {\n const existing = state.items.find((i) => isSameItem(i, item.productId, item.variantId))\n\n if (existing) {\n // REB-829 review follow-up (a): incoming maxStock is the\n // FRESHEST stock signal available — if it says 0, the product is\n // confirmed out of stock right now, so the existing line is\n // removed outright (same contract as updateQuantity's own\n // \"quantity resolves to <1 → remove\"), not merely left alone.\n if (item.maxStock !== null && Math.trunc(item.maxStock) < 1) {\n outcome = { status: 'refused', quantity: 0 }\n return {\n items: state.items.filter((i) => !isSameItem(i, item.productId, item.variantId)),\n }\n }\n\n const requestedQty = existing.quantity + item.quantity\n const newQty = normalizeQuantity(requestedQty, item.maxStock)\n if (newQty < 1) {\n // Invalid delta (NaN/non-positive quantity) — leave the\n // existing, previously-valid line untouched rather than\n // destroying good data because of one bad call.\n outcome = { status: 'refused', quantity: existing.quantity }\n return {}\n }\n\n // REB-839: nothing was gained when stock caps the line at (or,\n // with fresher stock, below) what it already held — report that\n // as 'refused', never as a partial-success 'clamped'. The line is\n // still reconciled below to the fresher maxStock.\n outcome = {\n status:\n newQty <= existing.quantity\n ? 'refused'\n : newQty < requestedQty\n ? 'clamped'\n : 'added',\n quantity: newQty,\n }\n\n // Refresh maxStock with the incoming value on every successful\n // merge — the stale-maxStock finding: a stored line used to keep\n // its ORIGINAL maxStock forever, so a later updateQuantity call\n // clamped against stock data that could be long out of date.\n return {\n items: state.items.map((i) =>\n isSameItem(i, item.productId, item.variantId)\n ? { ...i, quantity: newQty, maxStock: item.maxStock }\n : i,\n ),\n }\n }\n\n const qty = normalizeQuantity(item.quantity, item.maxStock)\n if (qty < 1) {\n // Invalid quantity, or maxStock 0 (out of stock) — never create\n // a line nobody could actually buy.\n outcome = { status: 'refused', quantity: 0 }\n return {}\n }\n\n outcome = { status: qty < item.quantity ? 'clamped' : 'added', quantity: qty }\n return { items: [...state.items, { ...item, quantity: qty }] }\n })\n\n return outcome\n },\n\n removeItem: (productId, variantId) => {\n set((state) => ({\n items: state.items.filter((i) => !isSameItem(i, productId, variantId)),\n }))\n },\n\n updateQuantity: (productId, variantId, quantity) => {\n const existing = get().items.find((i) => isSameItem(i, productId, variantId))\n if (!existing) return\n\n const newQty = normalizeQuantity(quantity, existing.maxStock)\n if (newQty < 1) {\n get().removeItem(productId, variantId)\n return\n }\n\n set((state) => ({\n items: state.items.map((i) =>\n isSameItem(i, productId, variantId) ? { ...i, quantity: newQty } : i,\n ),\n }))\n },\n\n clearCart: () => set({ items: [] }),\n\n getTotal: () => get().items.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0),\n\n getItemCount: () => get().items.reduce((sum, item) => sum + item.quantity, 0),\n }),\n {\n name: CART_STORAGE_KEY,\n version: CART_STORE_PERSIST_VERSION,\n skipHydration: true,\n },\n ),\n )\n}\n\ntype CartStoreApi = ReturnType<typeof createCartStore>\n\n/**\n * Versioned (REB-837 review fix) with {@link CART_STORE_PERSIST_VERSION} —\n * an incompatible shape change bumps that version, which mints a NEW\n * `Symbol.for(...)` registry key, so a stale old-shape singleton (e.g. kept\n * alive across an HMR reload in dev before a code change takes effect)\n * never satisfies the new one.\n */\nconst CART_STORE_GLOBAL_KEY = Symbol.for(\n `@reblu/site-next/cart-store@v${CART_STORE_PERSIST_VERSION}`,\n)\n\ninterface GlobalWithCartStore {\n [CART_STORE_GLOBAL_KEY]?: CartStoreApi\n}\n\nconst globalForCartStore = globalThis as typeof globalThis & GlobalWithCartStore\n\nexport const useCartStore: CartStoreApi =\n globalForCartStore[CART_STORE_GLOBAL_KEY] ??\n (globalForCartStore[CART_STORE_GLOBAL_KEY] = createCartStore())\n\n/**\n * TEST-ONLY (REB-837 review fix). Clears the `globalThis`-keyed slot so the\n * NEXT access to `useCartStore` — in THIS module instance — creates a fresh\n * store. The singleton is intentionally shared process-wide; most tests\n * should keep resetting STATE via `useCartStore.setState(...)` in\n * `beforeEach`, not the instance itself. Reach for this only to prove\n * something about the singleton mechanism (e.g. the cross-entry test in\n * `tests/cart-store-cross-entry-singleton.test.ts`) — it does not and\n * cannot affect an ALREADY-evaluated copy of this module inlined into a\n * different tsup entry's bundle (each such copy owns its own `const\n * globalForCartStore`/`CART_STORE_GLOBAL_KEY` closed over at that copy's\n * own module-evaluation time, same as any other ES module binding).\n */\nexport function __resetCartStoreForTests(): void {\n delete globalForCartStore[CART_STORE_GLOBAL_KEY]\n}\n","'use client'\n\nimport { useSyncExternalStore } from 'react'\n\nimport { useCartStore, type AddItemOutcome, type CartItem } from './store'\n\nexport interface UseCartResult {\n items: CartItem[]\n /** Sum of every line's `unitPrice * quantity`, in integer cents. */\n total: number\n /** Sum of every line's `quantity`. */\n itemCount: number\n /**\n * Whether `CartProvider`'s `persist.rehydrate()` has finished (REB-837).\n * `false` on the server and on the very first client render — `items`\n * (and therefore `itemCount`) jumps from `0` to whatever was persisted the\n * instant hydration finishes, which is NOT a real \"item added\" event. A\n * consumer driving `CartIcon`'s `bumpOnAdd` MUST gate the bump on this\n * flag (see `CartIcon`'s own guard) or every page load with a non-empty\n * cart bumps the badge once for free.\n */\n hydrated: boolean\n addItem: (item: CartItem) => AddItemOutcome\n removeItem: (productId: string, variantId?: string | null) => void\n updateQuantity: (\n productId: string,\n variantId: string | null | undefined,\n quantity: number,\n ) => void\n clearCart: () => void\n}\n\nfunction subscribeHydration(onStoreChange: () => void): () => void {\n return useCartStore.persist.onFinishHydration(onStoreChange)\n}\n\nfunction getHydratedSnapshot(): boolean {\n return useCartStore.persist.hasHydrated()\n}\n\n/** Server snapshot is always `false` — the server never reads `localStorage`. */\nfunction getServerHydratedSnapshot(): boolean {\n return false\n}\n\n/**\n * `useCartHydrated` — standalone so a SITE that only needs the flag (e.g. to\n * gate its own effects) doesn't have to subscribe to the whole cart via\n * `useCart()`.\n */\nexport function useCartHydrated(): boolean {\n return useSyncExternalStore(subscribeHydration, getHydratedSnapshot, getServerHydratedSnapshot)\n}\n\n/**\n * `useCart` — the ergonomic entry point for a SITE's cart UI: a single\n * selector call that re-renders only on `items` changes, with `total`/\n * `itemCount` derived inline (cheap — a plain `reduce` over a typically-small\n * array) rather than via `useCartStore`'s own `getTotal`/`getItemCount`\n * selectors, which are stable function references and would NOT trigger a\n * re-render on their own. A SITE that wants raw store access (existing\n * per-field selectors, e.g. `useCartStore((s) => s.addItem)`) can still\n * import `useCartStore` directly — this hook doesn't replace it.\n */\nexport function useCart(): UseCartResult {\n const items = useCartStore((s) => s.items)\n const addItem = useCartStore((s) => s.addItem)\n const removeItem = useCartStore((s) => s.removeItem)\n const updateQuantity = useCartStore((s) => s.updateQuantity)\n const clearCart = useCartStore((s) => s.clearCart)\n const hydrated = useCartHydrated()\n\n const total = items.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0)\n const itemCount = items.reduce((sum, item) => sum + item.quantity, 0)\n\n return { items, total, itemCount, hydrated, addItem, removeItem, updateQuantity, clearCart }\n}\n"],"mappings":";;;;;;;;;;;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,KAAK;AACzC,QAAM,UAAU,aAAa,CAAC,MAAM,EAAE,OAAO;AAC7C,QAAM,aAAa,aAAa,CAAC,MAAM,EAAE,UAAU;AACnD,QAAM,iBAAiB,aAAa,CAAC,MAAM,EAAE,cAAc;AAC3D,QAAM,YAAY,aAAa,CAAC,MAAM,EAAE,SAAS;AACjD,QAAM,WAAW,gBAAgB;AAEjC,QAAM,QAAQ,MAAM,OAAO,CAAC,KAAK,SAAS,MAAM,KAAK,YAAY,KAAK,UAAU,CAAC;AACjF,QAAM,YAAY,MAAM,OAAO,CAAC,KAAK,SAAS,MAAM,KAAK,UAAU,CAAC;AAEpE,SAAO,EAAE,OAAO,OAAO,WAAW,UAAU,SAAS,YAAY,gBAAgB,UAAU;AAC7F;","names":["import_react"]}
1
+ {"version":3,"sources":["../src/cart/index.ts","../src/cart/provider.tsx","../src/cart/store.ts","../src/cart/use-cart.ts","../src/cart/validate.ts","../src/cart/use-cart-validation.ts"],"sourcesContent":["'use client'\n\n/**\n * `@reblu/site-next/cart` (REB-829) — the SDK's cart client surface. This\n * barrel MUST begin with its OWN leading `'use client'` directive — same R4\n * rule as `./auth`/`./academy`: tsup only preserves a directive prologue\n * declared by the entry file itself (see `tsup.config.ts`).\n *\n * Mount `CartProvider` once near the root layout, then read/mutate the cart\n * from any Client Component via `useCart()` (derived `total`/`itemCount`) or\n * the raw `useCartStore` (per-field selectors, zustand-idiomatic). Both\n * share the SAME store instance and the SAME `shop-cart` localStorage key a\n * SITE migrating off a hand-rolled store already used.\n */\nexport { CartProvider } from './provider'\nexport { useCart, useCartHydrated, type UseCartResult } from './use-cart'\nexport {\n useCartStore,\n CART_STORAGE_KEY,\n CART_STORE_PERSIST_VERSION,\n __resetCartStoreForTests,\n type CartItem,\n type CartStore,\n type AddItemOutcome,\n type AddItemStatus,\n} from './store'\n// REB-981 — validate the persisted cart against live product/stock/price data.\nexport {\n validateCart,\n applyCartValidation,\n cartLineKey,\n DEFAULT_VALIDATE_CART_ENDPOINT,\n type CartValidator,\n type ValidateCartOptions,\n type CartValidationOutcome,\n type CartValidationStatus,\n type CartRemovedLine,\n type CartQuantityChange,\n type CartPriceChange,\n} from './validate'\nexport {\n useCartValidation,\n type UseCartValidationOptions,\n type UseCartValidationResult,\n} from './use-cart-validation'\n","'use client'\n\nimport { useEffect } from 'react'\nimport { useCartStore } from './store'\n\n/**\n * `CartProvider` — mount this once near the root layout (inside a Client\n * Component boundary). It owns the ONE explicit-hydration call\n * `useCartStore.persist.rehydrate()` requires (`store.ts`'s `skipHydration`\n * doc) — a SITE that forgets to mount it gets an always-empty cart that never\n * reads back whatever a previous visit (or page reload) persisted.\n */\nexport function CartProvider({ children }: { children: React.ReactNode }) {\n useEffect(() => {\n useCartStore.persist.rehydrate()\n }, [])\n\n return <>{children}</>\n}\n","import { create } from 'zustand'\nimport { persist } from 'zustand/middleware'\n\n/**\n * `useCartStore` — the SDK's cart state (REB-829). Ported verbatim (same\n * shape, same quantity-clamping rules) from demo-site's local\n * `lib/store/cart-store.ts`, which every SITE was otherwise expected to\n * hand-copy. `CART_STORAGE_KEY` stays exactly `'shop-cart'` on purpose: a\n * SITE migrating from the local copy keeps its visitors' existing carts.\n *\n * `skipHydration: true` (zustand persist) is load-bearing for SSR: without\n * it, zustand's persist middleware reads `localStorage` synchronously during\n * module evaluation, which throws (or silently desyncs) on the server. The\n * `CartProvider` (`./provider`) calls `useCartStore.persist.rehydrate()`\n * inside a `useEffect`, so hydration only ever happens client-side, after\n * mount — the same explicit-hydration contract the SITE README documents.\n */\nexport const CART_STORAGE_KEY = 'shop-cart'\n\n/**\n * The zustand `persist` schema version for the cart's PERSISTED shape (the\n * `CartItem[]` written to `localStorage` under `CART_STORAGE_KEY`) — bump\n * this (with a `migrate` option) if that shape ever changes incompatibly.\n * Also folded into {@link CART_STORE_GLOBAL_KEY} (REB-837 review fix): a\n * future incompatible shape change should mint a NEW global singleton\n * rather than let a stale, already-evaluated old-shape instance (e.g. kept\n * alive across an HMR reload in dev) silently satisfy the new one.\n */\nexport const CART_STORE_PERSIST_VERSION = 0\n\nexport interface CartItem {\n productId: string\n variantId?: string | null\n name: string\n variantName?: string\n /** Integer cents — every Reblu shop price travels as cents, never a float. */\n unitPrice: number\n quantity: number\n imageUrl?: string\n /** `null` means untracked/unlimited stock — never clamp against it. */\n maxStock: number | null\n /** Basis points, e.g. `2100` = 21%. `null`/`undefined` = unknown or exempt. */\n taxRateBps?: number | null\n taxRateName?: string | null\n}\n\n/**\n * Outcome of `addItem` (REB-837): a SITE's add-to-cart feedback (toast,\n * `AddToCartButton`'s built-in confirmed/error state) needs to know what\n * actually happened, not just fire-and-forget — `'clamped'` only differs\n * from `'added'` in the RESULTING quantity reaching less than what was\n * requested (capped by `maxStock`) while the line still grew; `'refused'`\n * means nothing was added (invalid quantity, out of stock, or the line was\n * already at `maxStock` — REB-839).\n */\nexport type AddItemStatus = 'added' | 'clamped' | 'refused'\n\nexport interface AddItemOutcome {\n status: AddItemStatus\n /** The resulting line quantity after the call (`0` when refused and no line exists). */\n quantity: number\n}\n\nexport interface CartStore {\n items: CartItem[]\n addItem: (item: CartItem) => AddItemOutcome\n removeItem: (productId: string, variantId?: string | null) => void\n updateQuantity: (\n productId: string,\n variantId: string | null | undefined,\n quantity: number,\n ) => void\n clearCart: () => void\n getTotal: () => number\n getItemCount: () => number\n}\n\nfunction isSameItem(a: CartItem, productId: string, variantId?: string | null): boolean {\n return a.productId === productId && (a.variantId ?? null) === (variantId ?? null)\n}\n\n/**\n * REB-829 review finding: neither `addItem` nor `updateQuantity` validated\n * `quantity` — `0`, a negative number, `NaN` (e.g. a stray `parseInt('')`\n * upstream), a non-integer, or `maxStock` itself being `0` (out of stock)\n * all used to leave an invalid line sitting in the cart, silently reaching\n * checkout.\n *\n * Normalizes to a safe POSITIVE INTEGER clamped to `maxStock`. Returns `0`\n * to signal \"not a valid positive quantity\" — callers decide what that\n * means for them: `addItem` ignores the call (never creates/corrupts a\n * line), `updateQuantity` removes the line (same as its pre-existing\n * `quantity <= 0` → remove contract, just now reached by more inputs).\n *\n * `Math.trunc` rounds toward zero (not `Math.floor`, which would round a\n * negative input further negative before the `< 1` check below ever saw it\n * — not a behavioural difference here since any negative result is\n * rejected either way, but `trunc` is the operation that actually means\n * \"drop the fractional part\").\n */\nfunction normalizeQuantity(quantity: number, maxStock: number | null): number {\n if (!Number.isFinite(quantity)) return 0\n const truncated = Math.trunc(quantity)\n if (truncated < 1) return 0\n if (maxStock === null) return truncated\n const maxTruncated = Math.trunc(maxStock)\n if (maxTruncated < 1) return 0\n return Math.min(truncated, maxTruncated)\n}\n\n/**\n * REB-837 cross-entry singleton guard — the mutation that caught it: a\n * single test rendering `useShopOrderStatus` (the `./shop` tsup entry) and\n * asserting on `useCartStore` (the `./cart` entry) saw the order resolve to\n * `paid` but the cart stay non-empty. Root cause: `splitting: false` (R4,\n * `tsup.config.ts`) bundles each entry standalone, so a plain relative\n * import of this file from `./shop` INLINES a SEPARATE copy of this\n * `create(...)` call — a second, disconnected store instance with its own\n * state, not the one `@reblu/site-next/cart` consumers read. `clearCart()`\n * from inside the hook silently cleared the WRONG store.\n *\n * `globalThis` keyed by a `Symbol.for(...)` (same registry entry regardless\n * of which bundle's copy of this module evaluates it first) makes every\n * entry that imports this file — however many times esbuild inlines it —\n * resolve to the SAME store. Verified by mutation: removing this guard\n * (reverting to a bare `export const useCartStore = create(...)`) turns\n * `use-shop-order-status.test.ts`'s cart-clearing assertions red again once\n * the hook is exercised through its OWN entry rather than imported directly\n * within `cart`'s own test file (which never saw the bug, since it only\n * ever touches ONE copy of this module).\n */\nfunction createCartStore() {\n return create<CartStore>()(\n persist(\n (set, get) => ({\n items: [],\n\n addItem: (item) => {\n // REB-837: `set`'s updater can't return a value to the caller, so the\n // outcome is captured in this closure variable and returned after.\n let outcome: AddItemOutcome = { status: 'refused', quantity: 0 }\n\n set((state) => {\n const existing = state.items.find((i) => isSameItem(i, item.productId, item.variantId))\n\n if (existing) {\n // REB-829 review follow-up (a): incoming maxStock is the\n // FRESHEST stock signal available — if it says 0, the product is\n // confirmed out of stock right now, so the existing line is\n // removed outright (same contract as updateQuantity's own\n // \"quantity resolves to <1 → remove\"), not merely left alone.\n if (item.maxStock !== null && Math.trunc(item.maxStock) < 1) {\n outcome = { status: 'refused', quantity: 0 }\n return {\n items: state.items.filter((i) => !isSameItem(i, item.productId, item.variantId)),\n }\n }\n\n const requestedQty = existing.quantity + item.quantity\n const newQty = normalizeQuantity(requestedQty, item.maxStock)\n if (newQty < 1) {\n // Invalid delta (NaN/non-positive quantity) — leave the\n // existing, previously-valid line untouched rather than\n // destroying good data because of one bad call.\n outcome = { status: 'refused', quantity: existing.quantity }\n return {}\n }\n\n // REB-839: nothing was gained when stock caps the line at (or,\n // with fresher stock, below) what it already held — report that\n // as 'refused', never as a partial-success 'clamped'. The line is\n // still reconciled below to the fresher maxStock.\n outcome = {\n status:\n newQty <= existing.quantity\n ? 'refused'\n : newQty < requestedQty\n ? 'clamped'\n : 'added',\n quantity: newQty,\n }\n\n // Refresh maxStock with the incoming value on every successful\n // merge — the stale-maxStock finding: a stored line used to keep\n // its ORIGINAL maxStock forever, so a later updateQuantity call\n // clamped against stock data that could be long out of date.\n return {\n items: state.items.map((i) =>\n isSameItem(i, item.productId, item.variantId)\n ? { ...i, quantity: newQty, maxStock: item.maxStock }\n : i,\n ),\n }\n }\n\n const qty = normalizeQuantity(item.quantity, item.maxStock)\n if (qty < 1) {\n // Invalid quantity, or maxStock 0 (out of stock) — never create\n // a line nobody could actually buy.\n outcome = { status: 'refused', quantity: 0 }\n return {}\n }\n\n outcome = { status: qty < item.quantity ? 'clamped' : 'added', quantity: qty }\n return { items: [...state.items, { ...item, quantity: qty }] }\n })\n\n return outcome\n },\n\n removeItem: (productId, variantId) => {\n set((state) => ({\n items: state.items.filter((i) => !isSameItem(i, productId, variantId)),\n }))\n },\n\n updateQuantity: (productId, variantId, quantity) => {\n const existing = get().items.find((i) => isSameItem(i, productId, variantId))\n if (!existing) return\n\n const newQty = normalizeQuantity(quantity, existing.maxStock)\n if (newQty < 1) {\n get().removeItem(productId, variantId)\n return\n }\n\n set((state) => ({\n items: state.items.map((i) =>\n isSameItem(i, productId, variantId) ? { ...i, quantity: newQty } : i,\n ),\n }))\n },\n\n clearCart: () => set({ items: [] }),\n\n getTotal: () => get().items.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0),\n\n getItemCount: () => get().items.reduce((sum, item) => sum + item.quantity, 0),\n }),\n {\n name: CART_STORAGE_KEY,\n version: CART_STORE_PERSIST_VERSION,\n skipHydration: true,\n },\n ),\n )\n}\n\ntype CartStoreApi = ReturnType<typeof createCartStore>\n\n/**\n * Versioned (REB-837 review fix) with {@link CART_STORE_PERSIST_VERSION} —\n * an incompatible shape change bumps that version, which mints a NEW\n * `Symbol.for(...)` registry key, so a stale old-shape singleton (e.g. kept\n * alive across an HMR reload in dev before a code change takes effect)\n * never satisfies the new one.\n */\nconst CART_STORE_GLOBAL_KEY = Symbol.for(\n `@reblu/site-next/cart-store@v${CART_STORE_PERSIST_VERSION}`,\n)\n\ninterface GlobalWithCartStore {\n [CART_STORE_GLOBAL_KEY]?: CartStoreApi\n}\n\nconst globalForCartStore = globalThis as typeof globalThis & GlobalWithCartStore\n\nexport const useCartStore: CartStoreApi =\n globalForCartStore[CART_STORE_GLOBAL_KEY] ??\n (globalForCartStore[CART_STORE_GLOBAL_KEY] = createCartStore())\n\n/**\n * TEST-ONLY (REB-837 review fix). Clears the `globalThis`-keyed slot so the\n * NEXT access to `useCartStore` — in THIS module instance — creates a fresh\n * store. The singleton is intentionally shared process-wide; most tests\n * should keep resetting STATE via `useCartStore.setState(...)` in\n * `beforeEach`, not the instance itself. Reach for this only to prove\n * something about the singleton mechanism (e.g. the cross-entry test in\n * `tests/cart-store-cross-entry-singleton.test.ts`) — it does not and\n * cannot affect an ALREADY-evaluated copy of this module inlined into a\n * different tsup entry's bundle (each such copy owns its own `const\n * globalForCartStore`/`CART_STORE_GLOBAL_KEY` closed over at that copy's\n * own module-evaluation time, same as any other ES module binding).\n */\nexport function __resetCartStoreForTests(): void {\n delete globalForCartStore[CART_STORE_GLOBAL_KEY]\n}\n","'use client'\n\nimport { useSyncExternalStore } from 'react'\n\nimport { useCartStore, type AddItemOutcome, type CartItem } from './store'\n\nexport interface UseCartResult {\n items: CartItem[]\n /** Sum of every line's `unitPrice * quantity`, in integer cents. */\n total: number\n /** Sum of every line's `quantity`. */\n itemCount: number\n /**\n * Whether `CartProvider`'s `persist.rehydrate()` has finished (REB-837).\n * `false` on the server and on the very first client render — `items`\n * (and therefore `itemCount`) jumps from `0` to whatever was persisted the\n * instant hydration finishes, which is NOT a real \"item added\" event. A\n * consumer driving `CartIcon`'s `bumpOnAdd` MUST gate the bump on this\n * flag (see `CartIcon`'s own guard) or every page load with a non-empty\n * cart bumps the badge once for free.\n */\n hydrated: boolean\n addItem: (item: CartItem) => AddItemOutcome\n removeItem: (productId: string, variantId?: string | null) => void\n updateQuantity: (\n productId: string,\n variantId: string | null | undefined,\n quantity: number,\n ) => void\n clearCart: () => void\n}\n\nfunction subscribeHydration(onStoreChange: () => void): () => void {\n return useCartStore.persist.onFinishHydration(onStoreChange)\n}\n\nfunction getHydratedSnapshot(): boolean {\n return useCartStore.persist.hasHydrated()\n}\n\n/** Server snapshot is always `false` — the server never reads `localStorage`. */\nfunction getServerHydratedSnapshot(): boolean {\n return false\n}\n\n/**\n * `useCartHydrated` — standalone so a SITE that only needs the flag (e.g. to\n * gate its own effects) doesn't have to subscribe to the whole cart via\n * `useCart()`.\n */\nexport function useCartHydrated(): boolean {\n return useSyncExternalStore(subscribeHydration, getHydratedSnapshot, getServerHydratedSnapshot)\n}\n\n/**\n * `useCart` — the ergonomic entry point for a SITE's cart UI: a single\n * selector call that re-renders only on `items` changes, with `total`/\n * `itemCount` derived inline (cheap — a plain `reduce` over a typically-small\n * array) rather than via `useCartStore`'s own `getTotal`/`getItemCount`\n * selectors, which are stable function references and would NOT trigger a\n * re-render on their own. A SITE that wants raw store access (existing\n * per-field selectors, e.g. `useCartStore((s) => s.addItem)`) can still\n * import `useCartStore` directly — this hook doesn't replace it.\n */\nexport function useCart(): UseCartResult {\n const items = useCartStore((s) => s.items)\n const addItem = useCartStore((s) => s.addItem)\n const removeItem = useCartStore((s) => s.removeItem)\n const updateQuantity = useCartStore((s) => s.updateQuantity)\n const clearCart = useCartStore((s) => s.clearCart)\n const hydrated = useCartHydrated()\n\n const total = items.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0)\n const itemCount = items.reduce((sum, item) => sum + item.quantity, 0)\n\n return { items, total, itemCount, hydrated, addItem, removeItem, updateQuantity, clearCart }\n}\n","import type {\n ShopCartValidation,\n ShopCartValidationError,\n ShopValidateCartInput,\n} from '@reblu/site-contracts'\n\nimport { useCartStore, type CartItem } from './store'\n\n/**\n * Cart validation (REB-981). A persisted cart (`localStorage` `shop-cart`)\n * outlives the catalog it was filled from: a product can be deleted,\n * deactivated, sold out or repriced between visits. `validateCart` asks the\n * SITE's validate-cart Route Handler (`createShopValidateCartRoute`) about\n * every line and APPLIES the answer to the store, so the cart page and the\n * checkout never show or submit a line nobody can buy.\n */\n\n/** Calls validate-cart for the given lines. Must reject (or throw) on failure. */\nexport type CartValidator = (input: ShopValidateCartInput) => Promise<ShopCartValidation>\n\nexport interface ValidateCartOptions {\n /**\n * The SITE's validate-cart Route Handler path, used by the default\n * validator. Defaults to `'/api/shop/validate-cart'`.\n */\n endpoint?: string\n /** Replaces the default `fetch`-based validator entirely. */\n validate?: CartValidator\n /**\n * `true` (default) removes lines whose error says `suggestedAction:\n * 'remove'` (`not_found`, `inactive`, `out_of_stock`). `false` leaves them\n * in the cart and only reports them through `lineErrors`.\n */\n removeUnavailable?: boolean\n}\n\n/** A line the validation removed — `name`/`variantName` come from the persisted line. */\nexport interface CartRemovedLine {\n productId: string\n variantId: string | null\n name: string\n variantName?: string\n type: ShopCartValidationError['type']\n message: string\n}\n\n/** A line whose quantity was clamped to the stock still available. */\nexport interface CartQuantityChange {\n productId: string\n variantId: string | null\n name: string\n variantName?: string\n from: number\n to: number\n}\n\n/** A line whose `unitPrice` (integer cents) was refreshed from the live price. */\nexport interface CartPriceChange {\n productId: string\n variantId: string | null\n name: string\n variantName?: string\n from: number\n to: number\n}\n\n/**\n * - `valid` — every line is buyable as it stands; nothing changed.\n * - `adjusted` — the cart was changed (lines removed, quantities clamped or\n * prices refreshed). Show the changes before letting the visitor pay.\n * - `invalid` — problems remain that were not applied (lines flagged with\n * `removeUnavailable: false`, or a cart-wide error).\n * - `empty` — no lines; nothing was sent.\n * - `failed` — the request failed; the cart was left exactly as it was.\n */\nexport type CartValidationStatus = 'valid' | 'adjusted' | 'invalid' | 'empty' | 'failed'\n\nexport interface CartValidationOutcome {\n status: CartValidationStatus\n removed: CartRemovedLine[]\n quantityChanges: CartQuantityChange[]\n priceChanges: CartPriceChange[]\n /** Every line error of the response, keyed by {@link cartLineKey}. */\n lineErrors: Record<string, ShopCartValidationError>\n /** Errors that match no cart line (e.g. `empty_cart`) or were not applied. */\n unresolvedErrors: ShopCartValidationError[]\n /** Set when `status` is `'failed'`. */\n error?: unknown\n}\n\nexport const DEFAULT_VALIDATE_CART_ENDPOINT = '/api/shop/validate-cart'\n\n/** Stable key for a cart line: `productId` + `variantId` (absent and `null` are the same line). */\nexport function cartLineKey(productId: string, variantId?: string | null): string {\n return JSON.stringify([productId, variantId ?? null])\n}\n\nfunction emptyOutcome(status: CartValidationStatus): CartValidationOutcome {\n return {\n status,\n removed: [],\n quantityChanges: [],\n priceChanges: [],\n lineErrors: {},\n unresolvedErrors: [],\n }\n}\n\nfunction isValidation(value: unknown): value is ShopCartValidation {\n if (!value || typeof value !== 'object') return false\n const v = value as Record<string, unknown>\n return typeof v.valid === 'boolean' && Array.isArray(v.errors) && Array.isArray(v.items)\n}\n\nfunction defaultValidator(endpoint: string): CartValidator {\n return async (input) => {\n const res = await fetch(endpoint, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify(input),\n })\n if (!res.ok) {\n throw new Error(`validate-cart failed with HTTP ${res.status}`)\n }\n const json: unknown = await res.json()\n const data = json && typeof json === 'object' ? (json as { data?: unknown }).data : undefined\n if (!isValidation(data)) {\n throw new Error('validate-cart returned an unexpected body')\n }\n return data\n }\n}\n\nfunction waitForHydration(): Promise<void> {\n if (useCartStore.persist.hasHydrated()) return Promise.resolve()\n return new Promise((resolve) => {\n const unsubscribe = useCartStore.persist.onFinishHydration(() => {\n unsubscribe()\n resolve()\n })\n })\n}\n\n/**\n * Pure: applies a validate-cart response to a list of lines. Lines the\n * response does not mention (e.g. added while the request was in flight)\n * are left untouched.\n */\nexport function applyCartValidation(\n items: CartItem[],\n validation: ShopCartValidation,\n { removeUnavailable = true }: Pick<ValidateCartOptions, 'removeUnavailable'> = {},\n): Omit<CartValidationOutcome, 'status' | 'error'> & { items: CartItem[] } {\n const removed: CartRemovedLine[] = []\n const quantityChanges: CartQuantityChange[] = []\n const priceChanges: CartPriceChange[] = []\n const lineErrors: Record<string, ShopCartValidationError> = {}\n const unresolvedErrors: ShopCartValidationError[] = []\n\n const byKey = new Map(items.map((item) => [cartLineKey(item.productId, item.variantId), item]))\n const removeKeys = new Set<string>()\n const newQuantity = new Map<string, number>()\n\n for (const error of validation.errors) {\n const key = cartLineKey(error.productId, error.variantId)\n const line = byKey.get(key)\n if (!line) {\n unresolvedErrors.push(error)\n continue\n }\n lineErrors[key] = error\n if (error.suggestedAction === 'remove' && removeUnavailable) {\n removeKeys.add(key)\n removed.push({\n productId: line.productId,\n variantId: line.variantId ?? null,\n name: line.name,\n ...(line.variantName !== undefined && { variantName: line.variantName }),\n type: error.type,\n message: error.message,\n })\n } else if (error.suggestedAction === 'update_quantity' && typeof error.newQuantity === 'number') {\n newQuantity.set(key, error.newQuantity)\n } else {\n // Flagged but not applied (`removeUnavailable: false`, or an action\n // this version does not know) — the line stays and still blocks.\n unresolvedErrors.push(error)\n }\n }\n\n const live = new Map(\n validation.items.map((item) => [cartLineKey(item.productId, item.variantId), item]),\n )\n\n const next: CartItem[] = []\n for (const line of items) {\n const key = cartLineKey(line.productId, line.variantId)\n if (removeKeys.has(key)) continue\n\n let updated = line\n const liveItem = live.get(key)\n if (liveItem) {\n if (liveItem.unitPrice !== line.unitPrice) {\n priceChanges.push({\n productId: line.productId,\n variantId: line.variantId ?? null,\n name: line.name,\n ...(line.variantName !== undefined && { variantName: line.variantName }),\n from: line.unitPrice,\n to: liveItem.unitPrice,\n })\n updated = { ...updated, unitPrice: liveItem.unitPrice }\n }\n if (liveItem.maxStock !== line.maxStock) {\n updated = { ...updated, maxStock: liveItem.maxStock }\n }\n }\n\n const clampTo = newQuantity.get(key)\n if (clampTo !== undefined) {\n const to = Math.trunc(clampTo)\n if (to < 1) {\n // No stock left at all — treat as unavailable.\n removed.push({\n productId: line.productId,\n variantId: line.variantId ?? null,\n name: line.name,\n ...(line.variantName !== undefined && { variantName: line.variantName }),\n type: 'out_of_stock',\n message: lineErrors[key]?.message ?? '',\n })\n continue\n }\n if (to < line.quantity) {\n quantityChanges.push({\n productId: line.productId,\n variantId: line.variantId ?? null,\n name: line.name,\n ...(line.variantName !== undefined && { variantName: line.variantName }),\n from: line.quantity,\n to,\n })\n updated = { ...updated, quantity: to }\n }\n }\n\n next.push(updated)\n }\n\n return { items: next, removed, quantityChanges, priceChanges, lineErrors, unresolvedErrors }\n}\n\n/**\n * Validates the persisted cart against live product, stock and price data\n * and applies the result to `useCartStore`:\n *\n * 1. removes lines the API says to remove, reporting their persisted names;\n * 2. clamps `insufficient_stock` lines to the quantity still available;\n * 3. refreshes `unitPrice` (and `maxStock`) from the live data.\n *\n * Waits for `CartProvider`'s hydration first, so it never validates the\n * empty pre-hydration cart. Never throws: a failed request resolves to\n * `status: 'failed'` and leaves the cart untouched. Running it again on an\n * already-clean cart is a no-op (`status: 'valid'`).\n */\nexport async function validateCart(options: ValidateCartOptions = {}): Promise<CartValidationOutcome> {\n await waitForHydration()\n\n const items = useCartStore.getState().items\n if (items.length === 0) return emptyOutcome('empty')\n\n const validate = options.validate ?? defaultValidator(options.endpoint ?? DEFAULT_VALIDATE_CART_ENDPOINT)\n\n let validation: ShopCartValidation\n try {\n validation = await validate({\n items: items.map((i) => ({\n productId: i.productId,\n variantId: i.variantId,\n quantity: i.quantity,\n })),\n })\n } catch (error) {\n return { ...emptyOutcome('failed'), error }\n }\n\n // Apply against the CURRENT lines, not the snapshot sent: the visitor may\n // have changed the cart while the request was in flight.\n const applied = applyCartValidation(useCartStore.getState().items, validation, options)\n const { items: nextItems, ...report } = applied\n const changed =\n report.removed.length > 0 || report.quantityChanges.length > 0 || report.priceChanges.length > 0\n const current = useCartStore.getState().items\n // Only write when a line object actually changed (a refreshed `maxStock`\n // alone counts) — an unchanged cart never re-renders its subscribers.\n if (nextItems.length !== current.length || nextItems.some((item, i) => item !== current[i])) {\n useCartStore.setState({ items: nextItems })\n }\n\n const status: CartValidationStatus =\n report.unresolvedErrors.length > 0 ? 'invalid' : changed ? 'adjusted' : 'valid'\n\n return { status, ...report }\n}\n","'use client'\n\nimport { useCallback, useEffect, useRef, useState } from 'react'\nimport type { ShopCartValidationError } from '@reblu/site-contracts'\n\nimport { useCartHydrated } from './use-cart'\nimport {\n cartLineKey,\n validateCart,\n type CartValidationOutcome,\n type CartValidationStatus,\n type ValidateCartOptions,\n} from './validate'\n\nexport interface UseCartValidationOptions extends ValidateCartOptions {\n /** Validate once automatically when the cart has hydrated. Defaults to `true`. */\n auto?: boolean\n}\n\nexport interface UseCartValidationResult extends Omit<CartValidationOutcome, 'status'> {\n /** `'idle'` before the first run, `'validating'` while a request is in flight. */\n status: CartValidationStatus | 'idle' | 'validating'\n /** The error the last run reported for this line, if any. */\n getLineError: (productId: string, variantId?: string | null) => ShopCartValidationError | undefined\n /**\n * Runs the validation now (e.g. right before submitting the checkout) and\n * resolves to its outcome. Concurrent calls share the in-flight request.\n */\n revalidate: () => Promise<CartValidationOutcome>\n}\n\nconst IDLE: Omit<CartValidationOutcome, 'status'> = {\n removed: [],\n quantityChanges: [],\n priceChanges: [],\n lineErrors: {},\n unresolvedErrors: [],\n}\n\n/**\n * `useCartValidation` (REB-981) — runs {@link validateCart} once when the\n * cart has hydrated (cart page mount) and exposes the last outcome, so the\n * page can show what was removed or adjusted and mark the offending lines.\n * Call `revalidate()` before checkout. Later cart edits never re-trigger the\n * automatic run, so it cannot loop.\n */\nexport function useCartValidation(options: UseCartValidationOptions = {}): UseCartValidationResult {\n const { auto = true } = options\n const hydrated = useCartHydrated()\n const [status, setStatus] = useState<UseCartValidationResult['status']>('idle')\n const [report, setReport] = useState<Omit<CartValidationOutcome, 'status'>>(IDLE)\n\n // Latest options without re-creating `revalidate` (synced after render,\n // never written during it).\n const optionsRef = useRef(options)\n useEffect(() => {\n optionsRef.current = options\n })\n const inFlight = useRef<Promise<CartValidationOutcome> | null>(null)\n const autoRan = useRef(false)\n\n const revalidate = useCallback((): Promise<CartValidationOutcome> => {\n if (inFlight.current) return inFlight.current\n setStatus('validating')\n const run = validateCart(optionsRef.current).then((outcome) => {\n inFlight.current = null\n const { status: nextStatus, ...rest } = outcome\n setReport(rest)\n setStatus(nextStatus)\n return outcome\n })\n inFlight.current = run\n return run\n }, [])\n\n useEffect(() => {\n if (!auto || !hydrated || autoRan.current) return\n autoRan.current = true\n void revalidate()\n }, [auto, hydrated, revalidate])\n\n const getLineError = useCallback(\n (productId: string, variantId?: string | null) =>\n report.lineErrors[cartLineKey(productId, variantId)],\n [report],\n )\n\n return { status, ...report, getLineError, revalidate }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,mBAA0B;;;ACF1B,qBAAuB;AACvB,wBAAwB;AAgBjB,IAAM,mBAAmB;AAWzB,IAAM,6BAA6B;AAiD1C,SAAS,WAAW,GAAa,WAAmB,WAAoC;AACtF,SAAO,EAAE,cAAc,cAAc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