"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreviewQrCodeService = void 0;
const util = require("util");
const os_1 = require("os");
const decorators_1 = require("../../../common/decorators");
const yok_1 = require("../../../common/yok");
class PreviewQrCodeService {
    constructor($config, $httpClient, $logger, $mobileHelper, $previewSdkService, $previewSchemaService, $qrCodeTerminalService, $qr) {
        this.$config = $config;
        this.$httpClient = $httpClient;
        this.$logger = $logger;
        this.$mobileHelper = $mobileHelper;
        this.$previewSdkService = $previewSdkService;
        this.$previewSchemaService = $previewSchemaService;
        this.$qrCodeTerminalService = $qrCodeTerminalService;
        this.$qr = $qr;
    }
    getPlaygroundAppQrCode(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const { projectDir } = options || {};
            const result = Object.create(null);
            if (!options ||
                !options.platform ||
                this.$mobileHelper.isAndroidPlatform(options.platform)) {
                result.android = yield this.getLiveSyncQrCode(this.getGooglePlayUrl(projectDir));
            }
            if (!options ||
                !options.platform ||
                this.$mobileHelper.isiOSPlatform(options.platform)) {
                result.ios = yield this.getLiveSyncQrCode(this.getAppStoreUrl(projectDir));
            }
            return result;
        });
    }
    getLiveSyncQrCode(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const shortenUrl = yield this.getShortenUrl(url);
            const imageData = yield this.$qr.generateDataUri(shortenUrl);
            return {
                originalUrl: url,
                shortenUrl,
                imageData,
            };
        });
    }
    printLiveSyncQrCode(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options);
            const url = yield this.getShortenUrl(qrCodeUrl);
            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): ${this.getAppStoreUrl(options.projectDir)}
	Google Play (Android): ${this.getGooglePlayUrl(options.projectDir)}`);
            }
        });
    }
    getShortenUrl(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) {
            }
            return url;
        });
    }
    getGooglePlayUrl(projectDir) {
        const schema = this.$previewSchemaService.getSchemaData(projectDir);
        return `https://play.google.com/store/apps/details?id=${schema.scannerAppId}`;
    }
    getAppStoreUrl(projectDir) {
        const schema = this.$previewSchemaService.getSchemaData(projectDir);
        return `https://itunes.apple.com/us/app/nativescript-playground/id${schema.scannerAppStoreId}?mt=8&ls=1`;
    }
}
__decorate([
    decorators_1.exported("previewQrCodeService")
], PreviewQrCodeService.prototype, "getPlaygroundAppQrCode", null);
exports.PreviewQrCodeService = PreviewQrCodeService;
yok_1.injector.register("previewQrCodeService", PreviewQrCodeService);
