"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 });
const path = require("path");
const preview_app_constants_1 = require("./preview-app-constants");
const constants_1 = require("../../../constants");
const isTextOrBinary = require('istextorbinary');
class PreviewAppLiveSyncService {
    constructor($fs, $hooksService, $logger, $platformService, $platformsData, $projectDataService, $previewSdkService, $previewAppPluginsService, $projectFilesManager, $projectFilesProvider) {
        this.$fs = $fs;
        this.$hooksService = $hooksService;
        this.$logger = $logger;
        this.$platformService = $platformService;
        this.$platformsData = $platformsData;
        this.$projectDataService = $projectDataService;
        this.$previewSdkService = $previewSdkService;
        this.$previewAppPluginsService = $previewAppPluginsService;
        this.$projectFilesManager = $projectFilesManager;
        this.$projectFilesProvider = $projectFilesProvider;
        this.excludedFileExtensions = [".ts", ".sass", ".scss", ".less"];
        this.excludedFiles = [".DS_Store"];
    }
    initialize() {
        this.$previewSdkService.initialize();
    }
    initialSync(data) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$previewSdkService.on(preview_app_constants_1.PreviewSdkEventNames.DEVICE_CONNECTED, (device) => __awaiter(this, void 0, void 0, function* () {
                this.$logger.trace("Found connected device", device);
                const filesToSyncMap = {};
                let promise = Promise.resolve();
                const startSyncFilesTimeout = (platform) => __awaiter(this, void 0, void 0, function* () {
                    yield promise
                        .then(() => __awaiter(this, void 0, void 0, function* () {
                        promise = this.syncFilesForPlatformSafe(data, platform, filesToSyncMap[platform]);
                        yield promise;
                    }));
                    filesToSyncMap[platform] = [];
                });
                yield this.$hooksService.executeBeforeHooks("preview-sync", {
                    hookArgs: {
                        projectData: this.$projectDataService.getProjectData(data.projectDir),
                        config: {
                            env: data.env,
                            platform: device.platform,
                            appFilesUpdaterOptions: data.appFilesUpdaterOptions,
                        },
                        filesToSyncMap,
                        startSyncFilesTimeout: startSyncFilesTimeout.bind(this)
                    }
                });
                yield this.$previewAppPluginsService.comparePluginsOnDevice(device);
                yield this.syncFilesForPlatformSafe(data, device.platform);
            }));
        });
    }
    syncFiles(data, files) {
        return __awaiter(this, void 0, void 0, function* () {
            this.showWarningsForNativeFiles(files);
            for (const device of this.$previewSdkService.connectedDevices) {
                yield this.$previewAppPluginsService.comparePluginsOnDevice(device);
            }
            const platforms = _(this.$previewSdkService.connectedDevices)
                .map(device => device.platform)
                .uniq()
                .value();
            for (const platform of platforms) {
                yield this.syncFilesForPlatformSafe(data, platform, files);
            }
        });
    }
    stopLiveSync() {
        return __awaiter(this, void 0, void 0, function* () {
            this.$previewSdkService.removeAllListeners();
            this.$previewSdkService.stop();
        });
    }
    syncFilesForPlatformSafe(data, platform, files) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$logger.info(`Start syncing changes for platform ${platform}.`);
            try {
                const { appFilesUpdaterOptions, env, projectDir } = data;
                const projectData = this.$projectDataService.getProjectData(projectDir);
                yield this.preparePlatform(platform, appFilesUpdaterOptions, env, projectData);
                yield this.applyChanges(projectData, platform, files);
                this.$logger.info(`Successfully synced changes for platform ${platform}.`);
            }
            catch (err) {
                this.$logger.warn(`Unable to apply changes for platform ${platform}. Error is: ${err}, ${JSON.stringify(err, null, 2)}.`);
            }
        });
    }
    applyChanges(projectData, platform, files) {
        return __awaiter(this, void 0, void 0, function* () {
            const platformData = this.$platformsData.getPlatformData(platform, projectData);
            const payloads = this.getFilePayloads(platformData, projectData, _(files).uniq().value());
            yield this.$previewSdkService.applyChanges(payloads, platform);
        });
    }
    getFilePayloads(platformData, projectData, files) {
        const appFolderPath = path.join(projectData.projectDir, constants_1.APP_FOLDER_NAME);
        const platformsAppFolderPath = path.join(platformData.appDestinationDirectoryPath, constants_1.APP_FOLDER_NAME);
        if (files && files.length) {
            files = files.map(file => path.join(platformsAppFolderPath, path.relative(appFolderPath, file)));
        }
        else {
            files = this.$projectFilesManager.getProjectFiles(platformsAppFolderPath);
        }
        const filesToTransfer = files
            .filter(file => file.indexOf(constants_1.TNS_MODULES_FOLDER_NAME) === -1)
            .filter(file => file.indexOf(constants_1.APP_RESOURCES_FOLDER_NAME) === -1)
            .filter(file => !_.includes(this.excludedFiles, path.basename(file)))
            .filter(file => !_.includes(this.excludedFileExtensions, path.extname(file)));
        this.$logger.trace(`Transferring ${filesToTransfer.join("\n")}.`);
        const payloads = filesToTransfer
            .map(file => {
            const projectFileInfo = this.$projectFilesProvider.getProjectFileInfo(file, platformData.normalizedPlatformName, null);
            const relativePath = path.relative(platformsAppFolderPath, file);
            const filePayload = {
                event: preview_app_constants_1.PreviewSdkEventNames.CHANGE_EVENT_NAME,
                file: path.join(path.dirname(relativePath), projectFileInfo.onDeviceFileName),
                binary: isTextOrBinary.isBinarySync(file),
                fileContents: ""
            };
            if (filePayload.binary) {
                const bitmap = this.$fs.readFile(file);
                const base64 = Buffer.from(bitmap).toString('base64');
                filePayload.fileContents = base64;
            }
            else {
                filePayload.fileContents = this.$fs.readText(path.join(path.dirname(projectFileInfo.filePath), projectFileInfo.onDeviceFileName));
            }
            return filePayload;
        });
        return payloads;
    }
    preparePlatform(platform, appFilesUpdaterOptions, env, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            const nativePrepare = { skipNativePrepare: true };
            const config = {};
            const platformTemplate = null;
            const prepareInfo = {
                platform,
                appFilesUpdaterOptions,
                env,
                projectData,
                nativePrepare,
                config,
                platformTemplate,
                skipCopyTnsModules: true,
                skipCopyAppResourcesFiles: true
            };
            yield this.$platformService.preparePlatform(prepareInfo);
        });
    }
    showWarningsForNativeFiles(files) {
        _.filter(files, file => file.indexOf(constants_1.APP_RESOURCES_FOLDER_NAME) > -1)
            .forEach(file => this.$logger.warn(`Unable to apply changes from ${constants_1.APP_RESOURCES_FOLDER_NAME} folder. You need to build your application in order to make changes in ${constants_1.APP_RESOURCES_FOLDER_NAME} folder.`));
    }
}
exports.PreviewAppLiveSyncService = PreviewAppLiveSyncService;
$injector.register("previewAppLiveSyncService", PreviewAppLiveSyncService);
