import { describe, expect, it } from 'vitest'
import type { Geometry } from 'geojson'
import { getCentroid } from './get-centroid'

describe('getCentroid', () => {
  it('should calculate centroid for a Point', () => {
    const geometry: Geometry = {
      type: 'Point',
      coordinates: [10, 20],
    }

    const result = getCentroid(geometry)

    expect(result).toEqual([10, 20])
  })

  it('should calculate centroid for a LineString', () => {
    const geometry: Geometry = {
      type: 'LineString',
      coordinates: [
        [0, 0],
        [10, 10],
      ],
    }

    const result = getCentroid(geometry)

    expect(result).toBeDefined()
    expect(result).toHaveLength(2)
  })

  it('should calculate centroid for a Polygon', () => {
    const geometry: Geometry = {
      type: 'Polygon',
      coordinates: [
        [
          [0, 0],
          [0, 10],
          [10, 10],
          [10, 0],
          [0, 0],
        ],
      ],
    }

    const result = getCentroid(geometry)

    expect(result).toBeDefined()
    expect(result).toEqual([5, 5])
  })

  it('should calculate centroid for a MultiPoint', () => {
    const geometry: Geometry = {
      type: 'MultiPoint',
      coordinates: [
        [0, 0],
        [10, 10],
      ],
    }

    const result = getCentroid(geometry)

    expect(result).toBeDefined()
    expect(result).toHaveLength(2)
  })

  it('should calculate centroid for a MultiLineString', () => {
    const geometry: Geometry = {
      type: 'MultiLineString',
      coordinates: [
        [
          [0, 0],
          [5, 5],
        ],
        [
          [5, 5],
          [10, 10],
        ],
      ],
    }

    const result = getCentroid(geometry)

    expect(result).toBeDefined()
    expect(result).toHaveLength(2)
  })

  it('should calculate centroid for a MultiPolygon', () => {
    const geometry: Geometry = {
      type: 'MultiPolygon',
      coordinates: [
        [
          [
            [0, 0],
            [0, 5],
            [5, 5],
            [5, 0],
            [0, 0],
          ],
        ],
        [
          [
            [5, 5],
            [5, 10],
            [10, 10],
            [10, 5],
            [5, 5],
          ],
        ],
      ],
    }

    const result = getCentroid(geometry)

    expect(result).toBeDefined()
    expect(result).toHaveLength(2)
  })

  it('should return undefined for a GeometryCollection', () => {
    const geometry: Geometry = {
      type: 'GeometryCollection',
      geometries: [
        {
          type: 'Point',
          coordinates: [0, 0],
        },
      ],
    }

    const result = getCentroid(geometry)

    expect(result).toBeUndefined()
  })
})
