"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 pauseStream = require('pause-stream');
const getRegistryAuthInfo = require('registry-auth-token');
const createCache_1 = require('./createCache');
const memoize = require('lodash.memoize');
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (client, opts) => {
    const cache = createCache_1.default({
        ttl: opts.cacheTTL,
        path: opts.cachePath
    });
    function get(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const cachedValue = yield cache.get(url);
            if (cachedValue)
                return cachedValue;
            const value = yield new Promise((resolve, reject) => {
                client.get(url, createOptions(url), (err, data, raw, res) => {
                    if (err)
                        return reject(err);
                    resolve(res);
                });
            });
            cache.set(url, value);
            return value;
        });
    }
    function getJSON(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const cachedValue = yield cache.get(url);
            if (cachedValue)
                return cachedValue;
            const value = yield new Promise((resolve, reject) => {
                client.get(url, createOptions(url), (err, data, raw, res) => {
                    if (err)
                        return reject(err);
                    resolve(data);
                });
            });
            cache.set(url, value);
            return value;
        });
    }
    const getStream = function (url) {
        return new Promise((resolve, reject) => {
            client.fetch(url, createOptions(url), (err, res) => {
                if (err)
                    return reject(err);
                const ps = pauseStream();
                res.pipe(ps.pause());
                resolve(ps);
            });
        });
    };
    function createOptions(url) {
        const authInfo = getRegistryAuthInfo(url, { recursive: true });
        if (!authInfo)
            return {};
        switch (authInfo.type) {
            case 'Bearer':
                return {
                    auth: {
                        token: authInfo.token
                    }
                };
            case 'Basic':
                return {
                    auth: {
                        username: authInfo.username,
                        password: authInfo.password
                    }
                };
            default:
                throw new Error(`Unsupported authorization type '${authInfo.type}'`);
        }
    }
    return {
        get: memoize(get),
        getJSON: memoize(getJSON),
        getStream: getStream
    };
};
//# sourceMappingURL=got.js.map