import {
  act, renderHook, waitFor,
} from "@testing-library/react";
import assert from "node:assert";
import { describe, it } from "node:test";
import { useLocalisationPlugin } from "./useLocalisationPlugin";
import type { TLocalisationSource } from "../../types/localisation";

describe("useLocalisationPlugin", () => {
  it("becomes ready when a resource is added after plugin initialization", async () => {
    const code = "async.dynamic";
    const locales: TLocalisationSource = {
      async: {
        dynamic: [
          "Асинхронное значение",
          "Async value",
        ],
      },
    };
    const { result, unmount } = renderHook(() => useLocalisationPlugin(code));

    try {
      await waitFor(() => {
        assert.ok(result.current.plugin);
      });
      assert.equal(result.current.isReady, false);

      const plugin = result.current.plugin;
      assert.ok(plugin);

      await act(async () => {
        await plugin.getResourceAppend(locales);
      });

      await waitFor(() => {
        assert.equal(result.current.isReady, true);
      });
      assert.equal(result.current.isLoading, false);
      assert.equal(plugin.getLocale(code), "Асинхронное значение");
    } finally {
      unmount();
    }
  });
});
