import { describe, test, expect } from "vitest"
import { computeFit, parseViewBox } from "../src/fit"

describe("fit — contain", () => {
  test("square content into wider slot centers horizontally", () => {
    const r = computeFit("contain", 100, 100, 400, 200, 100, 100)
    expect(r.width).toBe(200)
    expect(r.height).toBe(200)
    expect(r.x).toBe(200)
    expect(r.y).toBe(100)
    expect(r.viewBox).toBe("0 0 100 100")
  })

  test("square content into taller slot centers vertically", () => {
    const r = computeFit("contain", 100, 100, 200, 400, 100, 100)
    expect(r.width).toBe(200)
    expect(r.height).toBe(200)
    expect(r.x).toBe(100)
    expect(r.y).toBe(200)
  })

  test("horizontal content into square slot fits by width", () => {
    const r = computeFit("contain", 0, 0, 200, 200, 400, 100)
    expect(r.width).toBe(200)
    expect(r.height).toBe(50)
    expect(r.x).toBe(0)
    expect(r.y).toBe(75)
  })
})

describe("fit — cover", () => {
  test("square content into wide slot fills height", () => {
    const r = computeFit("cover", 0, 0, 400, 200, 100, 100)
    expect(r.width).toBe(400)
    expect(r.height).toBe(400)
    expect(r.x).toBe(0)
    expect(r.y).toBe(-100)
  })
})

describe("fit — stretch", () => {
  test("stretches to slot dimensions ignoring aspect", () => {
    const r = computeFit("stretch", 10, 20, 300, 150, 100, 100)
    expect(r.x).toBe(10)
    expect(r.y).toBe(20)
    expect(r.width).toBe(300)
    expect(r.height).toBe(150)
    expect(r.viewBox).toBe("0 0 100 100")
  })
})

describe("fit — parseViewBox", () => {
  test("extracts from viewBox attribute", () => {
    expect(parseViewBox('<svg viewBox="0 0 24 24">')).toEqual({ width: 24, height: 24 })
  })

  test("extracts from width/height attributes", () => {
    expect(parseViewBox('<svg width="48" height="48">')).toEqual({ width: 48, height: 48 })
  })

  test("defaults to 24x24 when nothing found", () => {
    expect(parseViewBox("<svg></svg>")).toEqual({ width: 24, height: 24 })
  })
})