import { SEVERITY } from '../SEVERITY';
import { isPerformanceObserverLongTaskAvailable } from './is-performance-api-available';
import { getDistortedDurationMonitor } from './measure-render';
import { TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS } from './TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS';
import { TTI_SEVERITY_THRESHOLD_DEFAULTS } from './TTI_SEVERITY_THRESHOLD_DEFAULTS';
export function measureTTI(onMeasureComplete, idleThreshold = 1000, cancelAfter = 60,
// Dependency Injection for easier testing
PerfObserver) {
  if (!isPerformanceObserverLongTaskAvailable()) {
    return;
  }
  const start = performance.now();
  // Keeping track of users moving away from the tab, which distorts the TTI measurement
  const distortedDurationMonitor = getDistortedDurationMonitor();
  let prevLongTask;
  let lastLongTask = {
    startTime: start,
    duration: 0
  };
  const cancelAfterMs = cancelAfter * 1000;
  const observer = new (PerfObserver || PerformanceObserver)(list => {
    const entries = list.getEntries();
    if (entries.length) {
      prevLongTask = lastLongTask;
      lastLongTask = entries[entries.length - 1];
    }
  });
  observer.observe({
    entryTypes: ['longtask']
  });
  const 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`

    const now = performance.now();
    const lastEnd = lastLongTask.startTime + lastLongTask.duration;
    const prevEnd = prevLongTask ? prevLongTask.startTime + prevLongTask.duration : lastEnd;
    const elapsedTimeMs = now - start;
    const 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);
}
export function getTTISeverity(tti, ttiFromInvocation, ttiSeverityNormalThreshold, ttiSeverityDegradedThreshold, ttiFromInvocationSeverityNormalThreshold, ttiFromInvocationSeverityDegradedThreshold) {
  const ttiNormalThreshold = ttiSeverityNormalThreshold || TTI_SEVERITY_THRESHOLD_DEFAULTS.NORMAL;
  const ttiDegradedThreshold = ttiSeverityDegradedThreshold || TTI_SEVERITY_THRESHOLD_DEFAULTS.DEGRADED;
  let ttiSeverity;
  if (tti >= ttiNormalThreshold && tti < ttiDegradedThreshold) {
    ttiSeverity = SEVERITY.DEGRADED;
  } else if (tti >= ttiDegradedThreshold) {
    ttiSeverity = SEVERITY.BLOCKING;
  } else {
    ttiSeverity = SEVERITY.NORMAL;
  }
  const ttiFromInvocationNormalThreshold = ttiFromInvocationSeverityNormalThreshold || TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.NORMAL;
  const ttiFromInvocationDegradedThreshold = ttiFromInvocationSeverityDegradedThreshold || TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS.DEGRADED;
  let ttiFromInvocationSeverity;
  if (ttiFromInvocation >= ttiFromInvocationNormalThreshold && ttiFromInvocation < ttiFromInvocationDegradedThreshold) {
    ttiFromInvocationSeverity = SEVERITY.DEGRADED;
  } else if (ttiFromInvocation >= ttiFromInvocationDegradedThreshold) {
    ttiFromInvocationSeverity = SEVERITY.BLOCKING;
  } else {
    ttiFromInvocationSeverity = SEVERITY.NORMAL;
  }
  return {
    ttiSeverity,
    ttiFromInvocationSeverity
  };
}
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { TTI_SEVERITY_THRESHOLD_DEFAULTS } from './TTI_SEVERITY_THRESHOLD_DEFAULTS';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS } from './TTI_FROM_INVOCATION_SEVERITY_THRESHOLD_DEFAULTS';