"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dp = require("dependency-path");
const shrinkwrap_1 = require("../fs/shrinkwrap");
const pnpm_shrinkwrap_1 = require("pnpm-shrinkwrap");
const R = require("ramda");
function default_1(pkgsToLink, shrinkwrap, pkg) {
    shrinkwrap.packages = shrinkwrap.packages || {};
    for (const dependencyAbsolutePath of R.keys(pkgsToLink)) {
        const dependencyPath = dp.relative(shrinkwrap.registry, dependencyAbsolutePath);
        const result = R.partition((childResolvedId) => pkgsToLink[dependencyAbsolutePath].optionalDependencies.has(pkgsToLink[childResolvedId].name), pkgsToLink[dependencyAbsolutePath].children);
        shrinkwrap.packages[dependencyPath] = toShrDependency({
            dependencyAbsolutePath,
            id: pkgsToLink[dependencyAbsolutePath].id,
            dependencyPath,
            resolution: pkgsToLink[dependencyAbsolutePath].resolution,
            updatedOptionalDeps: result[0],
            updatedDeps: result[1],
            registry: shrinkwrap.registry,
            pkgsToLink,
            prevResolvedDeps: shrinkwrap.packages[dependencyPath] && shrinkwrap.packages[dependencyPath].dependencies || {},
            prevResolvedOptionalDeps: shrinkwrap.packages[dependencyPath] && shrinkwrap.packages[dependencyPath].optionalDependencies || {},
            dev: pkgsToLink[dependencyAbsolutePath].dev,
            optional: pkgsToLink[dependencyAbsolutePath].optional,
        });
    }
    return pnpm_shrinkwrap_1.prune(shrinkwrap, pkg);
}
exports.default = default_1;
function toShrDependency(opts) {
    const shrResolution = toShrResolution(opts.dependencyPath, opts.resolution);
    const newResolvedDeps = updateResolvedDeps(opts.prevResolvedDeps, opts.updatedDeps, opts.registry, opts.pkgsToLink);
    const newResolvedOptionalDeps = updateResolvedDeps(opts.prevResolvedOptionalDeps, opts.updatedOptionalDeps, opts.registry, opts.pkgsToLink);
    const result = {
        resolution: shrResolution
    };
    if (!R.isEmpty(newResolvedDeps)) {
        result['dependencies'] = newResolvedDeps;
    }
    if (!R.isEmpty(newResolvedOptionalDeps)) {
        result['optionalDependencies'] = newResolvedOptionalDeps;
    }
    if (opts.dev) {
        result['dev'] = true;
    }
    if (opts.optional) {
        result['optional'] = true;
    }
    if (opts.dependencyAbsolutePath !== opts.id) {
        result['id'] = opts.id;
    }
    return result;
}
// previous resolutions should not be removed from shrinkwrap
// as installation might not reanalize the whole dependency tree
// the `depth` property defines how deep should dependencies be checked
function updateResolvedDeps(prevResolvedDeps, updatedDeps, registry, pkgsToLink) {
    const newResolvedDeps = R.fromPairs(R.props(updatedDeps, pkgsToLink)
        .map((dep) => [
        dep.name,
        shrinkwrap_1.absolutePathToRef(dep.absolutePath, dep.name, dep.resolution, registry)
    ]));
    return R.merge(prevResolvedDeps, newResolvedDeps);
}
function toShrResolution(dependencyPath, resolution) {
    if (!dp.isAbsolute(dependencyPath) && resolution.type === undefined && resolution.integrity) {
        return {
            integrity: resolution.integrity,
        };
    }
    return resolution;
}
//# sourceMappingURL=updateShrinkwrap.js.map