import { beforeEach, describe, expect, it, vi } from "vitest";
import { labelsByLanguage, localeForLanguage, missingTranslationKeys, readStoredLanguage, supportedLanguages } from "./i18n";

const store = new Map<string, string>();

beforeEach(() => {
  store.clear();
  vi.stubGlobal("localStorage", {
    getItem: (key: string) => store.get(key) ?? null,
    setItem: (key: string, value: string) => store.set(key, value),
    removeItem: (key: string) => store.delete(key),
    clear: () => store.clear()
  });
});

describe("Ukrainian language support", () => {
  it("registers Ukrainian with its native name and flag", () => {
    expect(supportedLanguages).toContainEqual({
      code: "uk-UA",
      nativeName: "Українська",
      flag: "🇺🇦"
    });
  });

  it("has every translation key available in Ukrainian and English", () => {
    expect(missingTranslationKeys("uk-UA")).toEqual([]);
    expect(missingTranslationKeys("en")).toEqual([]);
  });

  it("persists Ukrainian and keeps Norwegian as the default", () => {
    expect(readStoredLanguage()).toBe("nb-NO");
    localStorage.setItem("arbeidspuls.language", "uk-UA");
    expect(readStoredLanguage()).toBe("uk-UA");
    expect(localeForLanguage(readStoredLanguage())).toBe("uk-UA");
  });

  it("changes only display labels and leaves stored report content untouched", () => {
    const report = {
      status: "kan_fortsette",
      comment: "This user comment must stay exactly as written.",
      advisorNote: "Veiledernotat skal ikke oversettes."
    } as const;
    const serialized = JSON.stringify(report);

    expect(labelsByLanguage["uk-UA"].statusLabels[report.status]).toBe("Може продовжувати");
    expect(JSON.stringify(report)).toBe(serialized);
    expect(report.comment).toBe("This user comment must stay exactly as written.");
    expect(report.advisorNote).toBe("Veiledernotat skal ikke oversettes.");
  });
});
