"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.clearMeasure = clearMeasure;
exports.startMeasure = startMeasure;
exports.stopMeasure = stopMeasure;
var _isPerformanceApiAvailable = require("./is-performance-api-available");
var measureMap = new Map();

// eslint-disable-next-line @repo/internal/deprecations/deprecation-ticket-required -- Ignored via go/ED-25883
/**
 * @deprecated use 'startMeasure' from '@atlaskit/editor-common/performance-measures'
 */
function startMeasure(measureName) {
  if (!(0, _isPerformanceApiAvailable.isPerformanceAPIAvailable)()) {
    return;
  }
  performance.mark("".concat(measureName, "::start"));
  measureMap.set(measureName, performance.now());
}

// eslint-disable-next-line @repo/internal/deprecations/deprecation-ticket-required -- Ignored via go/ED-25883
/**
 * @deprecated use 'stopMeasure' from '@atlaskit/editor-common/performance-measures'
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function stopMeasure(measureName, onMeasureComplete) {
  if (!(0, _isPerformanceApiAvailable.isPerformanceAPIAvailable)()) {
    return;
  }
  performance.mark("".concat(measureName, "::end"));
  var start = onMeasureComplete ? measureMap.get(measureName) : undefined;
  try {
    performance.measure(measureName, "".concat(measureName, "::start"), "".concat(measureName, "::end"));
  } catch (error) {} finally {
    if (onMeasureComplete) {
      var entry = performance.getEntriesByName(measureName).pop();
      if (entry) {
        onMeasureComplete(entry.duration, entry.startTime);
      } else if (start) {
        onMeasureComplete(performance.now() - start, start);
      }
    }
    clearMeasure(measureName);
  }
}

// eslint-disable-next-line @repo/internal/deprecations/deprecation-ticket-required -- Ignored via go/ED-25883
/**
 * @deprecated use 'clearMeasure' from '@atlaskit/editor-common/performance-measures'
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function clearMeasure(measureName) {
  if (!(0, _isPerformanceApiAvailable.isPerformanceAPIAvailable)()) {
    return;
  }
  measureMap.delete(measureName);
  performance.clearMarks("".concat(measureName, "::start"));
  performance.clearMarks("".concat(measureName, "::end"));
  performance.clearMeasures(measureName);
}