"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IOSDeviceLiveSyncService = void 0;
const constants = require("../../constants");
const minimatch_1 = 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;
    }
    async setupSocketIfNeeded(projectData) {
        if (this.socket) {
            return true;
        }
        const appId = projectData.projectIdentifiers.ios;
        try {
            const ensureAppStarted = true;
            this.socket = await 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;
    }
    async removeFiles(deviceAppData, localToDevicePaths) {
        await Promise.all(_.map(localToDevicePaths, (localToDevicePathData) => this.device.fileSystem.deleteFile(localToDevicePathData.getDevicePath(), deviceAppData.appIdentifier)));
    }
    async shouldRestart(projectData, liveSyncInfo) {
        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;
    }
    async tryRefreshApplication(projectData, liveSyncInfo) {
        let didRefresh = true;
        const localToDevicePaths = liveSyncInfo.modifiedFilesData;
        let scriptRelatedFiles = [];
        constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach((pattern) => (scriptRelatedFiles = _.concat(scriptRelatedFiles, localToDevicePaths.filter((file) => (0, minimatch_1.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 = await this.refreshApplicationCore(projectData, liveSyncInfo);
            }
        }
        catch (e) {
            didRefresh = false;
        }
        return didRefresh;
    }
    async refreshApplicationCore(projectData, liveSyncInfo) {
        let didRefresh = true;
        if (this.canRefreshWithNotification(projectData, liveSyncInfo)) {
            didRefresh = await this.refreshWithNotification(projectData);
        }
        else {
            if (await this.setupSocketIfNeeded(projectData)) {
                await this.reloadPage();
            }
            else {
                didRefresh = false;
            }
        }
        return didRefresh;
    }
    async refreshWithNotification(projectData) {
        let didRefresh = false;
        await this.$lockService.executeActionWithLock(async () => {
            didRefresh = await this.$iOSSocketRequestExecutor.executeRefreshRequest(this.device, 5, projectData.projectIdentifiers.ios);
        }, `ios-device-livesync-${this.device.deviceInfo.identifier}-${projectData.projectIdentifiers.ios}.lock`);
        return didRefresh;
    }
    async restartApplication(projectData, liveSyncInfo) {
        await this.device.applicationManager.restartApplication({
            appId: liveSyncInfo.deviceAppData.appIdentifier,
            projectName: projectData.projectName,
            waitForDebugger: liveSyncInfo.waitForDebugger,
            projectDir: projectData.projectDir,
        });
        if (liveSyncInfo.useHotModuleReload) {
            await this.setupSocketIfNeeded(projectData);
        }
    }
    async reloadPage() {
        const message = JSON.stringify({
            method: "Page.reload",
            params: {
                ignoreCache: false,
            },
            id: ++currentPageReloadId,
        });
        await 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()}`);
        });
    }
    async sendMessage(message) {
        try {
            await 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);
            await this.destroySocket();
        }
    }
    async destroySocket() {
        if (this.socket) {
            await this.device.destroyAllSockets();
            this.socket = null;
        }
    }
}
exports.IOSDeviceLiveSyncService = IOSDeviceLiveSyncService;
IOSDeviceLiveSyncService.MIN_RUNTIME_VERSION_WITH_REFRESH_NOTIFICATION = "6.1.0";
__decorate([
    (0, decorators_1.performanceLog)()
], IOSDeviceLiveSyncService.prototype, "removeFiles", null);
