import { __assign, __read } from "tslib";
import { initSubjectInGlobal, createSafeObserver, initPrivateSubject } from '@slardar/sdk-template';
import { applyPV, applyFirstPV, FIRST_PV_SUBJECT_NAME } from '../../collector/client';
import { hiddenSubject, unloadSubject, loadSubject } from '../../collector/page';
import { longtaskSubject } from '../../collector/pref';
import { FP, FCP, LCP, FID, CLS, LONGTASK, TIMING, MPFID } from '../../monitors/performance-new';
import { wrapPerformanceMetric, getSPA, SINGLE_METRIC_EV_TYPE, LONG_TASK_EV_TYPE, } from '../../monitors/performance-new/util';
import { defaultMetricContext } from '../../monitors/performance/util';
import { PERFORMANCE_MONITOR_PLUGIN_NAME, defaultPerformancePrecollect } from './performance';
import { getPluginConfig } from './utils';
export function PerformanceMonitorPlugin(client) {
    client.on('init', function () {
        var _a;
        var precollect = client.pp || defaultPerformancePrecollect;
        // clear precollect observer no matter what is configured
        (_a = precollect.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
        var config = getPluginConfig(client, PERFORMANCE_MONITOR_PLUGIN_NAME, {});
        if (!config)
            return;
        var hiddenObserver = initSubjectInGlobal(client, hiddenSubject);
        var unloadObserver = initSubjectInGlobal(client, unloadSubject);
        var loadObserver = initSubjectInGlobal(client, loadSubject);
        var longtaskObserver = initSubjectInGlobal(client, longtaskSubject);
        var tearDownGroup = [];
        var overrides = undefined;
        var firstPVObserver = initPrivateSubject(client, [FIRST_PV_SUBJECT_NAME, applyFirstPV(client)], -1);
        firstPVObserver[0](function (o) {
            overrides = o;
        })();
        var reportMetric = function (data) {
            // every first screen metrics needs to be associated with the context when client init, but CLS and long task are not included
            var payloadWithOverrides = (data.ev_type === SINGLE_METRIC_EV_TYPE && data.payload.name === CLS[0]) ||
                data.ev_type === LONG_TASK_EV_TYPE
                ? data
                : __assign(__assign({}, data), { overrides: overrides });
            client.report(payloadWithOverrides);
        };
        var pvObserver = createSafeObserver(applyPV(client));
        [FP, FCP, LCP, FID, CLS].forEach(function (cur) {
            if (config[cur[0]] !== false) {
                var tearDown = [];
                cur[1](reportMetric, tearDown, [hiddenObserver, unloadObserver, pvObserver]);
                tearDownGroup.push(tearDown);
            }
        });
        [LONGTASK, TIMING, MPFID].forEach(function (cur) {
            if (config[cur[0]] !== false) {
                var tearDown = [];
                cur[1](reportMetric, tearDown, [loadObserver, unloadObserver, longtaskObserver]);
                tearDownGroup.push(tearDown);
            }
        });
        // spa_load
        var _b = __read(getSPA(client.report.bind(client)), 2), performanceInit = _b[0], performanceSend = _b[1];
        client.provide('performanceInit', performanceInit);
        client.provide('performanceSend', performanceSend);
        // clear precollect data after consumed
        precollect.entries.length = 0;
        // provide custom performance API
        var sendPref = function (customMetric) {
            var payload = __assign(__assign(__assign({}, defaultMetricContext), customMetric), { isCustom: true });
            client.report(wrapPerformanceMetric(payload));
        };
        client.provide('sendCustomPerfMetric', sendPref);
        client.on('beforeDestroy', function () {
            tearDownGroup
                .reduce(function (pre, cur) {
                return pre.concat(cur);
            }, [])
                .forEach(function (fn) { return fn(); });
            tearDownGroup.length = 0;
        });
    });
}
//# sourceMappingURL=performance-new.js.map