"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 preview_app_constants_1 = require("./preview-app-constants");
const util = require("util");
const chalk = require("chalk");
class PlaygroundQrCodeGenerator {
    constructor($previewSdkService, $httpClient, $qrCodeTerminalService, $config, $logger) {
        this.$previewSdkService = $previewSdkService;
        this.$httpClient = $httpClient;
        this.$qrCodeTerminalService = $qrCodeTerminalService;
        this.$config = $config;
        this.$logger = $logger;
    }
    generateQrCodeForiOS() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.generateQrCode(preview_app_constants_1.PlaygroundStoreUrls.APP_STORE_URL);
        });
    }
    generateQrCodeForAndroid() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.generateQrCode(preview_app_constants_1.PlaygroundStoreUrls.GOOGLE_PLAY_URL);
        });
    }
    generateQrCodeForCurrentApp() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.generateQrCode(this.$previewSdkService.qrCodeUrl);
        });
    }
    generateQrCode(url) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.generateQrCodeCore(url);
            this.printUsage();
        });
    }
    generateQrCodeCore(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const shortenUrlEndpoint = util.format(this.$config.SHORTEN_URL_ENDPOINT, encodeURIComponent(url));
            try {
                const response = yield this.$httpClient.httpRequest(shortenUrlEndpoint);
                const responseBody = JSON.parse(response.body);
                url = responseBody.shortURL || url;
            }
            catch (e) {
            }
            this.$qrCodeTerminalService.generate(url);
        });
    }
    printUsage() {
        this.$logger.info(`
-> Press ${this.underlineBoldCyan("a")} to show the QR code of NativeScript Playground app for ${this.underlineBoldCyan("Android")} devices
-> Press ${this.underlineBoldCyan("i")} to show the QR code of NativeScript Playground app for ${this.underlineBoldCyan("iOS")} devices
-> Press ${this.underlineBoldCyan("c")} to display the QR code of the ${this.underlineBoldCyan("current application")}.
		`);
    }
    underlineBoldCyan(str) {
        const { bold, underline, cyan } = chalk;
        return underline(bold(cyan(str)));
    }
}
exports.PlaygroundQrCodeGenerator = PlaygroundQrCodeGenerator;
$injector.register("playgroundQrCodeGenerator", PlaygroundQrCodeGenerator);
