import { Injectable } from '@angular/core';
import { LogLevel, LogProvider } from '@core/logger';
import { UserStateService } from '@swagger/isa';
import { environment } from '../../environments/environment';

@Injectable()
export class IsaLogProvider implements LogProvider {
  constructor(private readonly _infoService: UserStateService) {}

  log(logLevel: LogLevel, message: string, ...optionalParams: any[]): void {
    if (!environment.production && (logLevel === LogLevel.WARN || logLevel === LogLevel.ERROR)) {
      this._infoService
        .UserStateSaveLog({
          logType: logLevel,
          message: message,
          content: JSON.stringify(optionalParams),
        })
        .toPromise()
        .catch(() => {});
    }
  }
}
