"use strict";
exports.__esModule = true;
exports.Simulator = void 0;
var socket_io_client_1 = require("socket.io-client");
var index_1 = require("./index");
var simulatorStorageKey = index_1.config.simulatorStorageKey;
var Simulator = (function () {
    function Simulator(url) {
        this.previewId = 1;
        this.previewMethod = 'ide.simulator.preview';
        index_1.logger.info('connecting to simulator');
        this.socket = (0, socket_io_client_1.io)(url);
        this.socket.on('connect', function () {
            var _a;
            index_1.globalStorage.set((_a = {}, _a[simulatorStorageKey.simulatorConnectStatus] = 'connected', _a));
            index_1.logger.success('simulator connected');
        });
        this.socket.on('disconnect', function () {
            var _a;
            index_1.globalStorage.set((_a = {}, _a[simulatorStorageKey.simulatorConnectStatus] = 'disconnected', _a));
            index_1.logger.error('simulator disconnected, please restart the simulator');
        });
    }
    Simulator.prototype.sendMessage = function (message) {
        this.socket.send(message);
    };
    Simulator.prototype.emit = function (space, jsonData) {
        this.socket.emit(space, jsonData);
    };
    Simulator.prototype.upload = function (data, projectName, target, appid, devices) {
        var dataArr = this.uint8ToArray(new Uint8Array(data));
        var message = this.encodeMessage({
            target: target,
            projectName: projectName,
            appid: appid,
            size: data.byteLength,
            data: dataArr,
            devices: devices
        });
        this.sendMessage(message);
    };
    Simulator.prototype.encodeMessage = function (params) {
        return JSON.stringify({
            jsonrpc: '2.0',
            method: this.previewMethod,
            params: params,
            id: this.previewId
        });
    };
    Simulator.prototype.decodeMessage = function (data) {
        return JSON.parse(data);
    };
    Simulator.prototype.uint8ToArray = function (source) {
        return Array.prototype.slice.call(source);
    };
    return Simulator;
}());
exports.Simulator = Simulator;
