"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.succeedExp = exports.startExp = exports.failExp = exports.abortExp = exports.EXPERIENCE_NAME = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _ufo = require("@atlaskit/ufo");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /**
 * UFO experience tracking helpers for `@atlaskit/editor-plugin-autocomplete`.
 *
 * Pattern follows `packages/linking-platform/smart-card/src/state/analytics/ufoExperiences.ts`
 * (ConcurrentExperience keyed per request/instance) plus a try/catch safety wrap
 * inspired by `packages/editor/collab-provider/src/analytics/ufo.ts` so a UFO
 * runtime error can never break the autocomplete feature.
 *
 * Experience names must be registered in DataPortal's `task` attribute before
 * they can power FE Reliability SLOs:
 * https://hello.atlassian.net/wiki/spaces/AA6/pages/3961393753
 */
/**
 * Experience name strings are surfaced downstream by the UFO pipeline as
 * `platform.fe.<type>.<platform.component>.<name>` (e.g.
 * `platform.fe.operation.editor-plugin-autocomplete.slow-lane-fetch`), so
 * names here should be short, dot-free, and must not repeat the component
 * name. This matches the convention used by `@atlaskit/emoji`'s
 * `ufoExperiences` map (e.g. `'emoji-rendered'`).
 */
/**
 * Only experiences with meaningful async work and a real success/failure
 * outcome belong on UFO (which measures latency and success rate for FE
 * Reliability SLOs). Per-keystroke suggestion lifecycle counters (viewed /
 * inserted / dismissed) are tracked via analytics-next instead — they would
 * otherwise emit zero-duration events on every word boundary.
 */
var EXPERIENCE_NAME = exports.EXPERIENCE_NAME = {
  SLOW_LANE_FETCH: 'slow-lane-fetch',
  LOAD_VOCABULARY: 'load-vocabulary',
  LOAD_VECTORS: 'load-vectors'
};
var isUfoEnabled = function isUfoEnabled() {
  var _process;
  return !((_process = process) !== null && _process !== void 0 && (_process = _process.env) !== null && _process !== void 0 && _process.REACT_SSR);
};
var operationConfig = {
  platform: {
    component: 'editor-plugin-autocomplete'
  },
  type: _ufo.ExperienceTypes.Operation,
  performanceType: _ufo.ExperiencePerformanceTypes.Custom,
  performanceConfig: {
    histogram: (0, _defineProperty2.default)({}, _ufo.ExperiencePerformanceTypes.Custom, {
      duration: '50_100_250_500_1000_2000_4000'
    })
  }
};
var experiences = (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)({}, EXPERIENCE_NAME.SLOW_LANE_FETCH, new _ufo.ConcurrentExperience(EXPERIENCE_NAME.SLOW_LANE_FETCH, operationConfig)), EXPERIENCE_NAME.LOAD_VOCABULARY, new _ufo.ConcurrentExperience(EXPERIENCE_NAME.LOAD_VOCABULARY, operationConfig)), EXPERIENCE_NAME.LOAD_VECTORS, new _ufo.ConcurrentExperience(EXPERIENCE_NAME.LOAD_VECTORS, operationConfig));
var startExp = exports.startExp = function startExp(name, id, metadata) {
  if (!isUfoEnabled()) {
    return;
  }
  try {
    var instance = experiences[name].getInstance(id);
    instance.start();
    if (metadata) {
      instance.addMetadata(metadata);
    }
  } catch (_unused) {
    // UFO errors must never break the plugin
  }
};
var succeedExp = exports.succeedExp = function succeedExp(name, id, metadata) {
  if (!isUfoEnabled()) {
    return;
  }
  try {
    experiences[name].getInstance(id).success({
      metadata: metadata
    });
  } catch (_unused2) {
    // UFO errors must never break the plugin
  }
};
var failExp = exports.failExp = function failExp(name, id, metadata) {
  if (!isUfoEnabled()) {
    return;
  }
  try {
    experiences[name].getInstance(id).failure({
      metadata: metadata
    });
  } catch (_unused3) {
    // UFO errors must never break the plugin
  }
};
var abortExp = exports.abortExp = function abortExp(name, id, reason, metadata) {
  if (!isUfoEnabled()) {
    return;
  }
  try {
    // `reason` is the dedicated, canonical channel for the abort reason and
    // intentionally takes precedence over a `reason` key in `metadata`. Callers
    // must pass the reason via the param, not inside `metadata` (the `reason`
    // key there is reserved and would be overwritten).
    var merged = _objectSpread(_objectSpread({}, metadata), reason ? {
      reason: reason
    } : {});
    var hasMetadata = Object.keys(merged).length > 0;
    experiences[name].getInstance(id).abort(hasMetadata ? {
      metadata: merged
    } : undefined);
  } catch (_unused4) {
    // UFO errors must never break the plugin
  }
};