"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lockfile_file_1 = require("@pnpm/lockfile-file");
const chalk_1 = require("chalk");
const common_tags_1 = require("common-tags");
const R = require("ramda");
const table_1 = require("table");
const outdated_1 = require("../outdated");
const DEP_PRIORITY = {
    dependencies: 1,
    devDependencies: 2,
    optionalDependencies: 0,
};
const COMPARATORS = [
    ...outdated_1.DEFAULT_COMPARATORS,
    (o1, o2) => DEP_PRIORITY[o1.belongsTo] - DEP_PRIORITY[o2.belongsTo],
];
exports.default = async (pkgs, args, cmd, opts) => {
    const outdatedByNameAndType = {};
    if (opts.lockfileDirectory) {
        const outdatedPackagesByProject = await outdated_1.outdatedDependenciesOfWorkspacePackages(pkgs, args, opts);
        for (let { prefix, outdatedPackages, manifest } of outdatedPackagesByProject) {
            outdatedPackages.forEach((outdatedPkg) => {
                const key = JSON.stringify([outdatedPkg.packageName, outdatedPkg.belongsTo]);
                if (!outdatedByNameAndType[key]) {
                    outdatedByNameAndType[key] = { ...outdatedPkg, dependentPkgs: [] };
                }
                outdatedByNameAndType[key].dependentPkgs.push({ location: prefix, manifest });
            });
        }
    }
    else {
        await Promise.all(pkgs.map(async ({ manifest, path }) => {
            const { outdatedPackages } = (await outdated_1.outdatedDependenciesOfWorkspacePackages([{ manifest, path }], args, { ...opts, lockfileDirectory: path }))[0];
            outdatedPackages.forEach((outdatedPkg) => {
                const key = JSON.stringify([outdatedPkg.packageName, outdatedPkg.belongsTo]);
                if (!outdatedByNameAndType[key]) {
                    outdatedByNameAndType[key] = { ...outdatedPkg, dependentPkgs: [] };
                }
                outdatedByNameAndType[key].dependentPkgs.push({ location: lockfile_file_1.getLockfileImporterId(opts.prefix, path), manifest });
            });
        }));
    }
    if (opts.table !== false) {
        process.stdout.write(renderOutdatedTable(outdatedByNameAndType, opts));
        return;
    }
    process.stdout.write(renderOutdatedList(outdatedByNameAndType, opts));
};
function renderOutdatedTable(outdatedByNameAndType, opts) {
    let columnNames = [
        'Package',
        'Current',
        'Latest',
        'Dependents'
    ];
    let columnFns = [
        outdated_1.renderPackageName,
        outdated_1.renderCurrent,
        outdated_1.renderLatest,
        dependentPackages,
    ];
    if (opts.long) {
        columnNames.push('Details');
        columnFns.push(outdated_1.renderDetails);
    }
    // Avoid the overhead of allocating a new array caused by calling `array.map()`
    for (let i = 0; i < columnNames.length; i++)
        columnNames[i] = chalk_1.default.blueBright(columnNames[i]);
    const data = [
        columnNames,
        ...sortOutdatedPackages(Object.values(outdatedByNameAndType))
            .map((outdatedPkg) => columnFns.map((fn) => fn(outdatedPkg))),
    ];
    return table_1.table(data, {
        ...outdated_1.TABLE_OPTIONS,
        columns: {
            ...outdated_1.TABLE_OPTIONS.columns,
            // Dependents column:
            3: {
                width: outdated_1.getCellWidth(data, 3, 30),
                wrapWord: true,
            },
        },
    });
}
function renderOutdatedList(outdatedByNameAndType, opts) {
    return sortOutdatedPackages(Object.values(outdatedByNameAndType))
        .map((outdatedPkg) => {
        let info = common_tags_1.stripIndent `
        ${chalk_1.default.bold(outdated_1.renderPackageName(outdatedPkg))}
        ${outdated_1.renderCurrent(outdatedPkg)} ${chalk_1.default.grey('=>')} ${outdated_1.renderLatest(outdatedPkg)}`;
        const dependents = dependentPackages(outdatedPkg);
        if (dependents) {
            info += `\n${chalk_1.default.bold(outdatedPkg.dependentPkgs.length > 1
                ? 'Dependents:'
                : 'Dependent:')} ${dependents}`;
        }
        if (opts.long) {
            const details = outdated_1.renderDetails(outdatedPkg);
            if (details) {
                info += `\n${details}`;
            }
        }
        return info;
    })
        .join('\n\n') + '\n';
}
function dependentPackages({ dependentPkgs }) {
    return dependentPkgs
        .map(({ manifest, location }) => manifest.name || location)
        .sort()
        .join(', ');
}
function sortOutdatedPackages(outdatedPackages) {
    return R.sortWith(COMPARATORS, outdatedPackages.map(outdated_1.toOutdatedWithVersionDiff));
}
