"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreviewAppFilesService = void 0;
const path = require("path");
const _ = require("lodash");
const constants_1 = require("../../../constants");
const preview_app_constants_1 = require("./preview-app-constants");
const yok_1 = require("../../../common/yok");
const istextorbinary_1 = require("istextorbinary");
class PreviewAppFilesService {
    constructor($fs, $logger, $platformsDataService, $projectDataService, $projectFilesManager, $projectFilesProvider) {
        this.$fs = $fs;
        this.$logger = $logger;
        this.$platformsDataService = $platformsDataService;
        this.$projectDataService = $projectDataService;
        this.$projectFilesManager = $projectFilesManager;
        this.$projectFilesProvider = $projectFilesProvider;
        this.excludedFileExtensions = [".ts", ".sass", ".scss", ".less"];
        this.excludedFiles = [".DS_Store"];
    }
    getInitialFilesPayload(data, platform, deviceId) {
        const rootFilesDir = this.getRootFilesDir(data, platform);
        const filesToSync = this.$projectFilesManager.getProjectFiles(rootFilesDir);
        const payloads = this.getFilesPayload(data, { filesToSync }, platform, deviceId);
        return payloads;
    }
    getFilesPayload(data, filesData, platform, deviceId) {
        const { filesToSync, filesToRemove } = filesData;
        const filesToTransfer = filesToSync
            .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(`Sending ${filesToTransfer.join("\n")}.`);
        const files = this.createFilePayloads(data, platform, filesToTransfer, filesToRemove);
        return {
            files,
            platform,
            hmrMode: data.useHotModuleReload ? 1 : 0,
            deviceId,
        };
    }
    createFilePayloads(data, platform, filesToTransfer, filesToRemove) {
        const rootFilesDir = this.getRootFilesDir(data, platform);
        const payloadsToSync = _.filter(filesToTransfer, (file) => {
            const fileInfo = this.$projectFilesProvider.getProjectFileInfo(file, platform, {});
            return fileInfo && fileInfo.shouldIncludeFile;
        }).map((file) => this.createFilePayload(file, platform, rootFilesDir, preview_app_constants_1.PreviewSdkEventNames.CHANGE_EVENT_NAME));
        const payloadsToRemove = _.map(filesToRemove, (file) => this.createFilePayload(file, platform, rootFilesDir, preview_app_constants_1.PreviewSdkEventNames.UNLINK_EVENT_NAME));
        const payloads = payloadsToSync.concat(payloadsToRemove);
        return payloads;
    }
    createFilePayload(file, platform, rootFilesDir, event) {
        let fileContents = "";
        let binary = false;
        if (event === preview_app_constants_1.PreviewSdkEventNames.CHANGE_EVENT_NAME) {
            binary = (0, istextorbinary_1.isBinary)(file);
            if (binary) {
                const bitmap = this.$fs.readFile(file);
                const base64 = Buffer.from(bitmap).toString("base64");
                fileContents = base64;
            }
            else {
                fileContents = this.$fs.readText(file);
            }
        }
        const filePayload = {
            event,
            file: path.relative(rootFilesDir, file),
            binary,
            fileContents,
        };
        return filePayload;
    }
    getRootFilesDir(data, platform) {
        const projectData = this.$projectDataService.getProjectData(data.projectDir);
        const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
        const rootFilesDir = path.join(platformData.appDestinationDirectoryPath, constants_1.APP_FOLDER_NAME);
        return rootFilesDir;
    }
}
exports.PreviewAppFilesService = PreviewAppFilesService;
yok_1.injector.register("previewAppFilesService", PreviewAppFilesService);
