import { __assign, __read } from "tslib";
import { initPrivateSubject } from '@slardar/sdk-template';
import { FIRST_PV_SUBJECT_NAME, applyFirstPV } from '../../collector/client';
import { hashSubject, historySubject } from '../../collector/route';
import { pvGetterWithRouteObserver } from '../../monitors/pageview';
import { isManualMode } from '../../monitors/pageview/util';
import { now } from '../../utils';
import { getPluginConfig } from './utils';
export var PAGEVIEW_MONITOR_PLUGIN_NAME = 'pageview';
var defaultConfig = {
    sendInit: true,
    routeMode: 'history',
};
export function PageviewMonitorPlugin(client) {
    client.on('init', function () {
        var _a;
        var config = getPluginConfig(client, PAGEVIEW_MONITOR_PLUGIN_NAME, defaultConfig);
        if (!config || !location) {
            return;
        }
        var tearDownGroup = [];
        var routeMode = config.routeMode;
        var _b = __read(pvGetterWithRouteObserver(client.report.bind(client), tearDownGroup, isManualMode(routeMode) ? [] : [client.initSubject(routeMode === 'hash' ? hashSubject : historySubject)], __assign(__assign({}, config), { initPid: (_a = client.config()) === null || _a === void 0 ? void 0 : _a.pid, onPidUpdate: function (pid) {
                client.set({ pid: pid, viewId: pid + "_" + now(), actionId: undefined });
            } })), 1), sendPageview = _b[0];
        initPrivateSubject(client, [FIRST_PV_SUBJECT_NAME, applyFirstPV(client)], -1);
        client.on('config', function () {
            sendPageview(client.config().pid);
        });
        client.on('beforeDestroy', function () {
            tearDownGroup.forEach(function (e) { return e(); });
        });
        client.provide('sendPageview', sendPageview);
    });
}
//# sourceMappingURL=pageview.js.map