import { describe, expect, it } from "vitest";
import {
  LANGUAGES,
  isLanguage,
  resolveLocaleFromPath,
  stripLocaleFromPath,
} from "./resolver.js";

describe("LANGUAGES", () => {
  it("contains exactly 9 supported languages", () => {
    expect(LANGUAGES).toHaveLength(9);
    expect(LANGUAGES).toContain("ru");
    expect(LANGUAGES).toContain("en");
    expect(LANGUAGES).toContain("de");
  });
});

describe("isLanguage", () => {
  it("returns true for valid languages", () => {
    expect(isLanguage("ru")).toBe(true);
    expect(isLanguage("en")).toBe(true);
    expect(isLanguage("zh")).toBe(true);
  });

  it("returns false for invalid strings", () => {
    expect(isLanguage("xx")).toBe(false);
    expect(isLanguage("")).toBe(false);
    expect(isLanguage("RU")).toBe(false);
    expect(isLanguage("russian")).toBe(false);
  });
});

describe("resolveLocaleFromPath", () => {
  it("extracts BCP-47 locale from the first path segment", () => {
    expect(resolveLocaleFromPath("/ru-ru/onlineboard")).toBe("ru-ru");
    expect(resolveLocaleFromPath("/en-en/onlineboard/flight/SU100")).toBe("en-en");
    expect(resolveLocaleFromPath("/de-de/schedule")).toBe("de-de");
  });

  it("auto-promotes a bare short language code to its BCP-47 cousin", () => {
    expect(resolveLocaleFromPath("/ru/onlineboard")).toBe("ru-ru");
    expect(resolveLocaleFromPath("/en/onlineboard/flight/SU100")).toBe("en-en");
    expect(resolveLocaleFromPath("/de/schedule")).toBe("de-de");
  });

  it("returns null for paths without a valid locale prefix", () => {
    expect(resolveLocaleFromPath("/onlineboard")).toBeNull();
    expect(resolveLocaleFromPath("/xx/onlineboard")).toBeNull();
    expect(resolveLocaleFromPath("/xx-xx/onlineboard")).toBeNull();
    expect(resolveLocaleFromPath("/")).toBeNull();
    expect(resolveLocaleFromPath("")).toBeNull();
  });

  it("handles bare locale path (e.g., /ru-ru)", () => {
    expect(resolveLocaleFromPath("/ru-ru")).toBe("ru-ru");
    expect(resolveLocaleFromPath("/ru-ru/")).toBe("ru-ru");
  });
});

describe("stripLocaleFromPath", () => {
  it("strips BCP-47 locale and returns the rest", () => {
    expect(stripLocaleFromPath("/ru-ru/onlineboard")).toEqual({
      locale: "ru-ru",
      rest: "/onlineboard",
    });
    expect(stripLocaleFromPath("/en-en/onlineboard/flight/SU100")).toEqual({
      locale: "en-en",
      rest: "/onlineboard/flight/SU100",
    });
  });

  it("auto-promotes short codes when stripping", () => {
    expect(stripLocaleFromPath("/ru/onlineboard")).toEqual({
      locale: "ru-ru",
      rest: "/onlineboard",
    });
  });

  it("returns / as rest for bare locale path", () => {
    expect(stripLocaleFromPath("/ru-ru")).toEqual({ locale: "ru-ru", rest: "/" });
    expect(stripLocaleFromPath("/ru-ru/")).toEqual({ locale: "ru-ru", rest: "/" });
  });

  it("returns null for paths without a valid locale prefix", () => {
    expect(stripLocaleFromPath("/onlineboard")).toBeNull();
    expect(stripLocaleFromPath("/xx-xx/schedule")).toBeNull();
  });
});
