// Exercise 3: simulate a tiny Flight-like payload
//
// Real RSC "Flight" payloads interleave inlined server-rendered rows
// with "$L<n>" references to client component boundaries (see the
// theory file). This exercise simulates the STRUCTURE, not the real
// wire format, to build intuition.
//
// Implement buildFlightRows(tree), which walks a simplified element
// tree and produces an array of rows:
//   - for a node with type "server", push a row: { id, kind: "server", value: node.value }
//   - for a node with type "client", push a row: { id, kind: "client-ref", component: node.component }
// Assign ids sequentially starting at 0 in the order nodes are visited
// (depth-first, visiting `children` in array order after the node itself).

type TreeNode =
  | { type: "server"; value: string; children?: TreeNode[] }
  | { type: "client"; component: string };

type Row =
  | { id: number; kind: "server"; value: string }
  | { id: number; kind: "client-ref"; component: string };

function buildFlightRows(tree: TreeNode): Row[] {
  const rows: Row[] = [];
  let nextId = 0;

  function visit(node: TreeNode) {
    // TODO: implement depth-first traversal per the rules above.
    // Push a row for `node`, incrementing nextId each time, then
    // recurse into node.children (if it's a server node with children).
  }

  visit(tree);
  return rows;
}

// --- self-check: do not modify below this line ---
const tree: TreeNode = {
  type: "server",
  value: "div",
  children: [
    { type: "server", value: "h1" },
    { type: "client", component: "LikeButton" },
  ],
};

const rows = buildFlightRows(tree);
const expected: Row[] = [
  { id: 0, kind: "server", value: "div" },
  { id: 1, kind: "server", value: "h1" },
  { id: 2, kind: "client-ref", component: "LikeButton" },
];

const ok = JSON.stringify(rows) === JSON.stringify(expected);
console.log(ok ? "PASS" : `FAIL — got ${JSON.stringify(rows)}`);
