"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* () {
            const message = `Scan the QR code below to install ${"NativeScript Playground app".underline.bold} on your ${"iOS".underline.bold} device or get it from ${preview_app_constants_1.PlaygroundStoreUrls.APP_STORE_URL.underline.bold}.`;
            yield this.generateQrCode(preview_app_constants_1.PlaygroundStoreUrls.APP_STORE_URL, message);
        });
    }
    generateQrCodeForAndroid() {
        return __awaiter(this, void 0, void 0, function* () {
            const message = `Scan the QR code below to install ${"NativeScript Playground app".underline.bold} on your ${"Android".underline.bold} device or get it from ${preview_app_constants_1.PlaygroundStoreUrls.GOOGLE_PLAY_URL.underline.bold}.`;
            yield this.generateQrCode(preview_app_constants_1.PlaygroundStoreUrls.GOOGLE_PLAY_URL, message);
        });
    }
    generateQrCodeForCurrentApp(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const message = `Use ${"NativeScript Playground app".underline.bold} and scan the QR code below to preview the application on your device.`;
            yield this.generateQrCode(this.$previewSdkService.getQrCodeUrl(options), message);
        });
    }
    generateQrCode(url, message) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.generateQrCodeCore(url, message);
            this.printUsage();
        });
    }
    generateQrCodeCore(url, message) {
        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, message);
        });
    }
    printUsage() {
        this.$logger.info(`
-> Press ${this.underlineBoldCyan("a")} to get a link to NativeScript Playground app for ${this.underlineBoldCyan("Android")} on Google Play
-> Press ${this.underlineBoldCyan("i")} to get a link to NativeScript Playground app for ${this.underlineBoldCyan("iOS")} on the App Store
-> Press ${this.underlineBoldCyan("c")} to show the QR code of ${this.underlineBoldCyan("your application")}
		`);
    }
    underlineBoldCyan(str) {
        const { bold, underline, cyan } = chalk;
        return underline(bold(cyan(str)));
    }
}
exports.PlaygroundQrCodeGenerator = PlaygroundQrCodeGenerator;
$injector.register("playgroundQrCodeGenerator", PlaygroundQrCodeGenerator);
