"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.EntityPageStateBIReporter = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _bi = require("@wix/bex-core/bi");
// withoutDefaults

class EntityPageStateBIReporter {
  constructor(params) {
    (0, _defineProperty2.default)(this, "entityPageState", void 0);
    (0, _defineProperty2.default)(this, "reportBI", void 0);
    (0, _defineProperty2.default)(this, "startTime", performance.now());
    (0, _defineProperty2.default)(this, "appLoaded", void 0);
    this.entityPageState = params.entityPageState;
    this.reportBI = params.reportBI;
  }
  _getErrorType(e) {
    const isNetworkError = (e == null ? void 0 : e.code) === 'ERR_NETWORK';
    return isNetworkError ? 'Network error' : 'Technical issue';
  }
  init() {
    const {
      events
    } = this.entityPageState;
    const disposers = [(0, _bexCore.addEventListener)(events, 'save', (0, _mobx.action)(() => {
      events.once('saveError', err => {
        this.reportSaveError(err, this.startTime);
      });
    })), this.appLoaded = this.reportOnLoadStart(this.startTime)];
    return () => {
      disposers.forEach(disposer => disposer());
    };
  }
  reportButtonClick({
    ctaName,
    isValid,
    additionalInfo
  }) {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoPageCtaClicked)({
      ctaName,
      ctaType: 'Main',
      location: 'Entity page header',
      isValid,
      additionalInfo
    }));
  }
  reportSaveTryAgain() {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoTryAgainClicked)({
      actionName: 'Saving Entity',
      loaction: 'toast'
    }));
  }
  reportSaveError(e, startTime) {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoApiRequestDataLoadEnd)({
      errorType: this._getErrorType(e),
      duration: performance.now() - startTime
    }));
  }
  reportOnBeforeUnload() {
    const values = this.entityPageState.form.getValues();
    const fields = Object.keys(values);
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoPageDiscardChangesBeforeSave)({
      numOfFields: fields.length,
      numOfActions: fields.reduce((prev, key) => {
        var _this$entityPageState;
        return prev + +((_this$entityPageState = this.entityPageState.form.getFieldState(key)) == null ? void 0 : _this$entityPageState.isDirty);
      }, 0)
    }));
  }
  reportOnLoadStart(startTime) {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.loadStart)({
      ...this._commonDynamicBiParams()
    }));
    return () => {
      this.reportOnLoadEnd(Math.round(performance.now() - startTime));
    };
  }
  reportOnLoadEnd(endTime) {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.loadEnd)({
      ...this._commonDynamicBiParams(),
      loadingTime: endTime
    }));
  }
  _commonDynamicBiParams() {
    return {
      url: window.location.href,
      route: window.location.pathname,
      routerUsage: this.entityPageState._withinRouter
    };
  }
}
exports.EntityPageStateBIReporter = EntityPageStateBIReporter;
//# sourceMappingURL=EntityPageStateBIReporter.js.map