import { beforeEach, describe, expect, it, vi } from "vitest";
import { t } from "./i18n";

describe("i18n utility", () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.clearAllMocks();
  });

  it("returns fallback if key not found", () => {
    expect(t("this.key.truly.does.not.exist", "Fallback")).toBe("Fallback");
  });

  it("returns key if fallback not provided and key not found", () => {
    expect(t("this.key.truly.does.not.exist")).toBe(
      "this.key.truly.does.not.exist",
    );
  });

  it("replaces variables in template", () => {
    expect(t("this.test.key", "Hello {{ name }}", { name: "World" })).toBe(
      "Hello World",
    );
  });

  it("respects locale in localStorage", () => {
    window.localStorage.setItem("locale", "en");
    expect(t("ui.common.save", "Save")).toBe("Save");
  });

  it("defaults to ko if no locale set and browser language is ko", () => {
    vi.spyOn(window.navigator, "language", "get").mockReturnValue("ko-KR");
    expect(t("ui.common.save", "저장")).toBe("저장");
  });
});
