"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 dependencies_hierarchy_1 = require("dependencies-hierarchy");
const npa = require("npm-package-arg");
const printParseable_1 = require("./printParseable");
const printTree_1 = require("./printTree");
const DEFAULTS = {
    alwaysPrintRootPackage: true,
    depth: 0,
    long: false,
    only: undefined,
    parseable: false,
    registries: undefined,
};
function forPackages(packages, projectPath, maybeOpts) {
    return __awaiter(this, void 0, void 0, function* () {
        const opts = Object.assign({}, DEFAULTS, maybeOpts);
        const searched = packages.map((arg) => {
            const parsed = npa(arg);
            if (parsed.raw === parsed.name) {
                return parsed.name;
            }
            if (parsed.type !== 'version' && parsed.type !== 'range') {
                throw new Error(`Invalid argument - ${arg}. List can search only by version or range`);
            }
            return {
                name: parsed.name,
                range: parsed.fetchSpec,
            };
        });
        const tree = yield dependencies_hierarchy_1.forPackages(searched, projectPath, {
            depth: opts.depth,
            only: opts.only,
            registries: opts.registries,
            shrinkwrapDirectory: maybeOpts && maybeOpts.shrinkwrapDirectory,
        });
        const print = getPrinter(opts.parseable);
        return print(projectPath, tree, {
            alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
            long: opts.long,
        });
    });
}
exports.forPackages = forPackages;
function default_1(projectPath, maybeOpts) {
    return __awaiter(this, void 0, void 0, function* () {
        const opts = Object.assign({}, DEFAULTS, maybeOpts);
        const tree = yield dependencies_hierarchy_1.default(projectPath, {
            depth: opts.depth,
            only: opts.only,
            registries: opts.registries,
            shrinkwrapDirectory: maybeOpts && maybeOpts.shrinkwrapDirectory,
        });
        const print = getPrinter(opts.parseable);
        return print(projectPath, tree, {
            alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
            long: opts.long,
        });
    });
}
exports.default = default_1;
function getPrinter(parseable) {
    if (parseable)
        return printParseable_1.default;
    return printTree_1.default;
}
//# sourceMappingURL=index.js.map