"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("../../core");
const env_1 = __importDefault(require("../utils/env"));
const config_1 = __importDefault(require("../utils/config"));
const update_1 = __importDefault(require("../utils/update"));
let slardarClient = null;
class EventTracker extends core_1.EventTrackerModule {
    constructor() {
        super(config_1.default.slardarInitConfig);
    }
    [core_1.EventTrackerCmdEnum.Timer](event_name, opt) {
        super[core_1.EventTrackerCmdEnum.Timer](event_name, opt);
    }
    initSlardar(_) {
        const config = this.config;
        if (slardarClient) {
            return;
        }
        const client = env_1.default.oversea
            ? require('@slardar/electron/maliva').default
            : require('@slardar/electron').default;
        const proxy = update_1.default.getStorageProxy();
        client('init', {
            bid: config.bid,
            userId: _.uuid,
            release: this.commonTags.cliVersion,
            transport: {
                get: ({ url, data, success, fail }) => {
                    this.request
                        .fetch(url, { data, proxy, method: 'get' })
                        .then((res) => success === null || success === void 0 ? void 0 : success(res))
                        .catch((err) => fail === null || fail === void 0 ? void 0 : fail(err));
                },
                post: ({ url, data, success, fail }) => {
                    this.request
                        .fetch(url, { data, proxy, method: 'post' })
                        .then((res) => success === null || success === void 0 ? void 0 : success(res))
                        .catch((err) => fail === null || fail === void 0 ? void 0 : fail(err));
                },
            },
            monitors: config.monitors || {},
            plugins: Object.assign({}, (config.plugins || {})),
        });
        client('context.merge', this.commonTags);
        client.start();
        slardarClient = client;
    }
    commonSend(param) {
        if (param.type === 'log') {
            slardarClient === null || slardarClient === void 0 ? void 0 : slardarClient('sendLog', {
                level: param.level,
                content: param.value,
                extra: param.tags,
            });
        }
        else {
            const metrics = {};
            if (param.type === 'counter') {
                metrics.count = param.value;
            }
            else {
                metrics.time = param.value;
            }
            slardarClient === null || slardarClient === void 0 ? void 0 : slardarClient('sendEvent', {
                name: param.event_name,
                metrics,
                categories: param.tags,
            });
        }
    }
    reportError(e) {
        if (process.env.CLI_ENV === 'development') {
            throw e;
        }
        else {
            slardarClient === null || slardarClient === void 0 ? void 0 : slardarClient('captureException', e);
        }
    }
    initTea() { }
    [core_1.EventTrackerCmdEnum.TimeLine](_) {
    }
    [core_1.EventTrackerCmdEnum.End]() {
        slardarClient === null || slardarClient === void 0 ? void 0 : slardarClient('destroy');
        slardarClient = null;
    }
}
exports.default = EventTracker;
