"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS", {
  enumerable: true,
  get: function get() {
    return _TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS;
  }
});
Object.defineProperty(exports, "TTI_SEVERITY_THRESHOLD_DEFAULTS", {
  enumerable: true,
  get: function get() {
    return _TTI_SEVERITY_THRESHOLD_DEFAULTS.TTI_SEVERITY_THRESHOLD_DEFAULTS;
  }
});
exports.getTTISeverity = getTTISeverity;
exports.measureTTI = measureTTI;
var _SEVERITY = require("../SEVERITY");
var _isPerformanceApiAvailable = require("./is-performance-api-available");
var _measureRender = require("./measure-render");
var _TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS = require("./TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS");
var _TTI_SEVERITY_THRESHOLD_DEFAULTS = require("./TTI_SEVERITY_THRESHOLD_DEFAULTS");
function measureTTI(onMeasureComplete) {
  var idleThreshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
  var cancelAfter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 60;
  var
  // Dependency Injection for easier testing
  PerfObserver = arguments.length > 3 ? arguments[3] : undefined;
  if (!(0, _isPerformanceApiAvailable.isPerformanceObserverLongTaskAvailable)()) {
    return;
  }
  var start = performance.now();
  // Keeping track of users moving away from the tab, which distorts the TTI measurement
  var distortedDurationMonitor = (0, _measureRender.getDistortedDurationMonitor)();
  var prevLongTask;
  var lastLongTask = {
    startTime: start,
    duration: 0
  };
  var cancelAfterMs = cancelAfter * 1000;
  var observer = new (PerfObserver || PerformanceObserver)(function (list) {
    var entries = list.getEntries();
    if (entries.length) {
      prevLongTask = lastLongTask;
      lastLongTask = entries[entries.length - 1];
    }
  });
  observer.observe({
    entryTypes: ['longtask']
  });
  var _checkIdle = function checkIdle() {
    // 1. There hasn't been any long task in `idleThreshold` time: Interactive from the start.
    // 2. Only 1 long task: Interactive from the end of the only long task.
    // 3. Several long tasks:
    //    3.1 Interactive from the end of prevLongTask if `lastLongTask.start - prevLongTask.end >= idleThreshold`
    //    3.2 Interactive from the end of lastLongTask if `lastLongTask.start - prevLongTask.end < idleThreshold`

    var now = performance.now();
    var lastEnd = lastLongTask.startTime + lastLongTask.duration;
    var prevEnd = prevLongTask ? prevLongTask.startTime + prevLongTask.duration : lastEnd;
    var elapsedTimeMs = now - start;
    var canceled = elapsedTimeMs > cancelAfterMs;
    if (!prevLongTask) {
      observer.disconnect();
      distortedDurationMonitor.cleanup();
      return onMeasureComplete(prevEnd, 0, false, distortedDurationMonitor.distortedDuration);
    } else if (lastLongTask.startTime - prevEnd >= idleThreshold) {
      observer.disconnect();
      distortedDurationMonitor.cleanup();
      return onMeasureComplete(prevEnd, prevEnd - start, canceled, distortedDurationMonitor.distortedDuration);
    } else if (now - lastEnd >= idleThreshold || canceled) {
      observer.disconnect();
      distortedDurationMonitor.cleanup();
      return onMeasureComplete(lastEnd, lastEnd - start, canceled, distortedDurationMonitor.distortedDuration);
    }
    return setTimeout(_checkIdle, idleThreshold);
  };
  setTimeout(_checkIdle, idleThreshold);
}

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
function getTTISeverity(tti, ttiFromInvocation, ttiSeverityNormalThreshold, ttiSeverityDegradedThreshold, ttiFromInvocationSeverityNormalThreshold, ttiFromInvocationSeverityDegradedThreshold) {
  var ttiNormalThreshold = ttiSeverityNormalThreshold || _TTI_SEVERITY_THRESHOLD_DEFAULTS.TTI_SEVERITY_THRESHOLD_DEFAULTS.NORMAL;
  var ttiDegradedThreshold = ttiSeverityDegradedThreshold || _TTI_SEVERITY_THRESHOLD_DEFAULTS.TTI_SEVERITY_THRESHOLD_DEFAULTS.DEGRADED;
  var ttiSeverity;
  if (tti >= ttiNormalThreshold && tti < ttiDegradedThreshold) {
    ttiSeverity = _SEVERITY.SEVERITY.DEGRADED;
  } else if (tti >= ttiDegradedThreshold) {
    ttiSeverity = _SEVERITY.SEVERITY.BLOCKING;
  } else {
    ttiSeverity = _SEVERITY.SEVERITY.NORMAL;
  }
  var ttiFromInvocationNormalThreshold = ttiFromInvocationSeverityNormalThreshold || _TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.NORMAL;
  var ttiFromInvocationDegradedThreshold = ttiFromInvocationSeverityDegradedThreshold || _TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.DEGRADED;
  var ttiFromInvocationSeverity;
  if (ttiFromInvocation >= ttiFromInvocationNormalThreshold && ttiFromInvocation < ttiFromInvocationDegradedThreshold) {
    ttiFromInvocationSeverity = _SEVERITY.SEVERITY.DEGRADED;
  } else if (ttiFromInvocation >= ttiFromInvocationDegradedThreshold) {
    ttiFromInvocationSeverity = _SEVERITY.SEVERITY.BLOCKING;
  } else {
    ttiFromInvocationSeverity = _SEVERITY.SEVERITY.NORMAL;
  }
  return {
    ttiSeverity: ttiSeverity,
    ttiFromInvocationSeverity: ttiFromInvocationSeverity
  };
}
// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export