"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 helpers_1 = require("../common/helpers");
class AnalyticsSettingsService {
    constructor($userSettingsService, $staticConfig, $logger) {
        this.$userSettingsService = $userSettingsService;
        this.$staticConfig = $staticConfig;
        this.$logger = $logger;
    }
    canDoRequest() {
        return __awaiter(this, void 0, void 0, function* () {
            return true;
        });
    }
    getUserId() {
        return this.getSettingValueOrDefault("USER_ID");
    }
    getClientId() {
        return this.getSettingValueOrDefault(this.$staticConfig.ANALYTICS_INSTALLATION_ID_SETTING_NAME);
    }
    getClientName() {
        return "" + this.$staticConfig.CLIENT_NAME_ALIAS.cyan.bold;
    }
    getPrivacyPolicyLink() {
        return "http://www.telerik.com/company/privacy-policy";
    }
    getUserSessionsCount(projectName) {
        return __awaiter(this, void 0, void 0, function* () {
            const sessionsCountForProject = yield this.$userSettingsService.getSettingValue(this.getSessionsProjectKey(projectName));
            return sessionsCountForProject || 0;
        });
    }
    setUserSessionsCount(count, projectName) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.$userSettingsService.saveSetting(this.getSessionsProjectKey(projectName), count);
        });
    }
    getSessionsProjectKey(projectName) {
        return `${AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX}${projectName}`;
    }
    getSettingValueOrDefault(settingName) {
        return __awaiter(this, void 0, void 0, function* () {
            let guid = yield this.$userSettingsService.getSettingValue(settingName);
            if (!guid) {
                guid = helpers_1.createGUID(false);
                this.$logger.trace(`Setting new ${settingName}: ${guid}.`);
                yield this.$userSettingsService.saveSetting(settingName, guid);
            }
            return guid;
        });
    }
}
AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX = "SESSIONS_STARTED_";
$injector.register("analyticsSettingsService", AnalyticsSettingsService);
