import { describe, expect, it } from "vitest";
import { buildSubmittedPartsOrderLines, getSubmittedPartsOrderJobLineIds } from "./parts-order-modal.utils.js";

describe("parts order modal utilities", () => {
  it("preserves submitted job line ids after a row is removed", () => {
    const submittedLines = [
      { line_desc: "second line", job_line_id: "job-line-2" },
      { line_desc: "third line", job_line_id: "job-line-3" }
    ];
    const linesToOrder = [{ id: "job-line-1" }, { id: "job-line-2" }, { id: "job-line-3" }];

    const result = buildSubmittedPartsOrderLines({ submittedLines, linesToOrder, isReturn: false });

    expect(result.map((line) => line.job_line_id)).toEqual(["job-line-2", "job-line-3"]);
    expect(getSubmittedPartsOrderJobLineIds(result)).toEqual(["job-line-2", "job-line-3"]);
  });

  it("falls back to original return line ids when the form omits hidden metadata", () => {
    const submittedLines = [{ line_desc: "return line" }];
    const linesToOrder = [{ joblineid: "return-job-line-1" }];

    const result = buildSubmittedPartsOrderLines({ submittedLines, linesToOrder, isReturn: true });

    expect(result).toEqual([
      {
        line_desc: "return line",
        job_line_id: "return-job-line-1",
        cm_received: false
      }
    ]);
  });
});
