"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());
    });
};
const iOSProxyServices = require("../../common/mobile/ios/device/ios-proxy-services");
class IOSSocketRequestExecutor {
    constructor($errors, $injector, $iOSNotification, $iOSNotificationService, $logger, $projectData) {
        this.$errors = $errors;
        this.$injector = $injector;
        this.$iOSNotification = $iOSNotification;
        this.$iOSNotificationService = $iOSNotificationService;
        this.$logger = $logger;
        this.$projectData = $projectData;
    }
    executeAttachRequest(device, timeout) {
        return __awaiter(this, void 0, void 0, function* () {
            let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector);
            let data = [this.$iOSNotification.alreadyConnected, this.$iOSNotification.readyForAttach, this.$iOSNotification.attachAvailable]
                .map((notification) => this.$iOSNotificationService.awaitNotification(npc, notification, timeout)), alreadyConnected = data[0], readyForAttach = data[1], attachAvailable = data[2];
            npc.postNotificationAndAttachForData(this.$iOSNotification.attachAvailabilityQuery);
            let receivedNotification;
            try {
                receivedNotification = yield Promise.race([alreadyConnected, readyForAttach, attachAvailable]);
            }
            catch (e) {
                this.$errors.failWithoutHelp(`The application ${this.$projectData.projectId} does not appear to be running on ${device.deviceInfo.displayName} or is not built with debugging enabled.`);
            }
            switch (receivedNotification) {
                case this.$iOSNotification.alreadyConnected:
                    this.$errors.failWithoutHelp("A client is already connected.");
                    break;
                case this.$iOSNotification.attachAvailable:
                    yield this.executeAttachAvailable(npc, timeout);
                    break;
                case this.$iOSNotification.readyForAttach:
                    break;
            }
        });
    }
    executeLaunchRequest(device, timeout, readyForAttachTimeout, shouldBreak) {
        return __awaiter(this, void 0, void 0, function* () {
            let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector);
            try {
                yield this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.appLaunching, timeout);
                process.nextTick(() => {
                    if (shouldBreak) {
                        npc.postNotificationAndAttachForData(this.$iOSNotification.waitForDebug);
                    }
                    npc.postNotificationAndAttachForData(this.$iOSNotification.attachRequest);
                });
                yield this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.readyForAttach, readyForAttachTimeout);
            }
            catch (e) {
                this.$logger.trace(`Timeout error: ${e}`);
                this.$errors.failWithoutHelp("Timeout waiting for response from NativeScript runtime.");
            }
        });
    }
    executeAttachAvailable(npc, timeout) {
        return __awaiter(this, void 0, void 0, function* () {
            process.nextTick(() => npc.postNotificationAndAttachForData(this.$iOSNotification.attachRequest));
            try {
                yield this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.readyForAttach, timeout);
            }
            catch (e) {
                this.$errors.failWithoutHelp(`The application ${this.$projectData.projectId} timed out when performing the socket handshake.`);
            }
        });
    }
}
exports.IOSSocketRequestExecutor = IOSSocketRequestExecutor;
$injector.register("iOSSocketRequestExecutor", IOSSocketRequestExecutor);
