import type { VueI18n } from 'vue-i18n'
import ApiRequestService from './apiRequestService'
import UrlUtils from '~/services/utils/urlUtils'
import HydraNormalizer from '~/services/data/normalizer/hydraNormalizer'
import type { Enum } from '~/types/data.d'

class EnumManager {
  private apiRequestService: ApiRequestService
  private i18n: VueI18n

  public constructor(apiRequestService: ApiRequestService, i18n: VueI18n) {
    this.apiRequestService = apiRequestService
    this.i18n = i18n
  }

  public async fetch(enumName: string): Promise<Enum> {
    const url = UrlUtils.join('api', 'enum', enumName)

    const response = await this.apiRequestService.get(url)

    const { data } = HydraNormalizer.denormalize(response)

    const enum_: Enum = []
    for (const key in data.items) {
      if (Object.prototype.hasOwnProperty.call(data.items, key)) {
        enum_.push({ value: key, label: this.i18n.t(data.items[key]) })
      }
    }

    return enum_
  }
}

export default EnumManager
