import assert from "node:assert/strict";
import test from "node:test";
import { defaultLanguage, detectLanguage, readSavedLanguage, translate, writeSavedLanguage } from "./i18n.js";

test("defaults new installations to English while preserving a saved choice", () => {
  const emptyStorage = { getItem: () => null };
  const koreanStorage = { getItem: () => "ko" };
  assert.equal(defaultLanguage(emptyStorage), "en");
  assert.equal(defaultLanguage(koreanStorage), "ko");
});

test("uses Korean for Korean system locales and English otherwise", () => {
  assert.equal(detectLanguage("ko-KR"), "ko");
  assert.equal(detectLanguage("en-US"), "en");
  assert.equal(detectLanguage("ja-JP"), "en");
});

test("persists only supported language choices", () => {
  const values = new Map();
  const storage = { getItem: (key) => values.get(key) ?? null, setItem: (key, value) => values.set(key, value) };
  assert.equal(readSavedLanguage(storage), null);
  writeSavedLanguage(storage, "ko");
  assert.equal(readSavedLanguage(storage), "ko");
  writeSavedLanguage(storage, "fr");
  assert.equal(readSavedLanguage(storage), "ko");
});

test("translates text and substitutions", () => {
  assert.equal(translate("ko", "settings.title"), "설정");
  assert.equal(translate("en", "settings.title"), "Settings");
  assert.equal(translate("ko", "settings.dataReset"), "데이터 초기화");
  assert.equal(translate("en", "settings.dataReset"), "Data reset");
  assert.equal(translate("ko", "account.removeConfirm", { name: "Main" }), "이 앱에서 Main 계정을 삭제할까요? 로그인과 채팅 파일은 이 PC에 남습니다.");
  assert.equal(translate("ko", "notification.count", { count: 3 }), "새 알림 3개");
  assert.equal(translate("en", "notification.count", { count: 3 }), "3 notifications");
});

test("translates composer presets and change-sheet messages", () => {
  assert.equal(translate("en", "composer.preset.default"), "Default permissions");
  assert.equal(translate("ko", "composer.preset.default"), "기본 권한");
  assert.equal(translate("en", "changes.unavailable.binary_file"), "Binary files do not have a displayable diff.");
  assert.equal(translate("ko", "changes.unavailable.binary_file"), "바이너리 파일은 diff를 표시하지 않습니다.");
});

test("translates the per-notification dismiss action", () => {
  assert.equal(translate("en", "notification.dismiss"), "Dismiss notification");
  assert.equal(translate("ko", "notification.dismiss"), "알림 지우기");
});

test("account handoff copy explains chat continuity and extra input tokens plainly", () => {
  assert.equal(translate("ko", "conversation.accountHandoffDescription"), "계정을 바꿔도 지금 대화는 그대로 이어집니다.");
  assert.equal(translate("ko", "conversation.accountHandoffTokens"), "새 계정이 이전 대화 내용을 처음부터 다시 읽어야 하므로, 입력 토큰을 평소보다 많이 사용합니다.");
  assert.equal(translate("ko", "conversation.accountHandoffConfirm"), "바꾸고 보내기");
  assert.equal(translate("en", "conversation.accountHandoffDescription"), "Your current conversation will continue after switching accounts.");
  assert.equal(translate("en", "conversation.accountHandoffTokens"), "The new account must reread the conversation from the beginning, so this request will use more input tokens than usual.");
});
