// Exercise 2: build a leaf Client Component that receives server data
//
// Model AddToCartButton.tsx as a "use client" component. It receives
// `productId` and `initialCount` as props (as if passed down from a
// Server Component parent that already fetched them), keeps its own
// click-count state, and exposes a pure `nextState` reducer function
// you'll implement — this simulates the setCount((c) => c + 1) pattern
// without needing a real React runtime to run this file standalone.

"use client";

interface ButtonState {
  productId: string;
  count: number;
}

function nextState(state: ButtonState): ButtonState {
  // TODO: return a new state object with the same productId and
  // count incremented by 1 (do not mutate `state`).
  return state;
}

// --- self-check: do not modify below this line ---
const initial: ButtonState = { productId: "sku-123", count: 0 };
const after1 = nextState(initial);
const after2 = nextState(after1);

const ok =
  initial.count === 0 && // original untouched
  after1.count === 1 &&
  after1.productId === "sku-123" &&
  after2.count === 2 &&
  after1 !== initial; // must be a new object, not a mutation

console.log(ok ? "PASS" : `FAIL — after1=${JSON.stringify(after1)} after2=${JSON.stringify(after2)}`);
