import { SegmentSelectOption } from '@komi-app/fans-sdk'
import notification from 'utils/notification'

import { Api } from './api'

export interface IFanGeoAggService {
  getCountries(): Promise<SegmentSelectOption[]>
  getStates(): Promise<SegmentSelectOption[]>
  getCities(): Promise<SegmentSelectOption[]>
}

export default class FanGeoAggService implements IFanGeoAggService {
  constructor(private readonly api: Api) {}

  async getCountries(): Promise<SegmentSelectOption[]> {
    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<SegmentSelectOption[]> {
    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<SegmentSelectOption[]> {
    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
  }
}
