"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.help = exports.commandNames = exports.rcOptionsTypes = exports.cliOptionsTypes = void 0;
const cli_utils_1 = require("@pnpm/cli-utils");
const common_cli_options_help_1 = require("@pnpm/common-cli-options-help");
const store_connection_manager_1 = require("@pnpm/store-connection-manager");
const renderHelp = require("render-help");
const supi_1 = require("supi");
const install_1 = require("./install");
Object.defineProperty(exports, "cliOptionsTypes", { enumerable: true, get: function () { return install_1.cliOptionsTypes; } });
Object.defineProperty(exports, "rcOptionsTypes", { enumerable: true, get: function () { return install_1.rcOptionsTypes; } });
const recursive_1 = require("./recursive");
exports.commandNames = ['unlink', 'dislink'];
function help() {
    return renderHelp({
        aliases: ['dislink'],
        description: 'Removes the link created by \`pnpm link\` and reinstalls package if it is saved in \`package.json\`',
        descriptionLists: [
            {
                title: 'Options',
                list: [
                    {
                        description: `Unlink in every package found in subdirectories \
or in every workspace package, when executed inside a workspace. \
For options that may be used with \`-r\`, see "pnpm help recursive"`,
                        name: '--recursive',
                        shortAlias: '-r',
                    },
                    ...common_cli_options_help_1.UNIVERSAL_OPTIONS,
                ],
            },
        ],
        url: cli_utils_1.docsUrl('unlink'),
        usages: [
            'pnpm unlink (in package dir)',
            'pnpm unlink <pkg>...',
        ],
    });
}
exports.help = help;
async function handler(opts, params) {
    if (opts.recursive && opts.allProjects && opts.selectedProjectsGraph && opts.workspaceDir) {
        await recursive_1.default(opts.allProjects, params, { ...opts, selectedProjectsGraph: opts.selectedProjectsGraph, workspaceDir: opts.workspaceDir }, 'unlink');
        return;
    }
    const store = await store_connection_manager_1.createOrConnectStoreController(opts);
    const unlinkOpts = Object.assign(opts, {
        globalBin: opts.bin,
        storeController: store.ctrl,
        storeDir: store.dir,
    });
    if (!params || !params.length) {
        return supi_1.mutateModules([
            {
                dependencyNames: params,
                manifest: await cli_utils_1.readProjectManifestOnly(opts.dir, opts),
                mutation: 'unlinkSome',
                rootDir: opts.dir,
            },
        ], unlinkOpts);
    }
    return supi_1.mutateModules([
        {
            manifest: await cli_utils_1.readProjectManifestOnly(opts.dir, opts),
            mutation: 'unlink',
            rootDir: opts.dir,
        },
    ], unlinkOpts);
}
exports.handler = handler;
//# sourceMappingURL=unlink.js.map