"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogFS = exports.err = exports.id = void 0;
const FileSystem_1 = require("./FileSystem");
const chalk = import('chalk');
// const logger = new Console({
//     stdout: createWriteStream(join(homedir(), '.f5', 'app.log')),
//     stderr: createWriteStream(join(homedir(), '.f5', 'error.log')),
// })
const logger = console;
exports.default = logger;
const cmd = async (cmd) => (await chalk).default.bold.bgGreen(cmd);
const id = async (id) => (await chalk).default.yellow(id);
exports.id = id;
const err = async (error) => (await chalk).default.red('⚠ ' + error);
exports.err = err;
class LogFS extends FileSystem_1.FileSystem {
    constructor(id, fs) {
        super();
        this.id = id;
        this.fs = fs;
    }
    async open() {
        logger.log(await cmd('CONNECT'), await (0, exports.id)(this.id), '...');
        try {
            const res = await this.fs.open();
            logger.log(await cmd('CONNECTED'), await (0, exports.id)(this.id));
            return res;
        }
        catch (e) {
            let msg = `Could not connect to ${this.id}`;
            if ('message' in e) {
                msg += ': ' + e.message;
            }
            logger.error(await (0, exports.err)(msg));
            throw new Error(msg);
        }
    }
    async close() {
        logger.log(await cmd('CLOSE'), await (0, exports.id)(this.id));
        this.fs.close();
    }
    opened() {
        return this.fs.opened();
    }
    async pwd() {
        logger.log(await cmd('PWD'), await (0, exports.id)(this.id));
        try {
            return await this.fs.pwd();
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async ls(dir) {
        logger.log(await cmd('LS'), await (0, exports.id)(this.id) + dir);
        try {
            return await this.fs.ls(dir);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async get(remote, local) {
        logger.log(await cmd('GET'), `${local} ← ` + await (0, exports.id)(this.id) + remote);
        try {
            return await this.fs.get(remote, local);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async put(local, remote) {
        logger.log(await cmd('PUT'), `${local} → ` + await (0, exports.id)(this.id) + remote);
        try {
            return await this.fs.put(local, remote);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async rm(path, recursive) {
        logger.log(await cmd(recursive ? 'RMDIR' : 'RM'), await (0, exports.id)(this.id) + path);
        try {
            return await this.fs.rm(path, recursive);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async mkdir(path) {
        logger.log(await cmd('MKDIR'), await (0, exports.id)(this.id) + path);
        try {
            return await this.fs.mkdir(path);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async rename(from, to) {
        logger.log(await cmd('RENAME'), await (0, exports.id)(this.id) + from, ' → ', await (0, exports.id)(this.id) + to);
        try {
            return await this.fs.rename(from, to);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
    async mv(from, to) {
        logger.log(await cmd('MV'), await (0, exports.id)(this.id) + from, ' → ', await (0, exports.id)(this.id) + to);
        try {
            return await this.fs.mv(from, to);
        }
        catch (e) {
            logger.error(await (0, exports.err)(e.message));
            throw e;
        }
    }
    async cp(from, to, recursive) {
        logger.log(await cmd(recursive ? 'CP -R' : 'CP'), await (0, exports.id)(this.id) + from, ' → ', await (0, exports.id)(this.id) + to);
        try {
            return await this.fs.cp(from, to, recursive);
        }
        catch (e) {
            logger.error(await (0, exports.err)(e.message));
            throw e;
        }
    }
    async write(path, data) {
        logger.log(await cmd('WRITE'), await (0, exports.id)(this.id) + path);
        try {
            return await this.fs.write(path, data);
        }
        catch (e) {
            logger.error(e);
            throw e;
        }
    }
}
exports.LogFS = LogFS;
