import dayjs from "dayjs";
import {
  buildBurstDetectionRequest,
  createBurstDetectionAnalysisParametersState,
  parseScadaFrequencyMinutes,
  resolvePressureSamplingInterval,
} from "./AnalysisParameters";

describe("burst detection request", () => {
  it("requests the latest complete monitoring time by default", () => {
    const state = createBurstDetectionAnalysisParametersState();
    state.schemeName = " latest-case ";

    expect(buildBurstDetectionRequest(state)).toEqual({
      scheme_name: "latest-case",
      sampling_interval_minutes: 15,
    });
    expect(state.detectionMode).toBe("latest");
    expect(state.targetTime).not.toBeNull();
    expect(state.targetTime!.minute() % 15).toBe(0);
  });

  it("sends one target time for historical replay", () => {
    const targetTime = dayjs("2026-06-20T13:30:00+08:00");

    expect(
      buildBurstDetectionRequest(
        {
          schemeName: "history-case",
          detectionMode: "historical",
          targetTime,
          samplingIntervalMinutes: 30,
          samplingIntervalSource: "manual",
        },
      ),
    ).toEqual({
      scheme_name: "history-case",
      sampling_interval_minutes: 30,
      target_time: targetTime.toISOString(),
    });
  });

  it("uses the dominant pressure SCADA frequency as the editable default", () => {
    expect(parseScadaFrequencyMinutes("0:15:00")).toBe(15);
    expect(parseScadaFrequencyMinutes("1:00:00")).toBe(60);
    expect(
      resolvePressureSamplingInterval([
        { type: "pressure", transmission_frequency: "0:15:00" },
        { type: "pressure", transmission_frequency: "0:15:00" },
        { type: "pressure", transmission_frequency: "0:30:00" },
        { type: "pipe_flow", transmission_frequency: "1:00:00" },
      ]),
    ).toBe(15);
  });
});
