import { SegmentGeolocalisation } from 'types/segments'
import notification from 'utils/notification'

import { Api } from './api'

export default class SequenceLocationService {
  constructor(private readonly api: Api) {}

  async getCountries(): Promise<SegmentGeolocalisation[]> {
    const res = await this.api.getSegmentLocationCountries()

    if (!res) {
      notification.error({ message: 'Something went wrong' })
      return []
    }
    if (!('ok' in res)) {
      notification.error({ message: res.message })
      return []
    }

    return res.response.countries
  }

  async getStates(): Promise<SegmentGeolocalisation[]> {
    const res = await this.api.getSegmentLocationStates()

    if (!res) {
      notification.error({ message: 'Something went wrong' })
      return []
    }
    if (!('ok' in res)) {
      notification.error({ message: res.message })
      return []
    }

    return res.response.states
  }

  async getCities(): Promise<SegmentGeolocalisation[]> {
    const res = await this.api.getSegmentLocationCities()

    if (!res) {
      notification.error({ message: 'Something went wrong' })
      return []
    }
    if (!('ok' in res)) {
      notification.error({ message: res.message })
      return []
    }

    return res.response.cities
  }
}
