"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const I = require("../static/i18n.js");

test("LANGUAGES lists the four codes in order, English first", () => {
  assert.deepEqual(I.LANGUAGES.map((l) => l.code), ["en", "es", "fr", "ja"]);
  assert.equal(I.LANGUAGES[0].nativeName, "English");
  assert.equal(I.LANGUAGES.find((l) => l.code === "ja").nativeName, "日本語");
});

test("pickUiString returns the language value, falls back to en, then to key", () => {
  assert.equal(I.pickUiString("output.legend", "es"), I.UI_STRINGS["output.legend"].es);
  // a key present in en but (hypothetically) missing in fr falls back to en:
  const saved = I.UI_STRINGS["output.legend"].fr;
  delete I.UI_STRINGS["output.legend"].fr;
  assert.equal(I.pickUiString("output.legend", "fr"), I.UI_STRINGS["output.legend"].en);
  I.UI_STRINGS["output.legend"].fr = saved;
  assert.equal(I.pickUiString("totally.unknown.key", "en"), "totally.unknown.key");
});

test("UI_STRINGS has every language for every key", () => {
  for (const [key, vals] of Object.entries(I.UI_STRINGS)) {
    for (const code of ["en", "es", "fr", "ja"]) {
      assert.ok(typeof vals[code] === "string" && vals[code].length, `${key} missing ${code}`);
    }
  }
});

test("resolveStrings merges lang over en (per-key fallback)", () => {
  const cs = { en: { a: "A", b: "B" }, es: { a: "Aes" } };
  assert.deepEqual(I.resolveStrings(cs, "es"), { a: "Aes", b: "B" });
  assert.deepEqual(I.resolveStrings(cs, "fr"), { a: "A", b: "B" }); // no fr -> all en
  assert.deepEqual(I.resolveStrings({}, "es"), {});
});
