import type { FeatureData } from './types'

export function geojsonFeatures({
  geojson,
  uniqueIdProperty,
}: {
  geojson: FeatureData[]
  uniqueIdProperty?: string
}): FeatureData[] {
  return geojson.map((f) => {
    const properties = f.properties ?? {}
    const updatedProperties =
      uniqueIdProperty && properties[uniqueIdProperty] != null
        ? properties
        : {
            ...properties,
            id: uniqueIdProperty ? String(properties[uniqueIdProperty]) : '',
          }

    return {
      ...f,
      properties: updatedProperties,
    }
  })
}
