import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { ExperiencePerformanceTypes, ExperienceTypes, UFOExperience } from '@atlaskit/ufo';
export const experienceConfig = {
  type: ExperienceTypes.Operation,
  performanceType: ExperiencePerformanceTypes.Custom,
  platform: {
    component: 'editor'
  }
};
export let EditorExperience = /*#__PURE__*/function (EditorExperience) {
  EditorExperience["loadEditor"] = "load";
  EditorExperience["typing"] = "type";
  EditorExperience["interaction"] = "interact";
  EditorExperience["editSession"] = "edit-session";
  return EditorExperience;
}({});
export const RELIABILITY_INTERVAL = 30000;
export class ExperienceStore {
  constructor() {
    this.experiences = new Map();
    for (const experienceId of Object.values(EditorExperience)) {
      const experience = new UFOExperience(experienceId, experienceConfig);
      this.experiences.set(experienceId, experience);
    }
  }
  static getInstance(view, options = {}) {
    if (!this.stores.get(view) || options !== null && options !== void 0 && options.forceNewInstance) {
      const store = new ExperienceStore();
      this.stores.set(view, store);
    }
    return this.stores.get(view);
  }
  get(experienceId) {
    return this.experiences.get(experienceId);
  }
  getActive(experienceId) {
    const experience = this.experiences.get(experienceId);
    if (!(experience !== null && experience !== void 0 && experience.state.final)) {
      return experience;
    }
  }
  getAll() {
    return Array.from(this.experiences.values());
  }
  start(experienceId, startTime) {
    var _this$get;
    (_this$get = this.get(experienceId)) === null || _this$get === void 0 ? void 0 : _this$get.start(startTime);
  }
  addMetadata(experienceId, metadata) {
    var _this$get2;
    (_this$get2 = this.get(experienceId)) === null || _this$get2 === void 0 ? void 0 : _this$get2.addMetadata(metadata);
  }
  mark(experienceId, mark, value) {
    var _this$get3;
    (_this$get3 = this.get(experienceId)) === null || _this$get3 === void 0 ? void 0 : _this$get3.mark(mark, value);
  }
  success(experienceId, metadata) {
    var _this$getActive;
    return (_this$getActive = this.getActive(experienceId)) === null || _this$getActive === void 0 ? void 0 : _this$getActive.success({
      metadata
    });
  }
  fail(experienceId, metadata) {
    var _this$getActive2;
    (_this$getActive2 = this.getActive(experienceId)) === null || _this$getActive2 === void 0 ? void 0 : _this$getActive2.failure({
      metadata
    });
  }
  abort(experienceId, metadata) {
    // 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(() => {
      var _this$getActive3;
      (_this$getActive3 = this.getActive(experienceId)) === null || _this$getActive3 === void 0 ? void 0 : _this$getActive3.abort({
        metadata
      });
    }, 0);
  }
  abortAll(metadata) {
    this.experiences.forEach(experience => {
      this.abort(experience.id, metadata);
    });
  }
  failAll(metadata) {
    this.experiences.forEach(experience => {
      this.fail(experience.id, metadata);
    });
  }
}
_defineProperty(ExperienceStore, "stores", new WeakMap());