"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_loggers_1 = require("@pnpm/core-loggers");
const package_bins_1 = require("@pnpm/package-bins");
const utils_1 = require("@pnpm/utils");
const rimraf = require("@zkochan/rimraf");
const isWindows = require("is-windows");
const path = require("path");
async function removeDirectDependency(dependency, opts) {
    const results = await Promise.all([
        removeBins(dependency.name, opts),
        !opts.dryRun && remove(path.join(opts.modulesDir, dependency.name)),
    ]);
    const uninstalledPkg = results[0];
    if (!opts.muteLogs) {
        core_loggers_1.rootLogger.debug({
            prefix: opts.rootDir,
            removed: {
                dependencyType: dependency.dependenciesField === 'devDependencies' && 'dev' ||
                    dependency.dependenciesField === 'optionalDependencies' && 'optional' ||
                    dependency.dependenciesField === 'dependencies' && 'prod' ||
                    undefined,
                name: dependency.name,
                version: uninstalledPkg === null || uninstalledPkg === void 0 ? void 0 : uninstalledPkg.version,
            },
        });
    }
}
exports.default = removeDirectDependency;
async function removeOnWin(cmd) {
    core_loggers_1.removalLogger.debug(cmd);
    await Promise.all([
        rimraf(cmd),
        rimraf(`${cmd}.ps1`),
        rimraf(`${cmd}.cmd`),
    ]);
}
function removeOnNonWin(p) {
    core_loggers_1.removalLogger.debug(p);
    return rimraf(p);
}
const remove = isWindows() ? removeOnWin : removeOnNonWin;
async function removeBins(uninstalledPkg, opts) {
    const uninstalledPkgPath = path.join(opts.modulesDir, uninstalledPkg);
    const uninstalledPkgJson = await utils_1.safeReadPackageFromDir(uninstalledPkgPath);
    if (!uninstalledPkgJson)
        return;
    const cmds = await package_bins_1.default(uninstalledPkgJson, uninstalledPkgPath);
    if (!opts.dryRun) {
        await Promise.all(cmds
            .map((cmd) => path.join(opts.binsDir, cmd.name))
            .map(remove));
    }
    return uninstalledPkgJson;
}
