"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const normalize = require("normalize-path");
const osenv = require("osenv");
const path = require("path");
// tslint:disable-next-line
const isWindows = process.platform === 'win32' || global['FAKE_WINDOWS'];
const isFilespec = isWindows ? /^(?:[.]|~[/]|[/\\]|[a-zA-Z]:)/ : /^(?:[.]|~[/]|[/]|[a-zA-Z]:)/;
const isFilename = /[.](?:tgz|tar.gz|tar)$/i;
const isAbsolutePath = /^[/]|^[A-Za-z]:/;
function parsePref(pref, where) {
    if (pref.startsWith('link:')) {
        return fromLocal(pref, where, 'directory');
    }
    if (pref.endsWith('.tgz')
        || pref.endsWith('.tar.gz')
        || pref.endsWith('.tar')
        || pref.includes(path.sep)
        || pref.startsWith('file:')
        || isFilespec.test(pref)) {
        const type = isFilename.test(pref) ? 'file' : 'directory';
        return fromLocal(pref, where, type);
    }
    if (pref.startsWith('path:')) {
        const err = new Error('Local dependencies via `path:` protocol are not supported. ' +
            'Use the `link:` protocol for folder dependencies and `file:` for local tarballs');
        // tslint:disable:no-string-literal
        err['code'] = 'EUNSUPPORTEDPROTOCOL';
        err['pref'] = pref;
        err['protocol'] = 'path:';
        // tslint:enable:no-string-literal
        throw err;
    }
    return null;
}
exports.default = parsePref;
function fromLocal(pref, where, type) {
    if (!where)
        where = process.cwd();
    const spec = pref.replace(/\\/g, '/')
        .replace(/^(file|link):[/]*([A-Za-z]:)/, '$2') // drive name paths on windows
        .replace(/^(file|link):(?:[/]*([~./]))?/, '$2');
    const protocol = type === 'directory' ? 'link:' : 'file:';
    let fetchSpec;
    let normalizedPref;
    if (/^~[/]/.test(spec)) {
        // this is needed for windows and for file:~/foo/bar
        fetchSpec = resolvePath(osenv.home(), spec.slice(2));
        normalizedPref = `${protocol}${spec}`;
    }
    else {
        fetchSpec = resolvePath(where, spec);
        if (isAbsolute(spec)) {
            normalizedPref = `${protocol}${spec}`;
        }
        else {
            normalizedPref = `${protocol}${path.relative(where, fetchSpec)}`;
        }
    }
    const dependencyPath = normalize(path.relative(where, fetchSpec));
    const id = `${protocol}${dependencyPath}`;
    return {
        dependencyPath,
        fetchSpec,
        id,
        normalizedPref,
        type,
    };
}
function resolvePath(where, spec) {
    if (isAbsolutePath.test(spec))
        return spec;
    return path.resolve(where, spec);
}
function isAbsolute(dir) {
    if (dir[0] === '/')
        return true;
    if (/^[A-Za-z]:/.test(dir))
        return true;
    return false;
}
//# sourceMappingURL=parsePref.js.map