import {ApplicationSettingsProvider, AppVersionService} from "@webbpm/base-package";
import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {MfeConfigurationProvider} from "../provider/mfe-configuration.provider";
import {joinPath} from "../../../mfe-app-tools";

@Injectable({providedIn:'root'})
export class MfeAppVersionService extends AppVersionService {

  load(): Promise<any> {
    let http: HttpClient = this['http'];
    let url = joinPath(
        MfeConfigurationProvider.MFE_BASE_URL,
        ApplicationSettingsProvider.RESOURCES_PATH,
        'app.version'
    )
    return http
        .get(url, {responseType: 'text'})
        .toPromise()
        .then(version => {
          this['appVersion'] = version;
          console.log(`Business Metrics application version = ${version}`);
        })
        .catch((error: any): any => {
          console.error('Business Metrics file "app.version" could not be read');
          return Promise.reject(error);
        });
  }
}
