"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const archy = require("archy");
const chalk_1 = require("chalk");
const path = require("path");
const R = require("ramda");
const readPkg_1 = require("./readPkg");
const sortPackages = R.sortBy(R.path(['pkg', 'name']));
async function default_1(project, tree, opts) {
    if (!opts.alwaysPrintRootPackage && !tree.length)
        return '';
    let label = '';
    if (project.name) {
        label += project.name;
        if (project.version) {
            label += `@${project.version}`;
        }
        label += ' ';
    }
    label += project.path;
    const s = archy({
        label,
        nodes: await toArchyTree(tree, {
            long: opts.long,
            modules: path.join(project.path, 'node_modules'),
        }),
    });
    return s.replace(/\n$/, '');
}
exports.default = default_1;
async function toArchyTree(entryNodes, opts) {
    return Promise.all(sortPackages(entryNodes).map(async (node) => {
        const nodes = await toArchyTree(node.dependencies || [], opts);
        if (opts.long) {
            const pkg = await readPkg_1.default(path.join(node.pkg.path, 'node_modules', node.pkg.name, 'package.json'));
            const labelLines = [
                printLabel(node),
                pkg.description,
            ];
            if (pkg.repository) {
                labelLines.push(typeof pkg.repository === 'string'
                    ? pkg.repository
                    : pkg.repository.url);
            }
            if (pkg.homepage) {
                labelLines.push(pkg.homepage);
            }
            return {
                label: labelLines.join('\n'),
                nodes,
            };
        }
        return {
            label: printLabel(node),
            nodes,
        };
    }));
}
function printLabel(node) {
    let txt = `${node.pkg.name}@${node.pkg.version}`;
    if (node.searched) {
        return chalk_1.default.yellow.bgBlack(txt);
    }
    if (node.saved === false) {
        txt += ` ${chalk_1.default.whiteBright.bgBlack('not saved')}`;
    }
    return txt;
}
//# sourceMappingURL=renderTree.js.map