"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const createPkgId_1 = require('./createPkgId');
const fetch_1 = require('./fetch');
/**
 * Resolves a 'hosted' package hosted on 'github'.
 */
function resolveGithub(spec, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const ghSpec = parseGithubSpec(spec);
        const dist = {
            tarball: `https://codeload.github.com/${ghSpec.owner}/${ghSpec.repo}/tar.gz/${ghSpec.ref}`
        };
        return {
            id: ['github', ghSpec.owner, ghSpec.repo, ghSpec.ref].join(createPkgId_1.delimiter),
            fetch: (target, opts) => fetch_1.fetchFromRemoteTarball(target, dist, opts)
        };
    });
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = resolveGithub;
const PARSE_GITHUB_RE = /^github:([^\/]+)\/([^#]+)(#(.+))?$/;
function parseGithubSpec(spec) {
    const m = PARSE_GITHUB_RE.exec(spec.hosted.shortcut);
    if (!m) {
        throw new Error('cannot parse: ' + spec.hosted.shortcut);
    }
    const owner = m[1];
    const repo = m[2];
    const ref = m[4] || 'HEAD';
    return { owner, repo, ref };
}
//# sourceMappingURL=github.js.map