import assert from 'node:assert/strict';
import test from 'node:test';

import { containMediaLayout, containMediaRect } from './containMediaRect';

void test('containMediaRect: letterboxes 16:9 into square host', () => {
  const r = containMediaRect({
    hostW: 400,
    hostH: 400,
    mediaW: 1920,
    mediaH: 1080,
    scale: 1,
    ox: 0.5,
    oy: 0.5,
  });
  assert.ok(r);
  assert.ok(Math.abs(r!.w - 400) < 0.01);
  assert.ok(Math.abs(r!.h - (400 * 1080) / 1920) < 0.01);
  assert.ok(Math.abs(r!.x - 0) < 0.01);
  assert.ok(r!.y > 0);
});

void test('containMediaRect: zoom grows rect around ox/oy', () => {
  const base = containMediaRect({
    hostW: 800,
    hostH: 450,
    mediaW: 800,
    mediaH: 450,
    scale: 1,
    ox: 0.5,
    oy: 0.5,
  });
  const zoomed = containMediaRect({
    hostW: 800,
    hostH: 450,
    mediaW: 800,
    mediaH: 450,
    scale: 2,
    ox: 0.5,
    oy: 0.5,
  });
  assert.ok(base && zoomed);
  assert.ok(zoomed!.w > base!.w);
  assert.ok(zoomed!.x < base!.x);
});

void test('containMediaLayout: 90° swaps fit axes and element stays unrotated size', () => {
  const layout = containMediaLayout({
    hostW: 400,
    hostH: 400,
    mediaW: 1920,
    mediaH: 1080,
    scale: 1,
    ox: 0.5,
    oy: 0.5,
    rotationDeg: 90,
  });
  assert.ok(layout);
  // After 90°, layout AABB is portrait 1080x1920 fitted into square → height fills.
  assert.ok(Math.abs(layout!.contentRect.h - 400) < 0.01);
  assert.ok(Math.abs(layout!.contentRect.w - (400 * 1080) / 1920) < 0.01);
  assert.ok(Math.abs(layout!.elementW - layout!.contentRect.h) < 0.01);
  assert.ok(Math.abs(layout!.elementH - layout!.contentRect.w) < 0.01);
});
