"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 path = require("path");
const R = require("ramda");
const readPkg_1 = require("./readPkg");
const sortPackages = R.sortBy(R.prop('name'));
function default_1(projectPath, tree, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const pkgs = sortPackages(flatten(tree));
        const prefix = path.join(projectPath, 'node_modules');
        if (!opts.alwaysPrintRootPackage && !pkgs.length)
            return '';
        if (opts.long) {
            const entryPkg = yield readPkg_1.default(path.resolve(projectPath, 'package.json'));
            let firstLine = projectPath;
            if (entryPkg.name) {
                firstLine += `:${entryPkg.name}`;
                if (entryPkg.version) {
                    firstLine += `@${entryPkg.version}`;
                }
            }
            return [
                firstLine,
                ...pkgs.map((pkg) => `${path.join(prefix, `.${pkg.path}`)}:${pkg.name}@${pkg.version}`),
                '',
            ].join('\n');
        }
        return [
            projectPath,
            ...pkgs.map((pkg) => path.join(prefix, `.${pkg.path}`)),
            '',
        ].join('\n');
    });
}
exports.default = default_1;
function flatten(nodes) {
    let packages = [];
    for (const node of nodes) {
        packages.push(node.pkg);
        if (node.dependencies && node.dependencies.length) {
            packages = packages.concat(flatten(node.dependencies));
        }
    }
    return packages;
}
//# sourceMappingURL=printParseable.js.map