// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { renderHook } from "@testing-library/react";
import { useCityName } from "./useDictionaries.js";

vi.mock("@modern-js/runtime/router", () => ({
  useParams: () => ({ lang: "ru-ru" }),
}));

const mockDictionariesState = vi.fn();
vi.mock("@/shared/dictionaries/useDictionaries.js", () => ({
  useDictionaries: () => mockDictionariesState(),
}));

describe("useCityName", () => {
  it("returns the code when dictionaries are still loading", () => {
    mockDictionariesState.mockReturnValue({
      dictionaries: null,
      loading: true,
      error: null,
    });
    const { result } = renderHook(() => useCityName("SVO"));
    expect(result.current).toBe("SVO");
  });

  it("resolves a city code to the localized name", () => {
    mockDictionariesState.mockReturnValue({
      dictionaries: {
        cityByCode: new Map([["MOW", { name: "Москва" }]]),
        airportByCode: new Map(),
      },
      loading: false,
      error: null,
    });
    const { result } = renderHook(() => useCityName("MOW"));
    expect(result.current).toBe("Москва");
  });

  it("resolves an airport code to the localized name", () => {
    mockDictionariesState.mockReturnValue({
      dictionaries: {
        cityByCode: new Map(),
        airportByCode: new Map([["SVO", { name: "Шереметьево" }]]),
      },
      loading: false,
      error: null,
    });
    const { result } = renderHook(() => useCityName("SVO"));
    expect(result.current).toBe("Шереметьево");
  });

  it("falls back to the code for unknown lookups", () => {
    mockDictionariesState.mockReturnValue({
      dictionaries: {
        cityByCode: new Map(),
        airportByCode: new Map(),
      },
      loading: false,
      error: null,
    });
    const { result } = renderHook(() => useCityName("ZZZ"));
    expect(result.current).toBe("ZZZ");
  });

  it("handles empty string", () => {
    mockDictionariesState.mockReturnValue({
      dictionaries: null,
      loading: true,
      error: null,
    });
    const { result } = renderHook(() => useCityName(""));
    expect(result.current).toBe("");
  });
});
