import { describe, expect, test } from "vitest";
import { translateExternalError } from "./errors.js";
import { EXTERNAL_LANGUAGE_OPTIONS, externalDirection, externalMessage, externalMessages } from "./messages.js";

describe("external admin translations", () => {
  test("keeps all four locale catalogs structurally complete", () => {
    const englishKeys = Object.keys(externalMessages.en).sort();
    expect(englishKeys.length).toBeGreaterThan(250);
    for (const locale of ["zh-CN", "ar", "tr"]) {
      expect(Object.keys(externalMessages[locale]).sort()).toEqual(englishKeys);
    }
  });

  test("uses native language names and Arabic RTL only", () => {
    expect(EXTERNAL_LANGUAGE_OPTIONS.map((item) => item.label)).toEqual(["English", "简体中文", "العربية", "Türkçe"]);
    expect(["en", "zh-CN", "ar", "tr"].map((locale) => externalMessage(locale, "app.title"))).toEqual([
      "External Admin", "外管后台", "الإدارة الخارجية", "Harici Yönetim"
    ]);
    expect(["en", "zh-CN", "ar", "tr"].map((locale) => externalMessage(locale, "app.shortTitle"))).toEqual([
      "External Admin", "外管后台", "الإدارة الخارجية", "Harici Yönetim"
    ]);
    expect(externalDirection("ar")).toBe("rtl");
    expect(externalDirection("en")).toBe("ltr");
    expect(externalDirection("zh-CN")).toBe("ltr");
    expect(externalDirection("tr")).toBe("ltr");
  });

  test("never exposes an unknown backend-language message", () => {
    const t = (key) => externalMessage("en", key);
    expect(translateExternalError({ message: "服务端中文错误" }, t)).toBe("Request failed");
    expect(translateExternalError({ message: "服务端中文错误", status: 429 }, t)).toBe("Too many requests. Try again later.");
  });
});
