"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CustomFieldModalBiReporter = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _util = require("../util");
var _v = require("@wix/bex-utils/@wix/bi-logger-os-data/v2");
var _mobx = require("mobx");
var _formatPermissionsForBi = require("./formatPermissionsForBi");
class CustomFieldModalBiReporter {
  constructor(_params) {
    (0, _defineProperty2.default)(this, "customFieldModalState", void 0);
    (0, _defineProperty2.default)(this, "formSnapshot", null);
    (0, _defineProperty2.default)(this, "reportTypeFormFieldChange", void 0);
    (0, _defineProperty2.default)(this, "reportNameFormFieldChange", void 0);
    (0, _defineProperty2.default)(this, "reportKeyFormFieldChange", void 0);
    (0, _defineProperty2.default)(this, "reportPermissionFormFieldChange", void 0);
    (0, _defineProperty2.default)(this, "reportAddCustomField", ({
      origin,
      customOrigin
    }) => {
      this.reportBi((0, _util.withoutDefaults)(_v.cairoDataExAddCustomField)({
        origin,
        customOrigin
      }));
    });
    (0, _defineProperty2.default)(this, "reportAdvancedSettingsClick", actionName => {
      this.reportBi((0, _util.withoutDefaults)(_v.cairoDataExCustomFieldAdvancesSettings)({
        origin: this.origin,
        actionName
      }));
    });
    (0, _defineProperty2.default)(this, "reportLearnMoreClick", () => {
      this.reportBi((0, _util.withoutDefaults)(_v.cairoLearnMore)({
        origin: this.origin
      }));
    });
    (0, _defineProperty2.default)(this, "reportModalEndProcess", params => {
      const {
        endType
      } = params;
      const {
        $: {
          name,
          type,
          key
        }
      } = this.form;
      const {
        customFieldModalState: {
          origin,
          customOrigin
        }
      } = this;
      this.reportBi((0, _util.withoutDefaults)(_v.cairoDataExCustomFieldEndProcess)({
        actionName: this.mode === 'new' ? 'Add' : 'Edit',
        endType,
        fieldName: name.value,
        fieldType: type.value,
        key: key.value,
        numAdded: this.customFieldModalState.fieldCount(),
        changedFields: this.getBiChangedFields(),
        permissions: this.getBiPermissions(),
        origin,
        customOrigin
      }));
    });
    (0, _defineProperty2.default)(this, "reportSubmitFailure", params => {
      const {
        customFieldModalState
      } = this;
      const {
        type,
        name,
        key
      } = customFieldModalState.getFormValues();
      this.reportBi((0, _util.withoutDefaults)(_v.cairoDataExtensionUnsuccessfulUpdateInServer)({
        actionName: this.mode === 'new' ? 'Add' : 'Edit',
        errorType: params.errorType,
        origin: customFieldModalState.origin,
        permissions: this.getBiPermissions(),
        fieldType: type,
        fieldName: name,
        key
      }));
    });
    this.customFieldModalState = _params.customFieldModalState;
    this.reportTypeFormFieldChange = this.createReportFormFieldChange({
      fieldName: 'Type',
      fieldValue: () => this.form.$.type.value
    });
    this.reportNameFormFieldChange = this.createReportFormFieldChange({
      fieldName: 'Name',
      fieldValue: () => this.form.$.name.value
    });
    this.reportKeyFormFieldChange = this.createReportFormFieldChange({
      fieldName: 'Key',
      fieldValue: () => this.form.$.key.value
    });
    this.reportPermissionFormFieldChange = this.createReportFormFieldChange({
      fieldName: 'Permission',
      fieldValue: () => this.getBiPermissions()
    });
  }
  get reportBi() {
    return this.customFieldModalState.reportBi;
  }
  get form() {
    return this.customFieldModalState.form;
  }
  get mode() {
    return this.customFieldModalState.mode;
  }
  get origin() {
    return this.mode === 'new' ? 'Add Custom Field Modal' : 'Edit Custom Field Modal';
  }
  createReportFormFieldChange({
    fieldName,
    fieldValue
  }) {
    const {
      lodash: {
        debounce
      }
    } = this.customFieldModalState.container;
    return debounce(() => this.reportBi((0, _util.withoutDefaults)(_v.cairoChangesBeforeApply)({
      feature: this.mode === 'new' ? 'Add Custom Field' : 'Edit Custom Field',
      fieldName,
      fieldValue: fieldValue(),
      isClearButton: false
    })), 1500, {
      leading: false,
      trailing: true
    });
  }
  getBiChangedFields() {
    const prevValues = this.formSnapshot;
    if (this.mode === 'new' || !prevValues) {
      return undefined;
    }
    const nextValues = this.customFieldModalState.getFormValues();
    const changes = [];
    if (prevValues.name !== nextValues.name) {
      changes.push('Name');
    }
    if (prevValues.readApps !== nextValues.readApps || prevValues.readUsers !== nextValues.readUsers || prevValues.writeApps !== nextValues.writeApps || prevValues.writeUsers !== nextValues.writeUsers) {
      changes.push('Permission');
    }
    if (changes.length !== 0) {
      return changes.join(',');
    }
    return 'No changes';
  }
  getBiPermissions() {
    return (0, _formatPermissionsForBi.formatPermissionsForBi)(this.customFieldModalState.getFormValues());
  }
  init() {
    const {
      customFieldModalState: {
        form
      }
    } = this;
    const disposes = [(0, _mobx.reaction)(() => form.$.name.value, () => {
      this.reportNameFormFieldChange();
    }), (0, _mobx.reaction)(() => form.$.type.value, () => {
      this.reportTypeFormFieldChange();
    }), (0, _mobx.reaction)(() => ({
      readUsers: form.$.readUsers.value,
      readApps: form.$.readApps.value,
      writeUsers: form.$.writeUsers.value,
      writeApps: form.$.writeApps.value
    }), () => {
      this.reportPermissionFormFieldChange();
    })];
    return () => {
      disposes.forEach(disposer => {
        disposer == null || disposer();
      });
    };
  }
}
exports.CustomFieldModalBiReporter = CustomFieldModalBiReporter;
//# sourceMappingURL=CustomFieldModalBiReporter.js.map