"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var post_install_1 = require("../common/commands/post-install");
var emailValidator = require("email-validator");
var queryString = require("querystring");
var helpers = require("../common/helpers");
var PostInstallCliCommand = (function (_super) {
    __extends(PostInstallCliCommand, _super);
    function PostInstallCliCommand($fs, $httpClient, $prompter, $userSettingsService, $staticConfig, $commandsService, $htmlHelpService, $options, $doctorService, $analyticsService, $logger) {
        _super.call(this, $fs, $staticConfig, $commandsService, $htmlHelpService, $options, $doctorService, $analyticsService, $logger);
        this.$httpClient = $httpClient;
        this.$prompter = $prompter;
        this.$userSettingsService = $userSettingsService;
        this.logger = $logger;
    }
    PostInstallCliCommand.prototype.execute = function (args) {
        var _this = this;
        return (function () {
            _super.prototype.execute.call(_this, args).wait();
            if (_this.shouldAskForEmail()) {
                _this.logger.out("Leave your e-mail address here to subscribe for NativeScript newsletter and product updates, tips and tricks:");
                var email = _this.getEmail("(press Enter for blank)").wait();
                _this.$userSettingsService.saveSetting("EMAIL_REGISTERED", true).wait();
                _this.sendEmail(email);
            }
        }).future()();
    };
    PostInstallCliCommand.prototype.shouldAskForEmail = function () {
        return helpers.isInteractive() && process.env.CLI_NOPROMPT !== "1" && !this.$userSettingsService.getSettingValue("EMAIL_REGISTERED").wait();
    };
    PostInstallCliCommand.prototype.getEmail = function (prompt, options) {
        var _this = this;
        return (function () {
            var schema = {
                message: prompt,
                type: "input",
                name: "inputEmail",
                validate: function (value) {
                    if (value === "" || emailValidator.validate(value)) {
                        return true;
                    }
                    return "Please provide a valid e-mail or simply leave it blank.";
                },
                default: options && options.defaultAction
            };
            var result = _this.$prompter.get([schema]).wait();
            return result.inputString;
        }).future()();
    };
    PostInstallCliCommand.prototype.sendEmail = function (email) {
        if (email) {
            var postData = queryString.stringify({
                'elqFormName': "dev_uins_cli",
                'elqSiteID': '1325',
                'emailAddress': email,
                'elqCookieWrite': '0'
            });
            var options = {
                url: 'https://s1325.t.eloqua.com/e/f2',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postData.length
                },
                body: postData
            };
            this.$httpClient.httpRequest(options).wait();
        }
    };
    return PostInstallCliCommand;
}(post_install_1.PostInstallCommand));
exports.PostInstallCliCommand = PostInstallCliCommand;
$injector.registerCommand("post-install-cli", PostInstallCliCommand);
