import { now } from '../../utils';
import { initMetric } from '../performance/util';
export var SINGLE_METRIC_EV_TYPE = 'performance';
export var LONG_TASK_EV_TYPE = 'performance_longtask';
export var applySendOnceAndTearDown = function (wrapPerformanceMetric, report, tearDownGroup) {
    var hasInvoked = false;
    return function (metric) {
        tearDownGroup.length &&
            tearDownGroup.forEach(function (v) {
                v();
            });
        tearDownGroup.length = 0;
        if (hasInvoked)
            return;
        hasInvoked = true;
        report && report(wrapPerformanceMetric(metric));
    };
};
export var wrapPerformanceMetric = function (metric, overrides) { return ({
    ev_type: SINGLE_METRIC_EV_TYPE,
    payload: metric,
    overrides: overrides,
}); };
export var wrapLongtask = function (entry) { return ({
    ev_type: LONG_TASK_EV_TYPE,
    payload: {
        type: 'perf',
        longtasks: [entry],
    },
}); };
export var applyReportMetric = function (report) {
    return function (metric, overrides) {
        report(wrapPerformanceMetric(metric, overrides));
    };
};
var SPAMonitorMetricName = 'spa_load';
export var getSPA = function (report) {
    var startTime = 0;
    var metric = initMetric(SPAMonitorMetricName, 0);
    var invokeCb = function (load_time) {
        metric.value = load_time;
        report && report(wrapPerformanceMetric(metric));
    };
    var performanceInit = function () {
        startTime = now();
    };
    var performanceSend = function () {
        invokeCb(now() - startTime);
        startTime = 0;
    };
    return [performanceInit, performanceSend];
};
//# sourceMappingURL=util.js.map