import ApiRequestService from "./apiRequestService";
import UrlUtils from "~/services/utils/urlUtils";
import HydraDenormalizer from "~/services/data/normalizer/hydraDenormalizer";
import {Enum} from "~/types/data.d";
import {VueI18n} from "vue-i18n";

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 } = await HydraDenormalizer.denormalize(response)

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

        return enum_
    }
}

export default EnumManager
