"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ATTRIBUTES = void 0;
const node_path_1 = require("node:path");
const node_os_1 = require("node:os");
const promises_1 = require("node:fs/promises");
const fs = __importStar(require("fs"));
const ftp_1 = __importDefault(require("ftp"));
const FileSystem_1 = require("../FileSystem");
// https://github.com/mscdex/node-ftp
exports.ATTRIBUTES = [
    {
        name: "name",
        type: FileSystem_1.FileAttributeType.String,
        title: "Name"
    },
    {
        name: "size",
        type: FileSystem_1.FileAttributeType.Number,
        title: "Size"
    },
    {
        name: "modified",
        type: FileSystem_1.FileAttributeType.Date,
        title: "Last Modified"
    },
    {
        name: "rights",
        type: FileSystem_1.FileAttributeType.Rights,
        title: "Rights"
    }
];
function parseRights(part) {
    return (part.includes('r') ? 'r' : '-') + (part.includes('w') ? 'w' : '-') + (part.includes('x') ? 'x' : '-');
}
class Ftp extends FileSystem_1.FileSystem {
    constructor(host, user, password, port = 21, onError, onClose = () => { }) {
        super();
        this.host = host;
        this.user = user;
        this.password = password;
        this.port = port;
        this.onError = onError;
        this.onClose = onClose;
        this.connection = new ftp_1.default();
        this.connection.on('close', () => {
            this.connected = undefined;
            this.onClose();
        });
    }
    async open() {
        if (this.connected === undefined) {
            this.connected = new Promise((resolve, reject) => {
                this.connection
                    .on('ready', () => {
                    this.connection.on('error', this.onError);
                    resolve(this.uri = `ftp://${this.user}@${this.host}:${this.port}`);
                })
                    .on('error', reject);
            });
            this.connection.connect({ host: this.host, user: this.user, password: this.password, port: this.port });
        }
        return this.connected;
    }
    close() {
        this.connected != undefined && this.connection.end();
    }
    opened() {
        return this.connected != undefined;
    }
    async pwd() {
        await this.open();
        return new Promise((resolve, reject) => this.connection.pwd((e, pwd) => e ? reject(e) : resolve(pwd)));
    }
    async ls(dir) {
        await this.open();
        return new Promise((resolve, reject) => {
            this.connection.list(dir, (err, list) => {
                err ?
                    reject(err) :
                    resolve(list
                        .filter(f => f.name != '.' && f.name != '..')
                        .map(f => {
                        let target = f.target;
                        if (target && !(0, node_path_1.isAbsolute)(target)) {
                            target = (0, node_path_1.normalize)((0, node_path_1.join)(dir, target));
                        }
                        return {
                            path: (0, node_path_1.join)(dir, f.name),
                            name: f.name,
                            dir: f.type == 'd',
                            size: f.size,
                            modified: f.date,
                            owner: f.owner,
                            group: f.group,
                            rights: f.rights ? parseRights(f.rights.user) + parseRights(f.rights.group) + parseRights(f.rights.other) : '',
                            target
                        };
                    }));
            });
        });
    }
    async get(fromRemote, toLocal) {
        await this.open();
        return new Promise((resolve, reject) => {
            this.connection.get(fromRemote, (err, source) => {
                if (err) {
                    reject(err);
                }
                else {
                    const dest = fs.createWriteStream(toLocal);
                    dest.on('finish', () => resolve());
                    source.pipe(dest);
                }
            });
        });
    }
    async put(fromLocal, toRemote) {
        await this.open();
        return new Promise((resolve, reject) => {
            this.connection.put(fromLocal, toRemote, e => e ? reject(e) : resolve());
        });
    }
    async rm(path, recursive) {
        await this.open();
        return new Promise((resolve, reject) => recursive ?
            this.connection.rmdir(path, true, e => e ? reject(e) : resolve()) :
            this.connection.delete(path, e => e ? reject(e) : resolve()));
    }
    async mkdir(path) {
        await this.open();
        return new Promise((resolve, reject) => this.connection.mkdir(path, true, e => e ? reject(e) : resolve()));
    }
    async rename(from, to) {
        await this.open();
        return new Promise((resolve, reject) => this.connection.rename(from, to, e => e ? reject(e) : resolve()));
    }
    async mv(from, to) {
        await this.open();
        return new Promise((resolve, reject) => this.connection.rename(from, to, e => e ? reject(e) : resolve()));
    }
    async cp(from, to, recursive) {
        return Promise.reject(new Error("FTP doesn't support remote duplication"));
    }
    async write(path, s) {
        const tmp = (0, node_path_1.join)((0, node_os_1.tmpdir)(), (0, node_path_1.basename)(path));
        await (0, promises_1.writeFile)(tmp, s);
        await this.put(tmp, path);
        (0, promises_1.rm)(tmp);
    }
}
exports.default = Ftp;
