// The Lucia project.
// Copyright 2026-2026 DSP, inc.  All rights reserved.
// Authors:
//   imacat.yang@dsp.im (imacat), 2026/03/05

import { describe, it, expect } from "vitest";
import {
  setLineChartData,
  setBarChartData,
  timeRange,
  getXIndex,
  formatTime,
  formatMaxTwo,
} from "@/module/setChartData.js";

describe("timeRange", () => {
  it("splits time into equal parts", () => {
    const result = timeRange(0, 100, 3);
    expect(result).toEqual([0, 50, 100]);
  });

  it("rounds values to integers", () => {
    const result = timeRange(0, 10, 4);
    // 0, 3.33, 6.67, 10 -> rounded
    expect(result).toEqual([0, 3, 7, 10]);
  });
});

describe("getXIndex", () => {
  it("finds exact match index", () => {
    expect(getXIndex([10, 20, 30], 20)).toBe(1);
  });

  it("finds closest value index", () => {
    expect(getXIndex([10, 20, 30], 22)).toBe(1);
  });

  it("returns last matching index for equal distances", () => {
    // 15 is equidistant from 10 and 20, <= means last wins
    expect(getXIndex([10, 20, 30], 15)).toBe(1);
  });
});

describe("formatTime", () => {
  it("formats seconds only", () => {
    expect(formatTime(45)).toBe("45s");
  });

  it("formats minutes and seconds", () => {
    expect(formatTime(125)).toBe("2m5s");
  });

  it("formats hours, minutes, seconds", () => {
    expect(formatTime(3661)).toBe("1h1m1s");
  });

  it("formats days", () => {
    expect(formatTime(90061)).toBe("1d1h1m1s");
  });

  it("returns null for NaN", () => {
    expect(formatTime("abc")).toBeNull();
  });

  it("handles zero seconds", () => {
    expect(formatTime(0)).toBe("0s");
  });
});

describe("formatMaxTwo", () => {
  it("keeps only top two units", () => {
    expect(formatMaxTwo(["1d2h3m4s"])).toEqual(["1d 2h"]);
  });

  it("keeps single unit as-is", () => {
    expect(formatMaxTwo(["45s"])).toEqual(["45s"]);
  });

  it("handles two units", () => {
    expect(formatMaxTwo(["3m20s"])).toEqual(["3m 20s"]);
  });

  it("processes multiple items", () => {
    expect(formatMaxTwo(["1h30m10s", "45s"])).toEqual(["1h 30m", "45s"]);
  });
});

describe("setLineChartData", () => {
  it("prepends min and appends max to data", () => {
    const baseData = [
      { x: 1, y: 10 },
      { x: 2, y: 20 },
      { x: 3, y: 30 },
      { x: 4, y: 40 },
      { x: 5, y: 50 },
      { x: 6, y: 60 },
      { x: 7, y: 70 },
      { x: 8, y: 80 },
      { x: 9, y: 90 },
      { x: 10, y: 100 },
    ];
    const result = setLineChartData(baseData, "xMax", "xMin", false, 200, 0);
    // Should have 12 elements (10 original + 2 boundary)
    expect(result).toHaveLength(12);
    expect(result[0].x).toBe("xMin");
    expect(result[11].x).toBe("xMax");
  });

  it("clamps percent values between 0 and 1", () => {
    const baseData = [
      { x: 1, y: 0.1 },
      { x: 2, y: 0.2 },
      { x: 3, y: 0.3 },
      { x: 4, y: 0.4 },
      { x: 5, y: 0.5 },
      { x: 6, y: 0.6 },
      { x: 7, y: 0.7 },
      { x: 8, y: 0.8 },
      { x: 9, y: 0.9 },
      { x: 10, y: 0.95 },
    ];
    const result = setLineChartData(baseData, "xMax", "xMin", true, 1, 0);
    expect(result[0].y).toBeGreaterThanOrEqual(0);
    expect(result[0].y).toBeLessThanOrEqual(1);
    expect(result[11].y).toBeGreaterThanOrEqual(0);
    expect(result[11].y).toBeLessThanOrEqual(1);
  });
});

describe("setBarChartData", () => {
  it("converts x values to formatted date strings", () => {
    const baseData = [{ x: "2023-01-15T12:00:00", y: 100 }];
    const result = setBarChartData(baseData);
    expect(result[0].y).toBe(100);
    expect(result[0].x).toMatch(/2023\/1\/15/);
  });
});
