import {describe, expect, test} from 'bun:test'

import {
  bboxContains,
  padBbox,
  regionToBbox,
  zoomBand,
  type ClusterRegion,
} from '@/lib/bbox'

const region = (
  latitude: number,
  longitude: number,
  latitudeDelta: number,
  longitudeDelta: number,
): ClusterRegion => ({
  center: {latitude, longitude},
  latitudeDelta,
  longitudeDelta,
  zoom: 10,
})

describe('regionToBbox', () => {
  test('spans half the delta on each side of the center', () => {
    expect(regionToBbox(region(10, 20, 2, 4))).toEqual([
      18, 9, 22, 11,
    ])
  })

  test('clamps to valid coordinate ranges', () => {
    expect(regionToBbox(region(89, 179, 10, 10))).toEqual([
      174, 84, 180, 90,
    ])
  })
})

describe('padBbox', () => {
  test('grows each side by a fraction of the span', () => {
    expect(padBbox([0, 0, 10, 10], 0.1)).toEqual([-1, -1, 11, 11])
  })

  test('clamps padded edges', () => {
    expect(padBbox([-179, -89, 179, 89], 0.5)).toEqual([
      -180, -90, 180, 90,
    ])
  })
})

describe('bboxContains', () => {
  test('true when inner fits fully inside outer', () => {
    expect(bboxContains([0, 0, 10, 10], [1, 1, 9, 9])).toBe(true)
    expect(bboxContains([0, 0, 10, 10], [1, 1, 11, 9])).toBe(false)
    expect(bboxContains([0, 0, 10, 10], [0, 0, 10, 10])).toBe(true)
  })
})

describe('zoomBand', () => {
  test('buckets zoom to integer steps', () => {
    expect(zoomBand(12.9)).toBe(12)
    expect(zoomBand(13)).toBe(13)
  })
})
