import {ErrorHandler, Injectable, Injector} from '@angular/core';
import {BaseErrorHandler} from "@webbpm/base-package";

//todo: will be used after angular update in dev mode
@Injectable({providedIn: 'root'})
export class GlobalErrorHandler extends BaseErrorHandler implements ErrorHandler {

  constructor(injector: Injector) {
    super(injector);
  }

  handleError(error) {
    const chunkFailedMessage = /Loading chunk [\d]+ failed/;

    if (chunkFailedMessage.test(error.message)) {
      window.location.reload();
    }
    else if (!this.isPreviewPage()) {
      super.handleError(error);
    }
  }

  internalHandleError(error) {
    if (this.isPreviewPage()) {
      return;
    }
    else {
      super.internalHandleError(error);
    }
  }

  private isPreviewPage() {
    return window.location.hash.includes("webbpm-preview");
  }
}
