"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.experienceConfig = exports.RELIABILITY_INTERVAL = exports.ExperienceStore = exports.EditorExperience = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _ufo = require("@atlaskit/ufo");
var experienceConfig = exports.experienceConfig = {
  type: _ufo.ExperienceTypes.Operation,
  performanceType: _ufo.ExperiencePerformanceTypes.Custom,
  platform: {
    component: 'editor'
  }
};
var EditorExperience = exports.EditorExperience = /*#__PURE__*/function (EditorExperience) {
  EditorExperience["loadEditor"] = "load";
  EditorExperience["typing"] = "type";
  EditorExperience["interaction"] = "interact";
  EditorExperience["editSession"] = "edit-session";
  return EditorExperience;
}({});
var RELIABILITY_INTERVAL = exports.RELIABILITY_INTERVAL = 30000;
var ExperienceStore = exports.ExperienceStore = /*#__PURE__*/function () {
  function ExperienceStore() {
    (0, _classCallCheck2.default)(this, ExperienceStore);
    this.experiences = new Map();
    for (var _i = 0, _Object$values = Object.values(EditorExperience); _i < _Object$values.length; _i++) {
      var experienceId = _Object$values[_i];
      var experience = new _ufo.UFOExperience(experienceId, experienceConfig);
      this.experiences.set(experienceId, experience);
    }
  }
  (0, _createClass2.default)(ExperienceStore, [{
    key: "get",
    value: function get(experienceId) {
      return this.experiences.get(experienceId);
    }
  }, {
    key: "getActive",
    value: function getActive(experienceId) {
      var experience = this.experiences.get(experienceId);
      if (!(experience !== null && experience !== void 0 && experience.state.final)) {
        return experience;
      }
    }
  }, {
    key: "getAll",
    value: function getAll() {
      return Array.from(this.experiences.values());
    }
  }, {
    key: "start",
    value: function start(experienceId, startTime) {
      var _this$get;
      (_this$get = this.get(experienceId)) === null || _this$get === void 0 || _this$get.start(startTime);
    }
  }, {
    key: "addMetadata",
    value: function addMetadata(experienceId, metadata) {
      var _this$get2;
      (_this$get2 = this.get(experienceId)) === null || _this$get2 === void 0 || _this$get2.addMetadata(metadata);
    }
  }, {
    key: "mark",
    value: function mark(experienceId, _mark, value) {
      var _this$get3;
      (_this$get3 = this.get(experienceId)) === null || _this$get3 === void 0 || _this$get3.mark(_mark, value);
    }
  }, {
    key: "success",
    value: function success(experienceId, metadata) {
      var _this$getActive;
      return (_this$getActive = this.getActive(experienceId)) === null || _this$getActive === void 0 ? void 0 : _this$getActive.success({
        metadata: metadata
      });
    }
  }, {
    key: "fail",
    value: function fail(experienceId, metadata) {
      var _this$getActive2;
      (_this$getActive2 = this.getActive(experienceId)) === null || _this$getActive2 === void 0 || _this$getActive2.failure({
        metadata: metadata
      });
    }
  }, {
    key: "abort",
    value: function abort(experienceId, metadata) {
      var _this = this;
      // We add this wait in here because when React catches an error it unmounts the component
      // before the error boundary's componentDidCatch is called
      // In this case we want to fail the experience, but without this wait, abort is called first
      setTimeout(function () {
        var _this$getActive3;
        (_this$getActive3 = _this.getActive(experienceId)) === null || _this$getActive3 === void 0 || _this$getActive3.abort({
          metadata: metadata
        });
      }, 0);
    }
  }, {
    key: "abortAll",
    value: function abortAll(metadata) {
      var _this2 = this;
      this.experiences.forEach(function (experience) {
        _this2.abort(experience.id, metadata);
      });
    }
  }, {
    key: "failAll",
    value: function failAll(metadata) {
      var _this3 = this;
      this.experiences.forEach(function (experience) {
        _this3.fail(experience.id, metadata);
      });
    }
  }], [{
    key: "getInstance",
    value: function getInstance(view) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      if (!this.stores.get(view) || options !== null && options !== void 0 && options.forceNewInstance) {
        var store = new ExperienceStore();
        this.stores.set(view, store);
      }
      return this.stores.get(view);
    }
  }]);
  return ExperienceStore;
}();
(0, _defineProperty2.default)(ExperienceStore, "stores", new WeakMap());