/*
 * Copyright 2017-2022 VMware, Inc. All rights reserved. VMware Confidential
 */

import { Injectable } from "@angular/core";
import { Observable, of, throwError as _throw } from "rxjs";
import { ExtensionLoaderStep, ExternalExtensionLoaderState } from "../extension-loader.model";
import { ExternalExtensionLoaderErrorObservable } from "./external-extension-loader-error";
import { TranslationService } from "@vcd/i18n";

@Injectable()
export class RegisterExternalExtensionTranslationsStep implements ExtensionLoaderStep<ExternalExtensionLoaderState> {
    constructor(private translationService: TranslationService) {}

    /**
     * Registers the given translations.
     * State inputs: spec, manifest, translations
     * State outputs: none
     */
    perform(state: ExternalExtensionLoaderState): Observable<ExternalExtensionLoaderState> {
        try {
            console.log(`[${state.spec.id} (${state.manifest.name})] Registering i18n`);
            this.translationService.registerTranslations(state.translations);
            console.log(`[${state.spec.id} (${state.manifest.name})] i18n registered`);
            return of(state);
        } catch (err) {
            console.error(`[${state?.spec?.id} (${state?.manifest?.name})] Could not register i18n`, err);
            return new ExternalExtensionLoaderErrorObservable();
        }
    }
}
