import { expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { loadLocales, makeT, resolveI18n, resolveLang, translationCoverage } from "@wrnexus/i18n";

const localeDir = join(import.meta.dir, "../app/locales");
const i18n = resolveI18n(loadLocales(localeDir, { strict: true }), {
  default: "en",
  locales: ["en", "hi", "mr"],
  strict: true,
});

test("ships complete English, Hindi, and Marathi translations", () => {
  expect(i18n.langs).toEqual(["en", "hi", "mr"]);
  const coverage = translationCoverage(i18n);
  expect(coverage.en?.percentage).toBe(100);
  expect(coverage.hi?.percentage).toBe(100);
  expect(coverage.mr?.percentage).toBe(100);
  expect(makeT(i18n, "hi")("header.title")).toBe("बहुभाषी उदाहरण");
  expect(makeT(i18n, "mr")("header.title")).toBe("बहुभाषिक उदाहरण");
});

test("resolves browser negotiation and persisted cookie preferences", () => {
  expect(resolveLang(i18n, undefined, "mr-IN,hi;q=0.8,en;q=0.5")).toBe("mr");
  expect(resolveLang(i18n, "hi", "mr;q=1")).toBe("hi");
});

test("uses the packaged language switcher and document shell", () => {
  const page = readFileSync(join(import.meta.dir, "../app/pages/index.wrn"), "utf8");
  const document = readFileSync(join(import.meta.dir, "../app/layouts/document.wrn"), "utf8");
  expect(page).toContain(
    'import LanguageSwitcher from "@wrnexus/i18n/components/LanguageSwitcher.wrn"',
  );
  expect(page).toContain("<LanguageSwitcher");
  expect(page).not.toContain("locales=");
  expect(page).not.toContain("current=");
  expect(page).toContain('variant="compact"');
  expect(page).toContain('variant="segmented"');
  expect(page).toContain('t:placeholder="cards.attributes.placeholder"');
  expect(document).toContain("<html>");
});
