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

import { FALLBACK_LOCALE, getLocalizedPathWithDefault, isSupportedLocale, resolveLocale } from "../lib/locale";

describe("locale path helpers", () => {
  it("recognizes supported locales explicitly", () => {
    expect(isSupportedLocale("ar")).toBe(true);
    expect(isSupportedLocale("en")).toBe(true);
    expect(isSupportedLocale("de")).toBe(true);
    expect(isSupportedLocale("fr")).toBe(false);
    expect(isSupportedLocale(undefined)).toBe(false);
  });

  it("resolves invalid locales with an explicit fallback", () => {
    expect(resolveLocale("ar", "de")).toBe("ar");
    expect(resolveLocale("fr", "en")).toBe("en");
    expect(resolveLocale("", FALLBACK_LOCALE)).toBe("de");
  });

  it("keeps the configured 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 locale", () => {
    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("strips old locale prefixes before rebuilding the target path", () => {
    expect(getLocalizedPathWithDefault("en", "/ar/contact", "de")).toBe("/en/contact");
    expect(getLocalizedPathWithDefault("ar", "/de/about", "ar")).toBe("/about");
  });
});
