import { describe, expect, it } from 'vitest';
import {
  cardGuideToContainerStyle,
  computeCardGuideBounds,
  computeObjectCoverLayout,
  layoutGuideToVideoBounds,
  videoBoundsToContainerStyle,
} from '../../lib/scanner-video-layout.js';

describe('computeObjectCoverLayout', () => {
  it('computes horizontal crop when video is wider than container', () => {
    const layout = computeObjectCoverLayout(1280, 720, 390, 844);
    expect(layout).not.toBeNull();
    expect(layout.renderedHeight).toBe(844);
    expect(layout.renderedWidth).toBeGreaterThan(390);
    expect(layout.offsetX).toBeLessThan(0);
  });
});

describe('videoBoundsToContainerStyle', () => {
  it('maps full-frame video bounds to full container percentages', () => {
    const layout = computeObjectCoverLayout(1000, 1000, 500, 500);
    const style = videoBoundsToContainerStyle(
      { x: 0, y: 0, width: 1000, height: 1000 },
      layout
    );
    expect(style).toEqual({
      left: '0%',
      top: '0%',
      width: '100%',
      height: '100%',
    });
  });
});

describe('card guide layout', () => {
  it('centers a 5:7 guide within the container', () => {
    const guide = computeCardGuideBounds(400, 800);
    expect(guide.width / guide.height).toBeCloseTo(5 / 7, 5);
    expect(guide.left).toBeGreaterThan(0);
    expect(guide.top).toBeGreaterThan(0);
  });

  it('returns percentage styles for the guide overlay', () => {
    const layout = computeObjectCoverLayout(1280, 720, 390, 844);
    const style = cardGuideToContainerStyle(layout);
    expect(style.left).toMatch(/%$/);
    expect(style.width).toMatch(/%$/);
  });
});

describe('layoutGuideToVideoBounds', () => {
  it('maps the centered guide back to video pixel space', () => {
    const layout = computeObjectCoverLayout(1280, 720, 390, 844);
    const bounds = layoutGuideToVideoBounds(layout);
    expect(bounds.width).toBeGreaterThan(0);
    expect(bounds.height).toBeGreaterThan(0);
    expect(bounds.x).toBeGreaterThanOrEqual(0);
    expect(bounds.y).toBeGreaterThanOrEqual(0);
  });
});
