import assert from "node:assert/strict"
import { describe, it } from "node:test"
import {
  buildPageSnapLines,
  snapMoveRect,
  snapResizeRect,
} from "./docs-graphic-snap.ts"

const baseCtx = {
  pageWidth: 800,
  pageHeight: 1100,
  margins: { top: 96, right: 96, bottom: 96, left: 96 },
  otherRects: [{ x: 200, y: 300, width: 120, height: 80 }],
}

describe("docs-graphic-snap", () => {
  it("includes page edges, center, and margins in snap lines", () => {
    const lines = buildPageSnapLines(baseCtx)
    assert.ok(lines.x.includes(0))
    assert.ok(lines.x.includes(96))
    assert.ok(lines.x.includes(400))
    assert.ok(lines.x.includes(704))
    assert.ok(lines.x.includes(800))
    assert.ok(lines.y.includes(96))
    assert.ok(lines.y.includes(550))
  })

  it("snaps move to page center", () => {
    const rect = { x: 358, y: 100, width: 80, height: 60 }
    const { rect: snapped, guides } = snapMoveRect(rect, baseCtx, 8)
    assert.equal(snapped.x, 360)
    assert.ok(guides.some((g) => g.axis === "x" && g.position === 400))
  })

  it("snaps move to another graphic left edge", () => {
    const rect = { x: 198, y: 300, width: 50, height: 40 }
    const { rect: snapped, guides } = snapMoveRect(rect, baseCtx, 6)
    assert.equal(snapped.x, 200)
    assert.ok(guides.some((g) => g.axis === "x" && g.position === 200))
  })

  it("snaps resize east edge to margin", () => {
    const rect = { x: 500, y: 200, width: 198, height: 100 }
    const { rect: snapped } = snapResizeRect("e", rect, baseCtx, 8)
    assert.equal(snapped.width, 204)
    assert.equal(snapped.x + snapped.width, 704)
  })

  it("snaps resize north edge to page top", () => {
    const rect = { x: 120, y: 4, width: 80, height: 120 }
    const { rect: snapped } = snapResizeRect("n", rect, baseCtx, 8)
    assert.equal(snapped.y, 0)
    assert.equal(snapped.height, 124)
  })
})
