import { describe, it, expect, vi } from "vitest";
import { usePhaserResize } from "../phaserUtils";

function makeScene(overrides = {}) {
  const handlers = {};
  const scene = {
    scale: {
      gameSize: { width: 800, height: 600 },
      baseSize: { width: 800, height: 600 },
      displaySize: { width: 800, height: 600 },
      resolution: 1,
      on: vi.fn((event, cb) => { handlers[event] = cb; }),
      off: vi.fn(),
    },
    events: {
      once: vi.fn((event, cb) => { handlers["events:" + event] = cb; }),
    },
    _handlers: handlers,
  };
  return { scene, handlers };
}

describe("usePhaserResize", () => {
  it("registers a resize listener on scene.scale", () => {
    const { scene } = makeScene();
    const cb = vi.fn();
    usePhaserResize(scene, cb);
    expect(scene.scale.on).toHaveBeenCalledWith("resize", cb);
  });

  it("calls callback immediately with current scale values", () => {
    const { scene } = makeScene();
    const cb = vi.fn();
    usePhaserResize(scene, cb);
    expect(cb).toHaveBeenCalledOnce();
    expect(cb).toHaveBeenCalledWith(
      scene.scale.gameSize,
      scene.scale.baseSize,
      scene.scale.displaySize,
      scene.scale.resolution,
    );
  });

  it("registers a shutdown listener on scene.events", () => {
    const { scene } = makeScene();
    usePhaserResize(scene, vi.fn());
    expect(scene.events.once).toHaveBeenCalledWith("shutdown", expect.any(Function));
  });

  it("removes the resize listener when shutdown fires", () => {
    const { scene, handlers } = makeScene();
    const cb = vi.fn();
    usePhaserResize(scene, cb);
    // Trigger shutdown
    handlers["events:shutdown"]();
    expect(scene.scale.off).toHaveBeenCalledWith("resize", cb);
  });
});
