import { describe, expect, it } from "vitest";
import {
  applyPointerOffsetPreviewRect,
  applyVisualOverrides,
  getDragPointerOffset,
} from "./allocationVisualState.js";

describe("allocationVisualState", () => {
  it("keeps only the residual pointer offset after full-day snapping", () => {
    expect(getDragPointerOffset(53, 2, 20)).toBe(13);
    expect(getDragPointerOffset(-53, -2, 20)).toBe(-13);
  });

  it("clamps resize-start preview rectangles to the configured minimum width", () => {
    expect(
      applyPointerOffsetPreviewRect({
        left: 100,
        width: 40,
        mode: "resize-start",
        pointerOffsetX: 35,
        minWidth: 20,
      }),
    ).toEqual({
      left: 120,
      width: 20,
    });
  });

  it("keeps move previews stable when there is no residual pointer offset", () => {
    expect(
      applyPointerOffsetPreviewRect({
        left: 100,
        width: 40,
        mode: "move",
        pointerOffsetX: 0,
        minWidth: 20,
      }),
    ).toEqual({
      left: 100,
      width: 40,
    });
  });

  it("returns the original entries array when no override matches", () => {
    const entries = [
      {
        id: "alloc_1",
        startDate: "2026-04-10",
        endDate: "2026-04-12",
        label: "original",
      },
    ] as const;

    const result = applyVisualOverrides(
      entries,
      new Map([
        [
          "alloc_other",
          {
            startDate: new Date("2026-04-20T00:00:00.000Z"),
            endDate: new Date("2026-04-21T00:00:00.000Z"),
          },
        ],
      ]),
    );

    expect(result).toBe(entries);
  });

  it("clones override dates without mutating unaffected entries", () => {
    const entries = [
      {
        id: "alloc_1",
        startDate: "2026-04-10",
        endDate: "2026-04-12",
        label: "changed",
      },
      {
        id: "alloc_2",
        startDate: "2026-04-15",
        endDate: "2026-04-16",
        label: "unchanged",
      },
    ];
    const override = {
      startDate: new Date("2026-04-20T00:00:00.000Z"),
      endDate: new Date("2026-04-22T00:00:00.000Z"),
    };

    const result = applyVisualOverrides(entries, new Map([["alloc_1", override]]));

    expect(result).not.toBe(entries);
    expect(result[0]).not.toBe(entries[0]);
    expect(result[1]).toBe(entries[1]);
    expect(result[0]?.startDate).toEqual(new Date("2026-04-20T00:00:00.000Z"));
    expect(result[0]?.endDate).toEqual(new Date("2026-04-22T00:00:00.000Z"));
    expect(result[0]?.startDate).not.toBe(override.startDate);
    expect(result[0]?.endDate).not.toBe(override.endDate);
  });
});
