import { describe, expect, it } from "vitest";
import { createI18nInstance } from "./config.js";

describe("createI18nInstance", () => {
  it("creates an initialized i18next instance for the given locale", async () => {
    const i18n = await createI18nInstance({ locale: "ru" });
    expect(i18n.language).toBe("ru");
    expect(i18n.isInitialized).toBe(true);
  });

  it("can translate a known key from the Russian locale", async () => {
    const i18n = await createI18nInstance({ locale: "ru" });
    const value = i18n.t("BOARD.DEPARTURE");
    // The Russian file has "BOARD.DEPARTURE" = "Вылет"
    expect(value).toBe("Вылет");
  });

  it("can translate a known key from the English locale", async () => {
    const i18n = await createI18nInstance({ locale: "en" });
    const value = i18n.t("BOARD.DEPARTURE");
    expect(value).toBe("Departure");
  });

  it("uses initialResources instead of loading from filesystem when provided", async () => {
    const i18n = await createI18nInstance({
      locale: "ru",
      initialResources: {
        common: { TEST_KEY: "Тестовое значение" },
      },
    });
    expect(i18n.t("TEST_KEY")).toBe("Тестовое значение");
  });

  it("returns the key path if a key is missing (no fallback to other locale)", async () => {
    const i18n = await createI18nInstance({ locale: "ru" });
    expect(i18n.t("NONEXISTENT.KEY")).toBe("NONEXISTENT.KEY");
  });

  it("each call returns a fresh instance (request-scoped)", async () => {
    const a = await createI18nInstance({ locale: "ru" });
    const b = await createI18nInstance({ locale: "en" });
    expect(a).not.toBe(b);
    expect(a.language).toBe("ru");
    expect(b.language).toBe("en");
  });
});
