"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lockfile_file_1 = require("@pnpm/lockfile-file");
const chalk_1 = require("chalk");
const stripColor = require("strip-color");
const table = require("text-table");
const outdated_1 = require("../outdated");
exports.default = async (pkgs, args, cmd, opts) => {
    const outdatedPkgs = [];
    if (opts.lockfileDirectory) {
        const outdatedPackagesByProject = await outdated_1.outdatedDependenciesOfWorkspacePackages(pkgs, args, opts);
        for (let { prefix, outdatedPackages } of outdatedPackagesByProject) {
            outdatedPackages.forEach((outdatedPkg) => outdatedPkgs.push(Object.assign({}, outdatedPkg, { prefix }))); // tslint:disable-line:no-any
        }
    }
    else {
        await Promise.all(pkgs.map(async ({ manifest, path }) => {
            const { outdatedPackages } = (await outdated_1.outdatedDependenciesOfWorkspacePackages([{ manifest, path }], args, Object.assign({}, opts, { lockfileDirectory: path })))[0];
            outdatedPackages.forEach((outdatedPkg) => // tslint:disable-line:no-any
             outdatedPkgs.push(Object.assign({}, outdatedPkg, { prefix: lockfile_file_1.getLockfileImporterId(opts.prefix, path) })));
        }));
    }
    const columnNames = ['', 'Package', 'Current', 'Wanted', 'Latest'].map((txt) => chalk_1.default.underline(txt));
    console.log(table([columnNames].concat(outdatedPkgs
        .sort((o1, o2) => o1.prefix.localeCompare(o2.prefix))
        .map((outdatedPkg) => [
        outdatedPkg.prefix,
        chalk_1.default.yellow(outdatedPkg.packageName),
        outdatedPkg.current || 'missing',
        chalk_1.default.green(outdatedPkg.wanted),
        chalk_1.default.magenta(outdatedPkg.latest || ''),
    ])), {
        stringLength: (s) => stripColor(s).length,
    }));
};
