"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const npa = require("@zkochan/npm-package-arg");
const dependencies_hierarchy_1 = require("dependencies-hierarchy");
const path = require("path");
const readPkg_1 = require("./readPkg");
const renderParseable_1 = require("./renderParseable");
const renderTree_1 = require("./renderTree");
const DEFAULTS = {
    alwaysPrintRootPackage: true,
    depth: 0,
    long: false,
    only: undefined,
    parseable: false,
    registries: undefined,
};
async function forPackages(packages, projectPath, maybeOpts) {
    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 = await dependencies_hierarchy_1.forPackages(searched, projectPath, {
        depth: opts.depth,
        only: opts.only,
        registries: opts.registries,
        shrinkwrapDirectory: maybeOpts && maybeOpts.shrinkwrapDirectory,
    });
    const print = getPrinter(opts.parseable);
    const entryPkg = await readPkg_1.default(path.resolve(projectPath, 'package.json'));
    return print({
        name: entryPkg.name,
        path: projectPath,
        version: entryPkg.version,
    }, tree, {
        alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
        long: opts.long,
    });
}
exports.forPackages = forPackages;
async function default_1(projectPath, maybeOpts) {
    const opts = Object.assign({}, DEFAULTS, maybeOpts);
    const tree = opts.depth === -1
        ? []
        : await dependencies_hierarchy_1.default(projectPath, {
            depth: opts.depth,
            only: opts.only,
            registries: opts.registries,
            shrinkwrapDirectory: maybeOpts && maybeOpts.shrinkwrapDirectory,
        });
    const print = getPrinter(opts.parseable);
    const entryPkg = await readPkg_1.default(path.resolve(projectPath, 'package.json'));
    return print({
        name: entryPkg.name,
        path: projectPath,
        version: entryPkg.version,
    }, tree, {
        alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
        long: opts.long,
    });
}
exports.default = default_1;
function getPrinter(parseable) {
    if (parseable)
        return renderParseable_1.default;
    return renderTree_1.default;
}
//# sourceMappingURL=index.js.map