import { describe, expect, it } from "vitest";
import { getTouchPoint, resolveTouchDragDecision } from "./timelineTouch.js";

describe("timelineTouch", () => {
  it("falls back from active touches to changedTouches and then zeroes", () => {
    expect(
      getTouchPoint({
        touches: [{ clientX: 12, clientY: 18 }],
        changedTouches: [{ clientX: 40, clientY: 55 }],
      }),
    ).toEqual({ clientX: 12, clientY: 18 });

    expect(
      getTouchPoint({
        touches: [],
        changedTouches: [{ clientX: 40, clientY: 55 }],
      }),
    ).toEqual({ clientX: 40, clientY: 55 });

    expect(
      getTouchPoint({
        touches: [],
        changedTouches: [],
      }),
    ).toEqual({ clientX: 0, clientY: 0 });
  });

  it("stays undecided and ignores tiny movements below the threshold", () => {
    expect(
      resolveTouchDragDecision(
        { x: 100, y: 100, decided: false },
        { clientX: 106, clientY: 107 },
      ),
    ).toEqual({
      nextState: { x: 100, y: 100, decided: false },
      shouldHandleDrag: false,
    });
  });

  it("lets vertical scrolling win once movement is mostly vertical", () => {
    expect(
      resolveTouchDragDecision(
        { x: 100, y: 100, decided: false },
        { clientX: 106, clientY: 118 },
      ),
    ).toEqual({
      nextState: { x: 100, y: 100, decided: true },
      shouldHandleDrag: false,
    });
  });

  it("switches to drag handling once horizontal movement wins", () => {
    expect(
      resolveTouchDragDecision(
        { x: 100, y: 100, decided: false },
        { clientX: 118, clientY: 106 },
      ),
    ).toEqual({
      nextState: { x: 100, y: 100, decided: true },
      shouldHandleDrag: true,
    });
  });

  it("keeps handling drag after the decision was already made", () => {
    expect(
      resolveTouchDragDecision(
        { x: 100, y: 100, decided: true },
        { clientX: 101, clientY: 140 },
      ),
    ).toEqual({
      nextState: { x: 100, y: 100, decided: true },
      shouldHandleDrag: true,
    });
  });

});
