import { describe, expect, it } from "vitest";
import {
  finalizeAllocationMultiDrag,
  isAllocationMultiSelected,
  startAllocationMultiDrag,
  updateAllocationMultiDrag,
} from "./timelineAllocationMultiDrag.js";

type TestMultiDragState = {
  selectedAllocationIds: string[];
  multiDragDaysDelta: number;
  isMultiDragging: boolean;
  multiDragMode: "move" | "resize-start" | "resize-end";
};

const baseState: TestMultiDragState = {
  selectedAllocationIds: ["alloc-1", "alloc-2"],
  multiDragDaysDelta: 3,
  isMultiDragging: false,
  multiDragMode: "move",
};

describe("timelineAllocationMultiDrag", () => {
  it("requires the allocation to be in a real multi-selection", () => {
    expect(isAllocationMultiSelected(baseState, "alloc-1")).toBe(true);
    expect(isAllocationMultiSelected({ ...baseState, selectedAllocationIds: ["alloc-1"] }, "alloc-1")).toBe(false);
    expect(isAllocationMultiSelected(baseState, "alloc-9")).toBe(false);
  });

  it("starts multi-dragging by resetting stale delta state and preserving selection ids", () => {
    expect(startAllocationMultiDrag(baseState, "resize-end")).toEqual({
      selectedAllocationIds: ["alloc-1", "alloc-2"],
      multiDragDaysDelta: 0,
      isMultiDragging: true,
      multiDragMode: "resize-end",
    });
  });

  it("suppresses same-day delta updates to avoid redundant state churn", () => {
    expect(updateAllocationMultiDrag({ ...baseState, multiDragDaysDelta: 2 }, 2)).toBeNull();
  });

  it("updates the tracked delta when the drag crosses into a new day bucket", () => {
    expect(updateAllocationMultiDrag({ ...baseState, multiDragDaysDelta: 1 }, -2)).toEqual({
      ...baseState,
      multiDragDaysDelta: -2,
    });
  });

  it("finalizes multi-dragging by clearing transient drag state while keeping the selection", () => {
    expect(finalizeAllocationMultiDrag({ ...baseState, isMultiDragging: true, multiDragMode: "resize-start" })).toEqual({
      selectedAllocationIds: ["alloc-1", "alloc-2"],
      multiDragDaysDelta: 0,
      isMultiDragging: false,
      multiDragMode: "resize-start",
    });
  });
});
