"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("../common/helpers");
const decorators_1 = require("../common/decorators");
const _ = require("lodash");
const yok_1 = require("../common/yok");
const color_1 = require("../color");
class AnalyticsSettingsService {
    constructor($userSettingsService, $staticConfig, $hostInfo, $osInfo, $logger) {
        this.$userSettingsService = $userSettingsService;
        this.$staticConfig = $staticConfig;
        this.$hostInfo = $hostInfo;
        this.$osInfo = $osInfo;
        this.$logger = $logger;
    }
    async canDoRequest() {
        return true;
    }
    getUserId() {
        return this.getSettingValueOrDefault("USER_ID");
    }
    getClientId() {
        return this.getSettingValueOrDefault(this.$staticConfig.ANALYTICS_INSTALLATION_ID_SETTING_NAME);
    }
    getClientName() {
        return "" + color_1.color.cyan.bold(this.$staticConfig.CLIENT_NAME_ALIAS);
    }
    async getUserSessionsCount(projectName) {
        const sessionsCountForProject = await this.$userSettingsService.getSettingValue(this.getSessionsProjectKey(projectName));
        return sessionsCountForProject || 0;
    }
    async setUserSessionsCount(count, projectName) {
        return this.$userSettingsService.saveSetting(this.getSessionsProjectKey(projectName), count);
    }
    getUserAgentString(identifier) {
        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 = `${identifier} (${osString}; ${this.$osInfo.arch()})`;
        return userAgent;
    }
    getMacOSReleaseVersion(osRelease) {
        const majorVersion = osRelease && _.first(osRelease.split("."));
        return majorVersion && `10.${+majorVersion - 4}`;
    }
    getSessionsProjectKey(projectName) {
        return `${AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX}${projectName}`;
    }
    async getSettingValueOrDefault(settingName) {
        let guid = await this.$userSettingsService.getSettingValue(settingName);
        if (!guid) {
            guid = (0, helpers_1.createGUID)(false);
            this.$logger.trace(`Setting new ${settingName}: ${guid}.`);
            await this.$userSettingsService.saveSetting(settingName, guid);
        }
        return guid;
    }
}
AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX = "SESSIONS_STARTED_";
__decorate([
    (0, decorators_1.exported)("analyticsSettingsService")
], AnalyticsSettingsService.prototype, "getClientId", null);
__decorate([
    (0, decorators_1.exported)("analyticsSettingsService")
], AnalyticsSettingsService.prototype, "getUserAgentString", null);
yok_1.injector.register("analyticsSettingsService", AnalyticsSettingsService);
