import type { Map as MapLibreMap } from "maplibre-gl";
import { describe, expect, it, vi } from "vitest";

import {
  SUPPLY_ASSET_ICON_PATHS,
  SUPPLY_ASSET_IMAGE_IDS,
  registerSupplyAssetImages
} from "./supply-icons";

describe("supply facility map icons", () => {
  it("uses MapLibre-compatible PNG assets from the public map directory", () => {
    expect(Object.keys(SUPPLY_ASSET_ICON_PATHS)).toEqual(["valve", "reservoir", "pump", "tank"]);
    Object.values(SUPPLY_ASSET_ICON_PATHS).forEach((path) => {
      expect(path).toMatch(/^\/map\/.+\.png$/);
    });
  });

  it("registers each facility type at two-times pixel density", async () => {
    const image = { width: 96, height: 96 };
    const loadedImageIds = new Set<string>();
    const map = {
      loadImage: vi.fn(() => Promise.resolve({ data: image })),
      hasImage: vi.fn((imageId: string) => loadedImageIds.has(imageId)),
      addImage: vi.fn((imageId: string) => loadedImageIds.add(imageId))
    } as unknown as Pick<MapLibreMap, "loadImage" | "addImage" | "hasImage"> & {
      loadImage: ReturnType<typeof vi.fn>;
      addImage: ReturnType<typeof vi.fn>;
    };

    await expect(registerSupplyAssetImages(map)).resolves.toEqual({
      status: "ready",
      failedImageIds: []
    });
    expect(map.loadImage).toHaveBeenCalledTimes(Object.keys(SUPPLY_ASSET_IMAGE_IDS).length);
    expect(map.addImage).toHaveBeenCalledTimes(Object.keys(SUPPLY_ASSET_IMAGE_IDS).length);
    expect(map.addImage).toHaveBeenCalledWith(expect.any(String), image, { pixelRatio: 2 });
  });
});
