"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = exports.resolve = void 0;
const path_1 = require("path");
function resolve(source, dependency) {
    if (dependency === '@refloat/context') {
        return dependency;
    }
    const relative = dependency.startsWith('./') || dependency.startsWith('../');
    let path = dependency;
    if (!path.endsWith('.js')) {
        path += '.js';
    }
    if (relative) {
        return path;
    }
    let scheme = null;
    if (path[0] === '.') {
        path = (0, path_1.normalize)((0, path_1.join)((0, path_1.dirname)(source), path));
    }
    else {
        const parsed = parseScheme(path);
        scheme = parsed.scheme;
        path = (0, path_1.normalize)(parsed.path);
    }
    if (path.endsWith('/') || (!relative && path.split('/').length <= 3)) {
        path = (0, path_1.normalize)((0, path_1.join)(path, 'index.js'));
    }
    return relative ? path : `${scheme}://${path}`;
}
exports.resolve = resolve;
function parse(source) {
    const { scheme, path: schemePath } = parseScheme(source);
    const [host, owner, repoPath, ...paths] = (0, path_1.normalize)(schemePath).split('/');
    const path = paths.join('/');
    if (typeof host === 'string' &&
        typeof owner === 'string' &&
        typeof repoPath === 'string') {
        const [repo, ref = null] = repoPath.split('#');
        if (typeof repo === 'string') {
            return { scheme, host, owner, repo, path, ref };
        }
    }
    throw new Error(`Invalid config module source: "${source}"`);
}
exports.parse = parse;
function parseScheme(source) {
    if (re.git.https.test(source)) {
        return {
            scheme: 'git+https',
            path: source.replace(re.git.https, ''),
        };
    }
    return {
        scheme: 'git+ssh',
        path: source.replace(re.git.ssh, ''),
    };
}
const re = {
    git: {
        ssh: /^git\+ssh:\/\//,
        https: /^git\+https:\/\//,
    },
};
//# sourceMappingURL=resolve.js.map