"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.explorerSettings = explorerSettings;
exports.default = default_1;
const node_fs_1 = require("node:fs");
const path_1 = require("path");
const types_1 = require("../types");
const Local_1 = require("../fs/Local");
const URI_1 = require("../utils/URI");
const Connection_1 = __importDefault(require("../Connection"));
const Password_1 = __importDefault(require("../Password"));
const Session_1 = __importDefault(require("../Session"));
const ramda_1 = require("ramda");
function explorerSettings(attributes, config) {
    return {
        columns: config ? [
            ...config.columns
                .map(({ name, width }) => {
                const attribute = attributes.find((0, ramda_1.whereEq)({ name }));
                return attribute ? { ...attribute, visible: true, width } : null;
            })
                .filter(ramda_1.isNotNil),
            ...attributes
                .filter(({ name }) => !config.columns.find(c => name == c.name))
                .map(a => ({ ...a, visible: false, width: 300 }))
        ] : attributes.map(a => ({ ...a, visible: true, width: 300 })),
        ...({
            sort: config?.sort ?? ['name', types_1.SortOrder.Asc],
            history: config?.history ?? [],
            filter: config?.filter ?? null
        })
    };
}
async function default_1(file, onError) {
    const stat = (0, node_fs_1.statSync)(file);
    if (!stat.isFile() && !stat.isSymbolicLink()) {
        return false;
    }
    let config;
    try {
        config = JSON.parse((0, node_fs_1.readFileSync)(file).toString());
    }
    catch (e) {
        throw new Error(`Invalid connection file ${file}`);
    }
    if (!(0, ramda_1.where)({
        scheme: ramda_1.isNotEmpty,
        user: ramda_1.isNotEmpty,
        host: ramda_1.isNotEmpty,
        port: ramda_1.isNotEmpty
    }, config)) {
        throw new Error(`Invalid connection file ${file}`);
    }
    const id = (0, URI_1.connectionID)(config.scheme, config.user, config.host, config.port);
    try {
        await Password_1.default.get(id);
    }
    catch (e) {
        return false;
    }
    try {
        const attributes = await Connection_1.default.open(config.scheme, config.user, config.host, config.port);
        const pwd = await Connection_1.default.get(id).pwd();
        const settings = {
            name: (0, path_1.parse)(file).name,
            attributes,
            pwd,
            theme: config.theme ?? 'black',
            local: explorerSettings(Local_1.ATTRIBUTES, config.local),
            remote: explorerSettings(attributes, config.remote),
            path: {
                local: config.path?.local,
                remote: config.path?.remote ?? pwd
            },
            sync: config.sync ?? null
        };
        return { id, sid: Session_1.default.create(), settings };
    }
    catch (e) {
        Password_1.default.delete(id, false);
        onError(id, e);
        return false;
    }
}
