"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getPerformanceOptions = getPerformanceOptions;
Object.defineProperty(exports, "startMeasureReactNodeViewRendered", {
  enumerable: true,
  get: function get() {
    return _startMeasureReactNodeViewRendered.startMeasureReactNodeViewRendered;
  }
});
exports.stopMeasureReactNodeViewRendered = stopMeasureReactNodeViewRendered;
var _analytics = require("../analytics");
var _performanceMeasures = require("../performance-measures");
var _startMeasureReactNodeViewRendered = require("./startMeasureReactNodeViewRendered");
// This was existing logic when converting from ReactNodeView
// our current sampling for this event is not bound by node.type
var nodeViewRenderedEventsCounter = 0;
var DEFAULT_SAMPLING_RATE = 100;
var DEFAULT_SLOW_THRESHOLD = 7;
function getPerformanceOptions(view) {
  var _nodeViewTracking$sam, _nodeViewTracking$slo;
  // Please, do not copy or use this kind of code below
  // @ts-ignore
  var fakePluginKey = {
    key: 'analyticsPlugin$',
    getState: function getState(state) {
      // Ignored via go/ees005
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      return state['analyticsPlugin$'];
    }
  };
  var pluginState = fakePluginKey.getState(view.state);
  var nodeViewTracking = pluginState && pluginState.performanceTracking ? pluginState.performanceTracking.nodeViewTracking || {} : {};
  var samplingRate = (_nodeViewTracking$sam = nodeViewTracking.samplingRate) !== null && _nodeViewTracking$sam !== void 0 ? _nodeViewTracking$sam : DEFAULT_SAMPLING_RATE;
  var slowThreshold = (_nodeViewTracking$slo = nodeViewTracking.slowThreshold) !== null && _nodeViewTracking$slo !== void 0 ? _nodeViewTracking$slo : DEFAULT_SLOW_THRESHOLD;
  return {
    trackingEnabled: !!nodeViewTracking.enabled,
    samplingRate: samplingRate,
    slowThreshold: slowThreshold
  };
}
function stopMeasureReactNodeViewRendered(_ref) {
  var nodeTypeName = _ref.nodeTypeName,
    dispatchAnalyticsEvent = _ref.dispatchAnalyticsEvent,
    samplingRate = _ref.samplingRate,
    slowThreshold = _ref.slowThreshold;
  (0, _performanceMeasures.stopMeasure)("\uD83E\uDD89".concat(nodeTypeName, "::ReactNodeView"), function (duration) {
    if (++nodeViewRenderedEventsCounter % samplingRate === 0 && duration > slowThreshold) {
      dispatchAnalyticsEvent({
        action: _analytics.ACTION.REACT_NODEVIEW_RENDERED,
        actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
        eventType: _analytics.EVENT_TYPE.OPERATIONAL,
        attributes: {
          node: nodeTypeName,
          duration: duration
        }
      });
    }
  });
}
// eslint-disable-next-line @atlaskit/editor/no-re-export