import { describe, expect, it } from "vitest";
import { buildExtractionTree } from "./build-extraction-tree";
import { buildCaptureRows } from "./capture-rows";

describe("extraction normalization", () => {
  it("uses syntactic hierarchy but preserves engine ranges and status", () => {
    const matches = [
      {
        matchNumber: 1,
        value: "ab",
        valueStatus: "complete" as const,
        range: { startUtf16: 2, endUtf16: 4 },
        nativeRange: { start: 2, end: 4, unit: "utf16" as const },
        captures: [
          {
            groupNumber: 1,
            groupName: "outer",
            value: "ab",
            status: "participated" as const,
            range: { startUtf16: 2, endUtf16: 4 },
            nativeRange: { start: 2, end: 4, unit: "utf16" as const },
          },
          {
            groupNumber: 2,
            groupName: "inner",
            value: "",
            status: "matched-empty" as const,
            range: { startUtf16: 4, endUtf16: 4 },
            nativeRange: { start: 4, end: 4, unit: "utf16" as const },
          },
        ],
      },
    ];
    const captures = [
      {
        number: 1,
        name: "outer",
        range: { startUtf16: 0, endUtf16: 8 },
        repeated: false,
      },
      {
        number: 2,
        name: "inner",
        range: { startUtf16: 3, endUtf16: 5 },
        repeated: false,
        parentCaptureNumber: 1,
      },
    ];
    const tree = buildExtractionTree(matches, captures);
    expect(tree[0]?.children[1]?.groupNumber).toBe(1);
    expect(tree[0]?.children[1]?.children[0]).toEqual(
      expect.objectContaining({
        groupNumber: 2,
        status: "matched-empty",
        range: { startUtf16: 4, endUtf16: 4 },
        provenance: "engine-result",
      }),
    );
    expect(buildCaptureRows(matches, "x\nab")[0]).toEqual(
      expect.objectContaining({ line: 2, column: 1, nativeUnit: "utf16" }),
    );
  });
});
