import { expect, test } from "bun:test";
import {
  auditLocaleKeys,
  createPseudoLocale,
  extractTranslationKeys,
  flattenMessageKeys,
  localeDirection,
  pseudoLocalize,
} from "../src/index.ts";

test("extracts static translation keys from code and WRN attributes", () => {
  const keys = extractTranslationKeys(`
    const title = t("account.title")
    const copy = i18n.t('account.copy')
    const dynamic = t(key)
    <p data-i18n="account.help"></p>
  `);
  expect(keys.map(({ key }) => key)).toEqual(["account.copy", "account.help", "account.title"]);
});

test("audits locale completeness against a reference locale", () => {
  const messages = {
    en: { common: { hello: "Hello", bye: "Bye" } },
    fr: { common: { hello: "Bonjour", extra: "Extra" } },
  };
  expect(flattenMessageKeys(messages.en)).toEqual(["common.bye", "common.hello"]);
  expect(auditLocaleKeys(messages, "en").fr).toEqual({
    missing: ["common.bye"],
    extra: ["common.extra"],
  });
});

test("pseudo-locales preserve placeholders and exercise LTR and RTL layouts", () => {
  expect(pseudoLocalize("Hello {name} <strong>today</strong>")).toContain("{name}");
  const pseudo = createPseudoLocale({ greeting: "Hello {name}", nested: { save: "Save" } });
  expect(pseudo.greeting).toBe("[Hëllô {name}~~~]");
  expect((pseudo.nested as Record<string, string>).save).toBe("[Sàvë~~~]");
  expect(pseudoLocalize("Hello {name}", { rtl: true }).startsWith("\u202e[")).toBe(true);
  expect(localeDirection("ar-XB")).toBe("rtl");
});
