"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 nativescript_preview_sdk_1 = require("nativescript-preview-sdk");
const preview_app_constants_1 = require("./preview-app-constants");
const constants_1 = require("../../../common/constants");
const events_1 = require("events");
const pako = require("pako");
class PreviewSdkService extends events_1.EventEmitter {
    constructor($logger, $httpClient, $config) {
        super();
        this.$logger = $logger;
        this.$httpClient = $httpClient;
        this.$config = $config;
        this.messagingService = null;
        this.instanceId = null;
        this.connectedDevices = [];
    }
    getQrCodeUrl(options) {
        const hmrValue = options.useHotModuleReload ? "1" : "0";
        return `nsplay://boot?instanceId=${this.instanceId}&pKey=${preview_app_constants_1.PubnubKeys.PUBLISH_KEY}&sKey=${preview_app_constants_1.PubnubKeys.SUBSCRIBE_KEY}&template=play-ng&hmr=${hmrValue}`;
    }
    initialize(getInitialFiles) {
        return __awaiter(this, void 0, void 0, function* () {
            const initConfig = this.getInitConfig(getInitialFiles);
            this.messagingService = new nativescript_preview_sdk_1.MessagingService();
            this.instanceId = yield this.messagingService.initialize(initConfig);
        });
    }
    applyChanges(filesPayload) {
        return new Promise((resolve, reject) => {
            this.messagingService.applyChanges(this.instanceId, filesPayload, err => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve();
                }
            });
        });
    }
    stop() {
        this.messagingService.stop();
    }
    getInitConfig(getInitialFiles) {
        return {
            pubnubPublishKey: preview_app_constants_1.PubnubKeys.PUBLISH_KEY,
            pubnubSubscribeKey: preview_app_constants_1.PubnubKeys.SUBSCRIBE_KEY,
            msvKey: "cli",
            msvEnv: this.$config.PREVIEW_APP_ENVIRONMENT,
            showLoadingPage: false,
            callbacks: this.getCallbacks(),
            getInitialFiles
        };
    }
    getCallbacks() {
        return {
            onLogSdkMessage: (log) => {
                this.$logger.trace("Received onLogSdkMessage message: ", log);
            },
            onConnectedDevicesChange: (connectedDevices) => ({}),
            onLogMessage: (log, deviceName, deviceId) => {
                const device = _.find(this.connectedDevices, { id: deviceId });
                this.emit(constants_1.DEVICE_LOG_EVENT_NAME, log, deviceId, device ? device.platform : "");
                this.$logger.info(`LOG from device ${deviceName}: ${log}`);
            },
            onRestartMessage: () => {
                this.$logger.trace("Received onRestartMessage event.");
            },
            onUncaughtErrorMessage: () => {
                this.$logger.warn("The Preview app has terminated unexpectedly. Please run it again to get a detailed crash report.");
            },
            onDeviceConnectedMessage: (deviceConnectedMessage) => ({}),
            onDeviceConnected: (device) => {
                if (!_.find(this.connectedDevices, { id: device.id })) {
                    this.connectedDevices.push(device);
                }
            },
            onDevicesPresence: (devices) => ({}),
            onSendingChange: (sending) => ({}),
            onBiggerFilesUpload: (filesContent, callback) => __awaiter(this, void 0, void 0, function* () {
                const gzippedContent = Buffer.from(pako.gzip(filesContent));
                const byteLength = filesContent.length;
                if (byteLength > PreviewSdkService.MAX_FILES_UPLOAD_BYTE_LENGTH) {
                    this.$logger.warn("The files to upload exceed the maximum allowed size of 15MB. Your app might not work as expected.");
                }
                const playgroundUploadResponse = yield this.$httpClient.httpRequest({
                    url: this.$config.UPLOAD_PLAYGROUND_FILES_ENDPOINT,
                    method: "POST",
                    body: gzippedContent,
                    headers: {
                        "Content-Encoding": "gzip",
                        "Content-Type": "text/plain"
                    }
                });
                const responseBody = JSON.parse(playgroundUploadResponse.body);
                const location = responseBody && responseBody.location;
                callback(location, playgroundUploadResponse.error);
            })
        };
    }
}
PreviewSdkService.MAX_FILES_UPLOAD_BYTE_LENGTH = 15 * 1024 * 1024;
exports.PreviewSdkService = PreviewSdkService;
$injector.register("previewSdkService", PreviewSdkService);
