import { Injectable } from '@angular/core';
import { MessageService } from '../../services/message.service';
import { SpinnerService } from '../../services/spinner.service';
import { TranslateService } from '@ngx-translate/core';

@Injectable({
  providedIn: 'root'
})
export class MiscService {

  constructor(private message: MessageService, private spinnerService: SpinnerService,private translateService: TranslateService,) { }

    showLoader(): void {
        this.spinnerService.IsBusy(true);
    }

    hideLoader(): void {
        this.spinnerService.IsBusy(false);
    }

    handleSuccess(message: string): void {
        this.message.Success(message);
    }

    handleError(errorMessage: string, argumentValues: string[] = []): void {
        const translatedErrorMessage = this.translateService.instant(errorMessage, { value1: argumentValues[0], value2: argumentValues[1], value3: argumentValues[2] });
        this.message.Error(translatedErrorMessage);
    }
    getErrorMessageTranslation(key: string) {
        return this.getTranslation(`${key}`);
    }
    getTranslation(key: string, defaultLabel?: string): string {
        if (key == null || key === "" || key == undefined) {
            return defaultLabel ? defaultLabel : "";
        }
        let translated = this.translateService.instant(String(key));
        if (translated == key || translated == "") {
            if (defaultLabel && defaultLabel.length > 0) {
                return defaultLabel;
            }
            else {
                return key;
            }
        } else {
            return translated;
        }
    }
}
