"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.SettingsPageStateBIReporter = 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 SettingsPageStateBIReporter {
  constructor(params) {
    (0, _defineProperty2.default)(this, "settingsPageState", void 0);
    (0, _defineProperty2.default)(this, "reportBI", void 0);
    (0, _defineProperty2.default)(this, "appLoaded", void 0);
    this.settingsPageState = params.settingsPageState;
    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.settingsPageState;
    const startTime = performance.now();
    const disposers = [(0, _bexCore.addEventListener)(events, 'save', (0, _mobx.action)(() => {
      events.once('saveError', err => {
        this.reportSaveError(err, startTime);
      });
    })), this.appLoaded = this.reportOnLoadStart(startTime)];
    return () => {
      disposers.forEach(disposer => disposer());
    };
  }
  reportButtonClick({
    ctaName,
    isValid
  }) {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoPageCtaClicked)({
      ctaName,
      ctaType: 'Main',
      location: 'Settings page header',
      isValid
    }));
  }
  reportSaveTryAgain() {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoTryAgainClicked)({
      actionName: 'Saving Settings',
      loaction: 'toast'
    }));
  }
  reportSaveError(e, startTime) {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoApiRequestDataLoadEnd)({
      errorType: this._getErrorType(e),
      duration: performance.now() - startTime
    }));
  }
  reportOnBeforeUnload() {
    const values = this.settingsPageState.form.getValues();
    const fields = Object.keys(values);
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoPageDiscardChangesBeforeSave)({
      numOfFields: fields.length,
      numOfActions: fields.reduce((prev, key) => {
        var _this$settingsPageSta;
        return prev + +((_this$settingsPageSta = this.settingsPageState.form.getFieldState(key)) == null ? void 0 : _this$settingsPageSta.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 {
      route: window.location.pathname,
      url: window.location.href
    };
  }
}
exports.SettingsPageStateBIReporter = SettingsPageStateBIReporter;
//# sourceMappingURL=SettingsPageStateBIReporter.js.map