import { getCvPageCount, getCvPageMetrics, moveItem, normalizeCvLanguage, wrapSelection } from "./cvBuilder";

describe("moveItem", () => {
  test("moves an item forward", () => {
    expect(moveItem(["a", "b", "c"], 0, 2)).toEqual(["b", "c", "a"]);
  });
  test("moves an item backward", () => {
    expect(moveItem(["a", "b", "c"], 2, 0)).toEqual(["c", "a", "b"]);
  });
  test("no-op for equal or out-of-range indices", () => {
    const a = ["a", "b"];
    expect(moveItem(a, 1, 1)).toBe(a);
    expect(moveItem(a, 5, 0)).toBe(a);
    expect(moveItem(a, 0, -1)).toBe(a);
  });
  test("does not mutate the input", () => {
    const a = ["a", "b", "c"];
    moveItem(a, 0, 2);
    expect(a).toEqual(["a", "b", "c"]);
  });
});

describe("wrapSelection", () => {
  test("wraps a selection and keeps the selection over the inner text", () => {
    const r = wrapSelection("hello world", 6, 11, "**", "**");
    expect(r.text).toBe("hello **world**");
    expect(r.text.slice(r.selStart, r.selEnd)).toBe("world");
  });
  test("inserts a placeholder when nothing is selected", () => {
    const r = wrapSelection("", 0, 0, "**", "**", "bold text");
    expect(r.text).toBe("**bold text**");
    expect(r.text.slice(r.selStart, r.selEnd)).toBe("bold text");
  });
  test("wraps a link with a url suffix", () => {
    const r = wrapSelection("see docs", 4, 8, "[", "](https://)");
    expect(r.text).toBe("see [docs](https://)");
    expect(r.text.slice(r.selStart, r.selEnd)).toBe("docs");
  });
});

describe("CV page measurement", () => {
  test("uses the selected physical page size", () => {
    expect(getCvPageMetrics("a4")).toMatchObject({ widthMm: 210, heightMm: 297 });
    expect(getCvPageMetrics("letter")).toMatchObject({ widthMm: 215.9, heightMm: 279.4 });
  });

  test("rounds layout pixels without hiding partial pages", () => {
    const { heightPx } = getCvPageMetrics("a4");
    expect(getCvPageCount(Math.ceil(heightPx), heightPx)).toBe(1);
    expect(getCvPageCount(heightPx + 20, heightPx)).toBe(2);
    expect(getCvPageCount(heightPx * 2 + 20, heightPx)).toBe(3);
  });
});

describe("CV language compatibility", () => {
  test("maps legacy Norwegian values to the supported Bokmål locale", () => {
    expect(normalizeCvLanguage("no")).toBe("nb-NO");
    expect(normalizeCvLanguage("nb")).toBe("nb-NO");
    expect(normalizeCvLanguage("nb-NO")).toBe("nb-NO");
    expect(normalizeCvLanguage("en")).toBe("en");
  });
});
