import { describe, expect, it } from "vitest";
import { toTrustedMapAction } from "./map-actions";

describe("toTrustedMapAction", () => {
  it("rejects zoom_to_map actions without a valid center", () => {
    expect(toTrustedMapAction("zoom_to_map", { zoom: 14 })).toBeNull();
    expect(toTrustedMapAction("zoom_to_map", { center: ["east", 39.1] })).toBeNull();
  });

  it("rejects unsupported map actions", () => {
    expect(toTrustedMapAction("run_javascript", { code: "alert(1)" })).toBeNull();
  });

  it("rejects malformed or empty apply_layer_style params", () => {
    expect(
      toTrustedMapAction("apply_layer_style", {
        layer_group_id: 123,
        layer_id: [],
        visible: "true"
      })
    ).toBeNull();
    expect(toTrustedMapAction("apply_layer_style", {})).toBeNull();
    expect(
      toTrustedMapAction("apply_layer_style", {
        layer_group_id: "pipes",
        layer_id: "scada",
        visible: true
      })
    ).toBeNull();
  });

  it("rejects out-of-range coordinates and zoom", () => {
    expect(toTrustedMapAction("zoom_to_map", { center: [181, 39.1] })).toBeNull();
    expect(toTrustedMapAction("zoom_to_map", { center: [117.2, -91] })).toBeNull();
    expect(toTrustedMapAction("zoom_to_map", { center: [117.2, 39.1], zoom: 25 })).toBeNull();
  });

  it("rejects empty and oversized locate requests", () => {
    expect(toTrustedMapAction("locate_features", { ids: [] })).toBeNull();
    expect(toTrustedMapAction("locate_features", { ids: Array.from({ length: 101 }, (_, index) => `P${index}`) })).toBeNull();
  });

  it("accepts valid zoom_to_map coordinates", () => {
    expect(toTrustedMapAction("zoom_to_map", { lng: 117.2, lat: 39.1, zoom: 13 })).toEqual({
      type: "zoom_to_map",
      center: [117.2, 39.1],
      zoom: 13,
      fallbackText: undefined
    });
  });

  it("accepts legacy zoom coordinate aliases and numeric strings", () => {
    expect(toTrustedMapAction("zoom_to_map", { coordinate: ["117.2", "39.1"], zoom: "13" })).toEqual({
      type: "zoom_to_map",
      center: [117.2, 39.1],
      zoom: 13,
      fallbackText: undefined
    });
    expect(toTrustedMapAction("zoom_to_map", { x: "117.2", y: "39.1" })).toMatchObject({
      type: "zoom_to_map",
      center: [117.2, 39.1]
    });
  });

  it("converts EPSG:3857 zoom coordinates", () => {
    const action = toTrustedMapAction("zoom_to_map", { x: 13046644.321, y: 4736005.854, source_crs: "EPSG:3857" });

    expect(action?.type).toBe("zoom_to_map");
    expect(action?.type === "zoom_to_map" ? action.center[0] : undefined).toBeCloseTo(117.2, 3);
    expect(action?.type === "zoom_to_map" ? action.center[1] : undefined).toBeCloseTo(39.1, 3);
  });

  it("normalizes legacy locate tool actions", () => {
    expect(toTrustedMapAction("locate_pipes", { pipe_ids: "P1,P2" })).toEqual({
      type: "locate_features",
      featureIds: ["P1", "P2"],
      layer: "geo_pipes_mat",
      fallbackText: undefined
    });
  });

  it("normalizes numeric locate ids", () => {
    expect(toTrustedMapAction("locate_junctions", { junction_id: 42 })).toMatchObject({
      type: "locate_features",
      featureIds: ["42"],
      layer: "geo_junctions_mat"
    });
  });

  it("accepts supply layer visibility and scada locate actions", () => {
    expect(toTrustedMapAction("apply_layer_style", { layer_id: "scada", visible: false })).toMatchObject({
      type: "apply_layer_style",
      layerGroupId: "scada",
      layerId: "scada",
      visible: false
    });
    expect(toTrustedMapAction("locate_scada", { scada_id: "S-1" })).toMatchObject({
      type: "locate_features",
      featureIds: ["S-1"],
      layer: "geo_scada"
    });
  });

  it("rejects unknown layers and accepts known visibility controls", () => {
    expect(toTrustedMapAction("locate_features", { ids: ["P1"], layer: "geo_unknown" })).toBeNull();
    expect(toTrustedMapAction("apply_layer_style", { layer_id: "pipes", visible: true })).toMatchObject({
      type: "apply_layer_style",
      layerGroupId: "pipes",
      layerId: "pipes",
      visible: true
    });
    expect(toTrustedMapAction("apply_layer_style", { layer_group_id: "simulation", visible: false })).toMatchObject({
      type: "apply_layer_style",
      layerGroupId: "simulation",
      visible: false
    });
    expect(toTrustedMapAction("apply_layer_style", { layer_id: "conduits", visible: true })).toBeNull();
  });
});
