"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 });
class IOSDeviceBase {
    constructor() {
        this.cachedSockets = {};
    }
    getApplicationInfo(applicationIdentifier) {
        return this.applicationManager.getApplicationInfo(applicationIdentifier);
    }
    getLiveSyncSocket(appId) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.getSocket(appId);
        });
    }
    getDebugSocket(appId) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.getSocket(appId);
        });
    }
    getSocket(appId) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.$lockService.executeActionWithLock(() => __awaiter(this, void 0, void 0, function* () {
                if (this.cachedSockets[appId]) {
                    return this.cachedSockets[appId];
                }
                this.cachedSockets[appId] = yield this.getSocketCore(appId);
                if (this.cachedSockets[appId]) {
                    this.cachedSockets[appId].on("close", () => {
                        this.destroySocket(appId);
                    });
                    this.$processService.attachToProcessExitSignals(this, () => this.destroySocket(appId));
                }
                return this.cachedSockets[appId];
            }), "ios-debug-socket.lock");
        });
    }
    destroyLiveSyncSocket(appId) {
        this.destroySocket(appId);
    }
    destroyDebugSocket(appId) {
        this.destroySocket(appId);
    }
    getDebuggerPort(appId) {
        return __awaiter(this, void 0, void 0, function* () {
            const port = yield this.$iOSDebuggerPortService.getPort({ deviceId: this.deviceInfo.identifier, appId });
            if (!port) {
                this.$errors.failWithoutHelp("Device socket port cannot be found.");
            }
            return port;
        });
    }
    destroyAllSockets() {
        for (const appId in this.cachedSockets) {
            this.destroySocketSafe(this.cachedSockets[appId]);
        }
        this.cachedSockets = {};
    }
    destroySocket(appId) {
        this.destroySocketSafe(this.cachedSockets[appId]);
        this.cachedSockets[appId] = null;
    }
    destroySocketSafe(socket) {
        if (socket && !socket.destroyed) {
            socket.destroy();
        }
    }
}
exports.IOSDeviceBase = IOSDeviceBase;
