// @vitest-environment jsdom
import { renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
import i18next from "i18next";
// eslint-disable-next-line no-restricted-imports -- test needs initReactI18next to bootstrap i18n instance
import { initReactI18next } from "react-i18next";
import { I18nProvider, useI18n, useTranslation } from "./provider.js";

let i18nInstance: typeof i18next;

beforeAll(async () => {
  i18nInstance = i18next.createInstance();
  await i18nInstance.use(initReactI18next).init({
    lng: "en",
    resources: { en: { translation: { greeting: "hello" } } },
  });
});

function wrapper({ children }: { children: ReactNode }) {
  return <I18nProvider i18n={i18nInstance}>{children}</I18nProvider>;
}

describe("I18nProvider / useI18n / useTranslation", () => {
  it("provides the i18n instance via useI18n()", () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    expect(result.current.language).toBe("en");
  });

  it("useTranslation returns working t function", () => {
    const { result } = renderHook(() => useTranslation(), { wrapper });
    expect(result.current.t("greeting")).toBe("hello");
  });
});
