"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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.IOSDeviceLiveSyncService = void 0;
const constants = require("../../constants");
const minimatch = require("minimatch");
const _ = require("lodash");
const device_livesync_service_base_1 = require("./device-livesync-service-base");
const decorators_1 = require("../../common/decorators");
const semver = require("semver");
let currentPageReloadId = 0;
class IOSDeviceLiveSyncService extends device_livesync_service_base_1.DeviceLiveSyncServiceBase {
    constructor($logger, $iOSSocketRequestExecutor, $devicePlatformsConstants, $lockService, platformsDataService, $platformCommandHelper, device) {
        super(platformsDataService, device);
        this.$logger = $logger;
        this.$iOSSocketRequestExecutor = $iOSSocketRequestExecutor;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$lockService = $lockService;
        this.platformsDataService = platformsDataService;
        this.$platformCommandHelper = $platformCommandHelper;
        this.device = device;
    }
    canRefreshWithNotification(projectData, liveSyncInfo) {
        if (liveSyncInfo && liveSyncInfo.forceRefreshWithSocket) {
            return false;
        }
        if (this.device.isEmulator) {
            return false;
        }
        const currentRuntimeVersion = this.$platformCommandHelper.getCurrentPlatformVersion(this.$devicePlatformsConstants.iOS, projectData);
        const canRefresh = semver.gte(semver.coerce(currentRuntimeVersion), IOSDeviceLiveSyncService.MIN_RUNTIME_VERSION_WITH_REFRESH_NOTIFICATION);
        return canRefresh;
    }
    setupSocketIfNeeded(projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.socket) {
                return true;
            }
            const appId = projectData.projectIdentifiers.ios;
            try {
                const ensureAppStarted = true;
                this.socket = yield this.device.getDebugSocket(appId, projectData.projectName, projectData.projectDir, ensureAppStarted);
            }
            catch (err) {
                this.$logger.trace(`Error while connecting to the debug socket. Error is:`, err);
            }
            if (!this.socket) {
                return false;
            }
            this.attachEventHandlers();
            return true;
        });
    }
    removeFiles(deviceAppData, localToDevicePaths) {
        return __awaiter(this, void 0, void 0, function* () {
            yield Promise.all(_.map(localToDevicePaths, (localToDevicePathData) => this.device.fileSystem.deleteFile(localToDevicePathData.getDevicePath(), deviceAppData.appIdentifier)));
        });
    }
    shouldRestart(projectData, liveSyncInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            let shouldRestart = false;
            const deviceAppData = liveSyncInfo.deviceAppData;
            const localToDevicePaths = liveSyncInfo.modifiedFilesData;
            if (liveSyncInfo.isFullSync || liveSyncInfo.waitForDebugger) {
                shouldRestart = true;
            }
            else {
                const canExecuteFastSync = this.canExecuteFastSyncForPaths(liveSyncInfo, localToDevicePaths, projectData, deviceAppData.platform);
                const isRefreshConnectionSetup = this.canRefreshWithNotification(projectData, liveSyncInfo) ||
                    (!this.device.isOnlyWiFiConnected && this.socket);
                if (!canExecuteFastSync || !isRefreshConnectionSetup) {
                    shouldRestart = true;
                }
            }
            return shouldRestart;
        });
    }
    tryRefreshApplication(projectData, liveSyncInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            let didRefresh = true;
            const localToDevicePaths = liveSyncInfo.modifiedFilesData;
            let scriptRelatedFiles = [];
            constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach((pattern) => (scriptRelatedFiles = _.concat(scriptRelatedFiles, localToDevicePaths.filter((file) => minimatch(file.getDevicePath(), pattern, { nocase: true })))));
            const scriptFiles = _.filter(localToDevicePaths, (localToDevicePath) => _.endsWith(localToDevicePath.getDevicePath(), ".js"));
            const otherFiles = _.difference(localToDevicePaths, _.concat(scriptFiles, scriptRelatedFiles));
            try {
                if (otherFiles.length) {
                    didRefresh = yield this.refreshApplicationCore(projectData, liveSyncInfo);
                }
            }
            catch (e) {
                didRefresh = false;
            }
            return didRefresh;
        });
    }
    refreshApplicationCore(projectData, liveSyncInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            let didRefresh = true;
            if (this.canRefreshWithNotification(projectData, liveSyncInfo)) {
                didRefresh = yield this.refreshWithNotification(projectData);
            }
            else {
                if (yield this.setupSocketIfNeeded(projectData)) {
                    yield this.reloadPage();
                }
                else {
                    didRefresh = false;
                }
            }
            return didRefresh;
        });
    }
    refreshWithNotification(projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            let didRefresh = false;
            yield this.$lockService.executeActionWithLock(() => __awaiter(this, void 0, void 0, function* () {
                didRefresh = yield this.$iOSSocketRequestExecutor.executeRefreshRequest(this.device, 5, projectData.projectIdentifiers.ios);
            }), `ios-device-livesync-${this.device.deviceInfo.identifier}-${projectData.projectIdentifiers.ios}.lock`);
            return didRefresh;
        });
    }
    restartApplication(projectData, liveSyncInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.device.applicationManager.restartApplication({
                appId: liveSyncInfo.deviceAppData.appIdentifier,
                projectName: projectData.projectName,
                waitForDebugger: liveSyncInfo.waitForDebugger,
                projectDir: projectData.projectDir,
            });
            if (liveSyncInfo.useHotModuleReload) {
                yield this.setupSocketIfNeeded(projectData);
            }
        });
    }
    reloadPage() {
        return __awaiter(this, void 0, void 0, function* () {
            const message = JSON.stringify({
                method: "Page.reload",
                params: {
                    ignoreCache: false,
                },
                id: ++currentPageReloadId,
            });
            yield this.sendMessage(message);
        });
    }
    attachEventHandlers() {
        this.socket.on("close", (hadError) => {
            this.$logger.trace(`Socket closed, hadError is ${hadError}.`);
            this.socket = null;
        });
        this.socket.on("error", (error) => {
            this.$logger.trace(`Socket error received: ${error}`);
        });
        this.socket.on("data", (data) => {
            this.$logger.trace(`Socket sent data: ${data.toString()}`);
        });
    }
    sendMessage(message) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield new Promise((resolve, reject) => {
                    let isResolved = false;
                    const length = Buffer.byteLength(message, "utf16le");
                    const payload = Buffer.allocUnsafe(length + 4);
                    payload.writeInt32BE(length, 0);
                    payload.write(message, 4, length, "utf16le");
                    const errorCallback = (error) => {
                        if (!isResolved) {
                            isResolved = true;
                            reject(error);
                        }
                    };
                    this.socket.once("error", errorCallback);
                    this.socket.write(payload, "utf16le", () => {
                        this.socket.removeListener("error", errorCallback);
                        if (!isResolved) {
                            isResolved = true;
                            resolve();
                        }
                    });
                });
            }
            catch (error) {
                this.$logger.trace("Error while sending message:", error);
                yield this.destroySocket();
            }
        });
    }
    destroySocket() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.socket) {
                yield this.device.destroyAllSockets();
                this.socket = null;
            }
        });
    }
}
IOSDeviceLiveSyncService.MIN_RUNTIME_VERSION_WITH_REFRESH_NOTIFICATION = "6.1.0";
__decorate([
    (0, decorators_1.performanceLog)()
], IOSDeviceLiveSyncService.prototype, "removeFiles", null);
exports.IOSDeviceLiveSyncService = IOSDeviceLiveSyncService;
