import citiesDataset from '@/data/morocco-cities-rgph-2024.json'

export type SupportedCityLocale = 'en' | 'fr' | 'ar'

interface LocalizedName {
  en: string
  fr: string
  ar: string
}

interface MoroccanCity {
  id: string
  name: LocalizedName
  province: LocalizedName
}

interface MoroccanCitiesDataset {
  cities: MoroccanCity[]
}

export function getMoroccanCityOptions(locale: string) {
  const resolvedLocale: SupportedCityLocale = locale === 'fr' || locale === 'ar' ? locale : 'en'
  const cityNames = (citiesDataset as MoroccanCitiesDataset).cities.map((city) => city.name[resolvedLocale])

  return Array.from(new Set(cityNames)).sort((a, b) => a.localeCompare(b, resolvedLocale))
}

export function mergeLocationOptions(options: string[], selectedLocations: string[]) {
  const selected = selectedLocations.map((location) => location.trim()).filter(Boolean)
  return Array.from(new Set([...selected, ...options]))
}
