import {
  coerceTimelineDurationMinutes,
  formatTimelineTime,
  getRoundedCurrentTimelineMinutes,
  getTimelineDisabledRangePercentages,
  normalizeTimelineMinutes,
  parseTimelineDurationMinutes,
} from "./timelineTime";

describe("timelineTime", () => {
  it("normalizes invalid minutes before formatting", () => {
    expect(formatTimelineTime(-1)).toBe("00:00");
    expect(formatTimelineTime(Number.NaN)).toBe("00:00");
  });

  it("clamps minutes to the configured range", () => {
    expect(normalizeTimelineMinutes(-1, 60, 120)).toBe(60);
    expect(normalizeTimelineMinutes(180, 60, 120)).toBe(120);
  });

  it("rounds the current time down to the timeline step", () => {
    expect(getRoundedCurrentTimelineMinutes(new Date("2026-07-16T10:29:00"))).toBe(615);
  });

  it("rounds the current time down to a custom timeline step", () => {
    expect(
      getRoundedCurrentTimelineMinutes(
        new Date("2026-07-16T10:29:00"),
        60,
      ),
    ).toBe(600);
  });

  it("parses EPANET-style duration values as minutes", () => {
    expect(parseTimelineDurationMinutes("0:05")).toBe(5);
    expect(parseTimelineDurationMinutes("1:00")).toBe(60);
    expect(parseTimelineDurationMinutes("24:00")).toBe(1440);
    expect(parseTimelineDurationMinutes("0:05:00")).toBe(5);
  });

  it("falls back for invalid, missing, and zero duration values", () => {
    expect(coerceTimelineDurationMinutes("invalid", 1440)).toBe(1440);
    expect(coerceTimelineDurationMinutes(undefined, 1440)).toBe(1440);
    expect(coerceTimelineDurationMinutes("0:00", 1440)).toBe(1440);
  });

  it("calculates disabled range as full-day percentages", () => {
    expect(getTimelineDisabledRangePercentages(360, 1080)).toEqual({
      leftWidth: 25,
      rightLeft: 75,
      rightWidth: 25,
    });
  });
});
