"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptionsTracker = void 0;
const path = require("path");
const _ = require("lodash");
const yok_1 = require("../common/yok");
class OptionsTracker {
    constructor($analyticsService) {
        this.$analyticsService = $analyticsService;
    }
    trackOptions(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const trackObject = this.getTrackObject(options);
            yield this.$analyticsService.trackEventActionInGoogleAnalytics({
                action: "Options",
                additionalData: JSON.stringify(trackObject),
            });
        });
    }
    getTrackObject(options) {
        const optionsArgvCopy = _.cloneDeep(options.argv);
        return this.sanitizeTrackObject(optionsArgvCopy, options);
    }
    sanitizeTrackObject(data, options) {
        const shorthands = options ? options.shorthands : [];
        const optionsDefinitions = options ? options.options : {};
        _.forEach(data, (value, key) => {
            if (this.shouldSkipProperty(key, value, shorthands, optionsDefinitions)) {
                delete data[key];
            }
            else {
                if (options &&
                    optionsDefinitions[key] &&
                    optionsDefinitions[key].hasSensitiveValue !== false) {
                    value = OptionsTracker.PRIVATE_REPLACE_VALUE;
                }
                else if (key.toLowerCase().indexOf(OptionsTracker.PASSWORD_DETECTION_STRING) >=
                    0) {
                    value = OptionsTracker.PRIVATE_REPLACE_VALUE;
                }
                else if (_.isString(value) && value !== path.basename(value)) {
                    value = OptionsTracker.PATH_REPLACE_VALUE;
                }
                else if (_.isObject(value) && !_.isArray(value)) {
                    value = this.sanitizeTrackObject(value);
                }
                data[key] = value;
            }
        });
        return data;
    }
    shouldSkipProperty(key, value, shorthands = [], options = {}) {
        if (shorthands.indexOf(key) >= 0) {
            return true;
        }
        if (key.indexOf("-") >= 0) {
            return true;
        }
        if (key === "_") {
            return true;
        }
        const optionDef = options[key];
        if (optionDef && optionDef.type === "boolean") {
            if ((optionDef.default !== true && value === false) ||
                (optionDef.default === true && value === true)) {
                return true;
            }
        }
        if (_.isUndefined(value)) {
            return true;
        }
    }
}
OptionsTracker.PASSWORD_DETECTION_STRING = "password";
OptionsTracker.PRIVATE_REPLACE_VALUE = "private";
OptionsTracker.PATH_REPLACE_VALUE = "_localpath";
OptionsTracker.SIZE_EXEEDED_REPLACE_VALUE = "sizeExceeded";
exports.OptionsTracker = OptionsTracker;
yok_1.injector.register("optionsTracker", OptionsTracker);
