"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const util_1 = require("util");
const errors_1 = require("../../server/errors");
const util_2 = require("../../server/util");
const writer_1 = require("../writer");
function default_1(options) {
    return __awaiter(this, void 0, void 0, function* () {
        let fetcher = new util_2.Fetcher();
        let loggedIn = false;
        if (yield options.project.profile.hasAuthenticated()) {
            loggedIn = true;
            fetcher = fetcher.with(yield options.project.profile.tokens());
        }
        const res = yield fetcher.json('get', `/interactive/bundles/${options.bundle}`);
        if (res.status === 404) {
            if (loggedIn) {
                writer_1.default.write('Bundle not found.');
            }
            else {
                writer_1.default.write('Bundle not found. If this is your package, you should log in with `miix login` to see it.');
            }
            process.exit(1);
            return;
        }
        if (res.status !== 200) {
            throw new errors_1.UnexpectedHttpError(res, yield res.text());
        }
        const data = yield res.json();
        if (!options.verbose) {
            delete data.readme;
            delete data.hasPublicVersion;
            data.versions = data.versions.map((v) => lodash_1.pick(v, ['version', 'publishedAt']));
        }
        if (options.json) {
            writer_1.default.write(JSON.stringify(data, null, 2));
            return;
        }
        writer_1.default.write(util_1.inspect(data, false, null, true));
    });
}
exports.default = default_1;
