"use strict";
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.IOSSocketRequestExecutor = void 0;
const constants = require("../../common/constants");
const yok_1 = require("../../common/yok");
class IOSSocketRequestExecutor {
    constructor($errors, $iOSNotification, $iOSNotificationService) {
        this.$errors = $errors;
        this.$iOSNotification = $iOSNotification;
        this.$iOSNotificationService = $iOSNotificationService;
    }
    executeAttachRequest(device, timeout, appId) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceId = device.deviceInfo.identifier;
            const mainRequestName = this.$iOSNotification.getAttachRequest(appId, deviceId);
            const readyRequestName = this.$iOSNotification.getReadyForAttach(appId);
            yield this.executeRequest(mainRequestName, readyRequestName, appId, deviceId, timeout);
        });
    }
    executeRefreshRequest(device, timeout, appId) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceId = device.deviceInfo.identifier;
            const mainRequestName = this.$iOSNotification.getRefreshRequest(appId);
            const refreshRequestStartedName = this.$iOSNotification.getAppRefreshStarted(appId);
            const result = yield this.executeRequest(mainRequestName, refreshRequestStartedName, appId, deviceId, timeout);
            return result;
        });
    }
    executeRequest(mainRequestName, successfulyExecutedNotificationName, appId, deviceId, timeout) {
        return __awaiter(this, void 0, void 0, function* () {
            let isSuccessful = false;
            try {
                // We should create this promise here because we need to send the ObserveNotification on the device
                // before we send the PostNotification.
                const socket = yield this.$iOSNotificationService.postNotification(deviceId, successfulyExecutedNotificationName, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
                const notificationPromise = this.$iOSNotificationService.awaitNotification(deviceId, +socket, timeout);
                yield this.$iOSNotificationService.postNotification(deviceId, mainRequestName);
                yield notificationPromise;
                isSuccessful = true;
            }
            catch (e) {
                this.$errors.fail(`The application ${appId} does not appear to be running on ${deviceId} or is not built with debugging enabled. Try starting the application manually.`);
            }
            return isSuccessful;
        });
    }
}
exports.IOSSocketRequestExecutor = IOSSocketRequestExecutor;
yok_1.injector.register("iOSSocketRequestExecutor", IOSSocketRequestExecutor);
//# sourceMappingURL=socket-request-executor.js.map