"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const uuid = require("uuid");
const ua = require("universal-analytics");
class GoogleAnalyticsProvider {
    constructor(clientId, $staticConfig, $hostInfo, $osInfo) {
        this.clientId = clientId;
        this.$staticConfig = $staticConfig;
        this.$hostInfo = $hostInfo;
        this.$osInfo = $osInfo;
    }
    trackHit(trackInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            const visitor = ua({
                tid: GoogleAnalyticsProvider.GA_TRACKING_ID,
                cid: this.clientId,
                headers: {
                    ["User-Agent"]: this.getUserAgentString()
                }
            });
            this.setCustomDimensions(visitor, trackInfo.customDimensions);
            switch (trackInfo.googleAnalyticsDataType) {
                case "pageview":
                    yield this.trackPageView(visitor, trackInfo);
                    break;
                case "event":
                    yield this.trackEvent(visitor, trackInfo);
                    break;
            }
        });
    }
    setCustomDimensions(visitor, customDimensions) {
        const defaultValues = {
            ["cd1"]: this.$staticConfig.version,
            ["cd6"]: process.version,
            ["cd3"]: this.clientId,
            ["cd2"]: null,
            ["cd4"]: uuid.v4(),
            ["cd5"]: "Unknown"
        };
        customDimensions = _.merge(defaultValues, customDimensions);
        _.each(customDimensions, (value, key) => {
            visitor.set(key, value);
        });
    }
    trackEvent(visitor, trackInfo) {
        return new Promise((resolve, reject) => {
            visitor.event(trackInfo.category, trackInfo.action, trackInfo.label, trackInfo.value, { p: this.currentPage }, (err) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve();
            });
        });
    }
    trackPageView(visitor, trackInfo) {
        return new Promise((resolve, reject) => {
            this.currentPage = trackInfo.path;
            const pageViewData = {
                dp: trackInfo.path,
                dt: trackInfo.title
            };
            visitor.pageview(pageViewData, (err) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve();
            });
        });
    }
    getUserAgentString() {
        let osString = "";
        const osRelease = this.$osInfo.release();
        if (this.$hostInfo.isWindows) {
            osString = `Windows NT ${osRelease}`;
        }
        else if (this.$hostInfo.isDarwin) {
            osString = `Macintosh`;
            const macRelease = this.getMacOSReleaseVersion(osRelease);
            if (macRelease) {
                osString += `; Intel Mac OS X ${macRelease}`;
            }
        }
        else {
            osString = `Linux x86`;
            if (this.$osInfo.arch() === "x64") {
                osString += "_64";
            }
        }
        const userAgent = `tnsCli/${this.$staticConfig.version} (${osString}; ${this.$osInfo.arch()})`;
        return userAgent;
    }
    getMacOSReleaseVersion(osRelease) {
        const majorVersion = osRelease && _.first(osRelease.split("."));
        return majorVersion && `10.${+majorVersion - 4}`;
    }
}
GoogleAnalyticsProvider.GA_TRACKING_ID = "UA-111455-44";
exports.GoogleAnalyticsProvider = GoogleAnalyticsProvider;
$injector.register("googleAnalyticsProvider", GoogleAnalyticsProvider);
