import {
  computeCandidateIndex,
  createPendingVote,
  getBrowseAxis,
  getBrowseCueLabel,
  hasSelectionChanged
} from "./gestureController";

describe("gestureController", () => {
  it("keeps the baseline candidate when the drag is small", () => {
    expect(
      computeCandidateIndex({
        baselineIndex: 1,
        dragDistance: 80,
        totalCandidates: 4,
        dragSpan: 360
      })
    ).toBe(1);
  });

  it("snaps once the drag crosses the 25 percent threshold", () => {
    expect(
      computeCandidateIndex({
        baselineIndex: 1,
        dragDistance: 90,
        totalCandidates: 4,
        dragSpan: 360
      })
    ).toBe(2);
    expect(
      computeCandidateIndex({
        baselineIndex: 1,
        dragDistance: -90,
        totalCandidates: 4,
        dragSpan: 360
      })
    ).toBe(0);
  });

  it("clamps the selected index within the candidate list", () => {
    expect(
      computeCandidateIndex({
        baselineIndex: 0,
        dragDistance: -600,
        totalCandidates: 3,
        dragSpan: 300
      })
    ).toBe(0);
  });

  it("switches browse direction by viewport mode", () => {
    expect(getBrowseAxis("mobile-portrait")).toBe("horizontal");
    expect(getBrowseAxis("mobile-landscape")).toBe("vertical");
    expect(getBrowseAxis("desktop")).toBe("vertical");
    expect(getBrowseCueLabel("mobile-portrait")).toBe("Swipe left or right");
    expect(getBrowseCueLabel("desktop")).toBe("Swipe up or down");
  });

  it("detects whether the selection changed", () => {
    expect(hasSelectionChanged("a", "b")).toBe(true);
    expect(hasSelectionChanged("a", "a")).toBe(false);
    expect(hasSelectionChanged(null, "b")).toBe(false);
  });

  it("creates a pending vote payload with stable ids", () => {
    const vote = createPendingVote({
      lectureId: "lecture-1",
      slideId: "slide-1",
      previousGlitchSlideId: "a",
      selectedGlitchSlideId: "b",
      startedAtSec: 12.4
    });

    expect(vote).toMatchObject({
      lectureId: "lecture-1",
      slideId: "slide-1",
      previousGlitchSlideId: "a",
      selectedGlitchSlideId: "b",
      startedAtSec: 12.4
    });
    expect(vote.createdAtIso).toEqual(expect.any(String));
  });
});
