import { describe, expect, it } from 'vitest';

import { MAP_MIN_ZOOM } from './consts';
import { boundsToCenterZoom } from './fit-bounds';

describe('boundsToCenterZoom', () => {
  it('centers on the middle of the box', () => {
    const target = boundsToCenterZoom({ south: 51.4, north: 51.6, west: -0.3, east: 0.1 });
    expect(target.lat).toBeCloseTo(51.5, 5);
    expect(target.lng).toBeCloseTo(-0.1, 5);
  });

  it('zooms close for a small box and far out for a country-sized box', () => {
    const street = boundsToCenterZoom({ south: 51.5, north: 51.51, west: -0.11, east: -0.1 });
    const england = boundsToCenterZoom({ south: 50.0, north: 55.5, west: -5.7, east: 1.8 });
    expect(street.zoom).toBeGreaterThan(england.zoom);
    expect(england.zoom).toBeGreaterThanOrEqual(MAP_MIN_ZOOM);
    // Greater London-ish box should land in a sensible city-scale zoom range
    const london = boundsToCenterZoom({ south: 51.44, north: 51.59, west: -0.31, east: 0.05 });
    expect(london.zoom).toBeGreaterThan(8);
    expect(london.zoom).toBeLessThan(12);
  });

  it('caps zoom-in for degenerate (single point) boxes', () => {
    const point = boundsToCenterZoom({ south: 51.5, north: 51.5, west: -0.1, east: -0.1 });
    expect(point.zoom).toBeLessThanOrEqual(13);
  });

  it('tolerates swapped corners', () => {
    const target = boundsToCenterZoom({ south: 51.6, north: 51.4, west: 0.1, east: -0.3 });
    expect(target.lat).toBeCloseTo(51.5, 5);
    expect(target.lng).toBeCloseTo(-0.1, 5);
    expect(Number.isFinite(target.zoom)).toBe(true);
  });
});
