"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 emailValidator = require("email-validator");
const queryString = require("querystring");
const helpers = require("../common/helpers");
class SubscriptionService {
    constructor($httpClient, $prompter, $userSettingsService, $logger) {
        this.$httpClient = $httpClient;
        this.$prompter = $prompter;
        this.$userSettingsService = $userSettingsService;
        this.$logger = $logger;
    }
    subscribeForNewsletter() {
        return __awaiter(this, void 0, void 0, function* () {
            if (yield this.shouldAskForEmail()) {
                this.$logger.out("Leave your e-mail address here to subscribe for NativeScript newsletter and product updates, tips and tricks:");
                const email = yield this.getEmail("(press Enter for blank)");
                yield this.$userSettingsService.saveSetting("EMAIL_REGISTERED", true);
                yield this.sendEmail(email);
            }
        });
    }
    shouldAskForEmail() {
        return __awaiter(this, void 0, void 0, function* () {
            return helpers.isInteractive() && process.env.CLI_NOPROMPT !== "1" && !(yield this.$userSettingsService.getSettingValue("EMAIL_REGISTERED"));
        });
    }
    getEmail(prompt, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const schema = {
                message: prompt,
                type: "input",
                name: "inputEmail",
                validate: (value) => {
                    if (value === "" || emailValidator.validate(value)) {
                        return true;
                    }
                    return "Please provide a valid e-mail or simply leave it blank.";
                }
            };
            const result = yield this.$prompter.get([schema]);
            return result.inputEmail;
        });
    }
    sendEmail(email) {
        return __awaiter(this, void 0, void 0, function* () {
            if (email) {
                const postData = queryString.stringify({
                    'elqFormName': "dev_uins_cli",
                    'elqSiteID': '1325',
                    'emailAddress': email,
                    'elqCookieWrite': '0'
                });
                const 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
                };
                yield this.$httpClient.httpRequest(options);
            }
        });
    }
}
exports.SubscriptionService = SubscriptionService;
$injector.register("subscriptionService", SubscriptionService);
