"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.removeUnmanagedDependencies = removeUnmanagedDependencies;

var _immer = _interopRequireDefault(require("immer"));

var _lodash = require("lodash");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function removeUnmanagedDependencies(currentDependencies, templateDependencies, context) {
  // Check which dependencies are tracked in the lockfile, but no longer
  // appear in the new template dependencies
  const dependenciesToRemove = (0, _lodash.difference)(context.coatGlobalLockfile.dependencies.dependencies, Object.keys(templateDependencies.dependencies));
  const devDependenciesToRemove = (0, _lodash.difference)(context.coatGlobalLockfile.dependencies.devDependencies, Object.keys(templateDependencies.devDependencies));
  const peerDependenciesToRemove = (0, _lodash.difference)(context.coatGlobalLockfile.dependencies.peerDependencies, Object.keys(templateDependencies.peerDependencies));
  const optionalDependenciesToRemove = (0, _lodash.difference)(context.coatGlobalLockfile.dependencies.optionalDependencies, Object.keys(templateDependencies.optionalDependencies)); // Remove dependencies from the current dependency groups

  return (0, _immer.default)(currentDependencies, draft => {
    dependenciesToRemove.forEach(dependencyName => {
      delete draft.dependencies[dependencyName];
    });
    devDependenciesToRemove.forEach(dependencyName => {
      delete draft.devDependencies[dependencyName];
    });
    peerDependenciesToRemove.forEach(dependencyName => {
      delete draft.peerDependencies[dependencyName];
    });
    optionalDependenciesToRemove.forEach(dependencyName => {
      delete draft.optionalDependencies[dependencyName];
    });
  });
}