"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dp = require("dependency-path");
const get_npm_tarball_url_1 = require("get-npm-tarball-url");
const url = require("url");
exports.default = (relDepPath, pkgSnapshot, registry) => {
    // tslint:disable:no-string-literal
    if (pkgSnapshot.resolution['type']) {
        return pkgSnapshot.resolution;
    }
    if (!pkgSnapshot.resolution['tarball']) {
        return Object.assign({}, pkgSnapshot.resolution, { registry: pkgSnapshot.resolution['registry'] || registry, tarball: getTarball() });
    }
    if (pkgSnapshot.resolution['tarball'].startsWith('file:')) {
        return pkgSnapshot.resolution;
    }
    return Object.assign({}, pkgSnapshot.resolution, { registry: pkgSnapshot.resolution['registry'] || registry, tarball: url.resolve(registry, pkgSnapshot.resolution['tarball']) });
    function getTarball() {
        const parsed = dp.parse(relDepPath);
        if (!parsed['name'] || !parsed['version']) {
            throw new Error(`Couldn't get tarball URL from dependency path ${relDepPath}`);
        }
        return get_npm_tarball_url_1.default(parsed['name'], parsed['version'], { registry });
    }
    // tslint:enable:no-string-literal
};
//# sourceMappingURL=pkgSnapshotToResolution.js.map