"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 util = require("util");
const os_1 = require("os");
const preview_app_constants_1 = require("./preview-app-constants");
class PlaygroundQrCodeGenerator {
    constructor($previewSdkService, $httpClient, $qrCodeTerminalService, $config, $logger) {
        this.$previewSdkService = $previewSdkService;
        this.$httpClient = $httpClient;
        this.$qrCodeTerminalService = $qrCodeTerminalService;
        this.$config = $config;
        this.$logger = $logger;
    }
    generateQrCode(options) {
        return __awaiter(this, void 0, void 0, function* () {
            let url = this.$previewSdkService.getQrCodeUrl(options);
            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.$logger.info();
            const message = `${os_1.EOL} Generating qrcode for url ${url}.`;
            this.$logger.trace(message);
            if (options.link) {
                this.$logger.printMarkdown(message);
            }
            else {
                this.$qrCodeTerminalService.generate(url);
                this.$logger.info();
                this.$logger.printMarkdown(`# Use \`NativeScript Playground app\` and scan the \`QR code\` above to preview the application on your device.`);
                this.$logger.printMarkdown(`
To scan the QR code and deploy your app on a device, you need to have the \`NativeScript Playground app\`:
	App Store (iOS): ${preview_app_constants_1.PlaygroundStoreUrls.APP_STORE_URL}
	Google Play (Android): ${preview_app_constants_1.PlaygroundStoreUrls.GOOGLE_PLAY_URL}`);
            }
        });
    }
}
exports.PlaygroundQrCodeGenerator = PlaygroundQrCodeGenerator;
$injector.register("playgroundQrCodeGenerator", PlaygroundQrCodeGenerator);
