"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.clearMeasure = clearMeasure;
exports.isMeasuring = isMeasuring;
exports.startMeasure = startMeasure;
exports.stopMeasure = stopMeasure;
var _isPerformanceApiAvailable = require("../utils/performance/is-performance-api-available");
var measureMap = new Map();
function startMeasure(measureName) {
  if (!(0, _isPerformanceApiAvailable.isPerformanceAPIAvailable)()) {
    return;
  }
  performance.mark("".concat(measureName, "::start"));
  measureMap.set(measureName, performance.now());
}

// 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 @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);
}

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function isMeasuring(measureName) {
  if (!(0, _isPerformanceApiAvailable.isPerformanceAPIAvailable)()) {
    return false;
  }
  return measureMap.get(measureName) !== undefined;
}