import { describe, expect, it } from "vitest";

import {
  FALLBACK_LOCALE,
  getDirection,
  getLocalizedPath,
  getLocalizedPathWithDefault,
  isSupportedLocale,
  resolveLocale,
  stripLocalePrefix,
} from "@/lib/locale";

describe("isSupportedLocale", () => {
  it("accepts the three app locales", () => {
    expect(isSupportedLocale("de")).toBe(true);
    expect(isSupportedLocale("en")).toBe(true);
    expect(isSupportedLocale("ar")).toBe(true);
  });

  it("rejects everything else", () => {
    expect(isSupportedLocale("fr")).toBe(false);
    expect(isSupportedLocale("")).toBe(false);
    expect(isSupportedLocale(undefined)).toBe(false);
    expect(isSupportedLocale(null)).toBe(false);
  });
});

describe("resolveLocale", () => {
  it("keeps supported locales", () => {
    expect(resolveLocale("ar", "de")).toBe("ar");
  });

  it("falls back for unsupported locales", () => {
    expect(resolveLocale("fr", "en")).toBe("en");
    expect(resolveLocale(undefined, FALLBACK_LOCALE)).toBe("de");
    expect(resolveLocale(null, "ar")).toBe("ar");
  });
});

describe("getDirection", () => {
  it("is rtl for arabic only", () => {
    expect(getDirection("ar")).toBe("rtl");
    expect(getDirection("de")).toBe("ltr");
    expect(getDirection("en")).toBe("ltr");
    expect(getDirection("fr")).toBe("ltr");
  });
});

describe("stripLocalePrefix", () => {
  it("removes a bare locale prefix", () => {
    expect(stripLocalePrefix("/de")).toBe("/");
    expect(stripLocalePrefix("/ar")).toBe("/");
  });

  it("removes a nested locale prefix", () => {
    expect(stripLocalePrefix("/en/about")).toBe("/about");
    expect(stripLocalePrefix("/ar/portfolio/x")).toBe("/portfolio/x");
  });

  it("returns unprefixed paths unchanged", () => {
    expect(stripLocalePrefix("/about")).toBe("/about");
    expect(stripLocalePrefix("/")).toBe("/");
    expect(stripLocalePrefix("")).toBe("/");
  });

  it("does not strip lookalike segments", () => {
    expect(stripLocalePrefix("/design")).toBe("/design");
  });
});

describe("getLocalizedPathWithDefault", () => {
  it("keeps the default locale on the bare domain", () => {
    expect(getLocalizedPathWithDefault("ar", "/", "ar")).toBe("/");
    expect(getLocalizedPathWithDefault("de", "/", "ar")).toBe("/de");
    expect(getLocalizedPathWithDefault("en", "/", "ar")).toBe("/en");
  });

  it("builds nested paths against the configured default", () => {
    expect(getLocalizedPathWithDefault("ar", "/coming-soon", "ar")).toBe("/coming-soon");
    expect(getLocalizedPathWithDefault("de", "/coming-soon", "ar")).toBe("/de/coming-soon");
    expect(getLocalizedPathWithDefault("en", "/portfolio", "de")).toBe("/en/portfolio");
  });

  it("re-bases an already-prefixed path onto the requested locale", () => {
    expect(getLocalizedPathWithDefault("en", "/ar/contact", "de")).toBe("/en/contact");
    expect(getLocalizedPathWithDefault("ar", "/de/about", "ar")).toBe("/about");
  });

  it("normalizes an empty path to root", () => {
    expect(getLocalizedPathWithDefault("de", "", "de")).toBe("/");
    expect(getLocalizedPathWithDefault("en", "", "de")).toBe("/en");
  });

  it("falls back to the default locale for unsupported input", () => {
    expect(getLocalizedPathWithDefault("fr", "/about", "de")).toBe("/about");
  });

  it("getLocalizedPath is an alias of getLocalizedPathWithDefault", () => {
    expect(getLocalizedPath("en", "/about", "de")).toBe(
      getLocalizedPathWithDefault("en", "/about", "de"),
    );
  });
});
