"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const pLimit = require("p-limit");
const path = require("path");
const pathAbsolute = require("path-absolute");
const R = require("ramda");
const supi_1 = require("supi");
const createStoreController_1 = require("../createStoreController");
const findWorkspacePackages_1 = require("../findWorkspacePackages");
const getConfigs_1 = require("../getConfigs");
const installLimit = pLimit(4);
exports.default = async (input, opts) => {
    const cwd = opts && opts.prefix || process.cwd();
    const storeControllerCache = new Map();
    let workspacePackages;
    let localPackages;
    if (opts.linkWorkspacePackages && opts.workspacePrefix) {
        workspacePackages = await findWorkspacePackages_1.default(opts.workspacePrefix);
        localPackages = findWorkspacePackages_1.arrayOfLocalPackagesToMap(workspacePackages);
    }
    else {
        localPackages = {};
    }
    const store = await createStoreController_1.cached(storeControllerCache, opts);
    const linkOpts = Object.assign(opts, {
        localPackages,
        store: store.path,
        storeController: store.ctrl,
    });
    // pnpm link
    if (!input || !input.length) {
        await supi_1.linkToGlobal(cwd, linkOpts);
        return;
    }
    const [pkgPaths, pkgNames] = R.partition((inp) => inp.startsWith('.'), input);
    if (pkgNames.length) {
        let globalPkgNames;
        if (opts.workspacePrefix) {
            workspacePackages = await findWorkspacePackages_1.default(opts.workspacePrefix);
            const pkgsFoundInWorkspace = workspacePackages.filter((pkg) => pkgNames.indexOf(pkg.manifest.name) !== -1);
            pkgsFoundInWorkspace.forEach((pkgFromWorkspace) => pkgPaths.push(pkgFromWorkspace.path));
            if (pkgsFoundInWorkspace.length && !linkOpts.saveDev && !linkOpts.saveProd && !linkOpts.saveOptional) {
                linkOpts.saveProd = true;
            }
            globalPkgNames = pkgNames.filter((pkgName) => !pkgsFoundInWorkspace.some((pkgFromWorkspace) => pkgFromWorkspace.manifest.name === pkgName));
        }
        else {
            globalPkgNames = pkgNames;
        }
        const globalPkgPath = pathAbsolute(opts.globalPrefix);
        globalPkgNames.forEach((pkgName) => pkgPaths.push(path.join(globalPkgPath, 'node_modules', pkgName)));
    }
    await Promise.all(pkgPaths.map((prefix) => installLimit(async () => {
        const s = await createStoreController_1.cached(storeControllerCache, opts);
        await supi_1.install(Object.assign({}, await getConfigs_1.default(Object.assign({}, opts.cliArgs, { prefix }), {
            command: ['link'],
            excludeReporter: true,
        }), { localPackages, store: s.path, storeController: s.ctrl }));
    })));
    await supi_1.link(pkgPaths, path.join(cwd, 'node_modules'), linkOpts);
    await Promise.all(Array.from(storeControllerCache.values())
        .map(async (storeControllerPromise) => {
        const storeControllerHolder = await storeControllerPromise;
        await storeControllerHolder.ctrl.close();
    }));
};
//# sourceMappingURL=link.js.map