"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLocal = isLocal;
exports.connectionID = connectionID;
exports.parseURI = parseURI;
exports.createURI = createURI;
const types_1 = require("../types");
const whatwg_url_1 = require("whatwg-url");
function isLocal(uri) {
    return uri.startsWith('file:');
}
function connectionID(scheme, user, host, port) {
    return scheme == 'file' ? types_1.LocalFileSystemID : `${scheme}://${user}@${host}:${port}`;
}
// https://url.spec.whatwg.org/#special-scheme
// https://jsdom.github.io/whatwg-url/#url=bmF0czovL2xvY2FsaG9zdDo0MjAwLw==&base=ZmlsZTovLy8=
function parseURI(uri) {
    const { protocol, pathname, username, hostname, port: p } = new whatwg_url_1.URL(uri);
    const port = p ? parseInt(p) : defaultPort(protocol.slice(0, -1));
    return {
        id: connectionID(protocol.slice(0, -1), username, hostname, port),
        scheme: protocol.slice(0, -1),
        path: decodeURI(pathname),
        user: username,
        host: hostname,
        port
    };
}
function createURI(id, path) {
    const u = (0, whatwg_url_1.parseURL)(id);
    u.path = path;
    if (!u.port) {
        u.port = defaultPort(u.scheme);
    }
    return (0, whatwg_url_1.serializeURL)(u);
}
function defaultPort(protocol) {
    switch (protocol) {
        case 'ftp': return 21;
        case 'sftp': return 22;
        case 'http': return 80;
    }
    return null;
}
