import { describe, expect, it } from "vitest";
import {
  getFeatureInsightMetrics,
  getFeaturePanelModel,
  getFeaturePanelProperties,
  getFeaturePropertyLabel
} from "./feature-properties";

describe("feature panel properties", () => {
  it.each([
    ["pipes", ["id", "diameter", "length", "material"]],
    ["junctions", ["id", "elevation", "demand", "pressure"]]
  ] as const)("shows the configured %s fields", (layer, expectedKeys) => {
    const entries = getFeaturePanelProperties(layer, {}, "feature-id");

    expect(entries.map((entry) => entry.key)).toEqual(expectedKeys);
    expect(entries[0].value).toBe("feature-id");
  });

  it("formats pipe diameter from GeoServer DN millimeter values", () => {
    const entries = getFeaturePanelProperties("pipes", {
      id: "P-1",
      diameter: 600,
      length: 42.5,
      material: "DI"
    });

    expect(entries.map(({ label, value }) => [label, value])).toEqual([
      ["编号", "P-1"],
      ["管径", "600 mm"],
      ["长度", "42.50 m"],
      ["材质", "球墨铸铁"]
    ]);
  });

  it("builds insight metrics from the water-network layer configuration", () => {
    const metrics = getFeatureInsightMetrics("pipes", {
      diameter: 600,
      length: 42.5,
      material: "DI",
      status: "OPEN"
    });

    expect(metrics).toEqual([
      { label: "管径", value: "600 mm" },
      { label: "长度", value: "42.50 m" },
      { label: "材质", value: "球墨铸铁" },
      { label: "状态", value: "开启" }
    ]);
  });

  it("localizes common enumerated property values in the panel", () => {
    expect(getFeaturePanelProperties("valves", {
      id: "V-1",
      node1: "J-1",
      node2: "J-2",
      diameter: 300,
      v_type: "PRV",
      setting: "OPEN"
    }).map(({ label, value }) => [label, value])).toEqual([
      ["编号", "V-1"],
      ["起点节点", "J-1"],
      ["终点节点", "J-2"],
      ["管径", "300 mm"],
      ["阀门类型", "减压阀"],
      ["阀门设定", "开启"]
    ]);

    expect(getFeaturePanelProperties("scada", {
      id: "S-1",
      type: "pressure",
      associated_element_id: "J-1",
      transmission_mode: "non_realtime",
      transmission_frequency: "15min",
      reliability: "high"
    }).map(({ label, value }) => [label, value])).toEqual([
      ["编号", "S-1"],
      ["类型", "压力监测"],
      ["关联资产", "J-1"],
      ["传输方式", "非实时"],
      ["传输频率", "15min"],
      ["可靠性", "高"]
    ]);

    expect(getFeaturePanelProperties("scada", {
      id: "S-2",
      type: "pipe_flow"
    }).find((entry) => entry.key === "type")?.value).toBe("流量监测");
  });

  it("uses the map feature id when the properties omit it", () => {
    const model = getFeaturePanelModel("junctions", { demand: 3.4 }, "map-feature-id");

    expect(model.id).toBe("map-feature-id");
    expect(model.badge).toBeUndefined();
    expect(model.attributes.map((entry) => entry.key)).toEqual(["elevation", "demand", "pressure"]);
  });

  it.each([
    ["valves", ["id", "node1", "node2", "diameter", "v_type", "setting"]],
    ["reservoirs", ["id", "head", "pattern"]],
    ["scada", ["id", "type", "associated_element_id", "transmission_mode", "transmission_frequency", "reliability"]],
    ["pumps", ["id", "node1", "node2", "status"]],
    ["tanks", ["id", "elevation", "init_level", "min_level", "max_level"]]
  ] as const)("localizes configured %s panel field labels", (layer, keys) => {
    const entries = getFeaturePanelProperties(layer, {}, "feature-id");

    expect(entries.map((entry) => entry.key)).toEqual(keys);
    expect(entries).toEqual(
      entries.map((entry) => ({
        ...entry,
        label: getFeaturePropertyLabel(entry.key)
      }))
    );
    expect(entries.filter((entry) => entry.key !== "id").map((entry) => entry.label))
      .not.toContainEqual(expect.stringMatching(/^[a-z][a-z0-9_]*$/));
  });
});
