"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IOSDeviceFileSystem = void 0;
const os_1 = require("os");
const _ = require("lodash");
class IOSDeviceFileSystem {
    constructor(device, $logger, $iosDeviceOperations, $fs) {
        this.device = device;
        this.$logger = $logger;
        this.$iosDeviceOperations = $iosDeviceOperations;
        this.$fs = $fs;
    }
    async listFiles(devicePath, appIdentifier) {
        if (!devicePath) {
            devicePath = ".";
        }
        this.$logger.info("Listing %s", devicePath);
        const deviceIdentifier = this.device.deviceInfo.identifier;
        let children = [];
        const result = await this.$iosDeviceOperations.listDirectory([
            { deviceId: deviceIdentifier, path: devicePath, appId: appIdentifier },
        ]);
        children = result[deviceIdentifier][0].response;
        this.$logger.info(children.join(os_1.EOL));
    }
    async getFile(deviceFilePath, appIdentifier, outputFilePath) {
        if (outputFilePath) {
            await this.$iosDeviceOperations.downloadFiles([
                {
                    appId: appIdentifier,
                    deviceId: this.device.deviceInfo.identifier,
                    source: deviceFilePath,
                    destination: outputFilePath,
                },
            ]);
            return;
        }
        const fileContent = await this.getFileContent(deviceFilePath, appIdentifier);
        this.$logger.info(fileContent);
    }
    async getFileContent(deviceFilePath, appIdentifier) {
        const result = await this.$iosDeviceOperations.readFiles([
            {
                deviceId: this.device.deviceInfo.identifier,
                path: deviceFilePath,
                appId: appIdentifier,
            },
        ]);
        const response = result[this.device.deviceInfo.identifier][0];
        return response.response;
    }
    async putFile(localFilePath, deviceFilePath, appIdentifier) {
        await this.uploadFilesCore([
            {
                appId: appIdentifier,
                deviceId: this.device.deviceInfo.identifier,
                files: [{ source: localFilePath, destination: deviceFilePath }],
            },
        ]);
    }
    async deleteFile(deviceFilePath, appIdentifier) {
        await this.$iosDeviceOperations.deleteFiles([
            {
                appId: appIdentifier,
                destination: deviceFilePath,
                deviceId: this.device.deviceInfo.identifier,
            },
        ], (err) => {
            this.$logger.trace(`Error while deleting file: ${deviceFilePath}: ${err.message} with code: ${err.code}`);
            if (err.code !== IOSDeviceFileSystem.AFC_DELETE_FILE_NOT_FOUND_ERROR) {
                this.$logger.warn(`Cannot delete file: ${deviceFilePath}. Reason: ${err.message}`);
            }
        });
    }
    async transferFiles(deviceAppData, localToDevicePaths) {
        const filesToUpload = _.filter(localToDevicePaths, (l) => this.$fs.getFsStats(l.getLocalPath()).isFile());
        const files = filesToUpload.map((l) => ({
            source: l.getLocalPath(),
            destination: l.getDevicePath(),
        }));
        await this.uploadFilesCore([
            {
                deviceId: this.device.deviceInfo.identifier,
                appId: deviceAppData.appIdentifier,
                files: files,
            },
        ]);
        return filesToUpload;
    }
    async transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath) {
        await this.transferFiles(deviceAppData, localToDevicePaths);
        return localToDevicePaths;
    }
    async updateHashesOnDevice(hashes, appIdentifier) {
        return;
    }
    async uploadFilesCore(filesToUpload) {
        await this.$iosDeviceOperations.uploadFiles(filesToUpload, (err) => {
            if (err.deviceId === this.device.deviceInfo.identifier) {
                throw err;
            }
        });
    }
}
exports.IOSDeviceFileSystem = IOSDeviceFileSystem;
IOSDeviceFileSystem.AFC_DELETE_FILE_NOT_FOUND_ERROR = 8;
