"use strict";

var _mapInstanceProperty2 = require("@babel/runtime-corejs3/core-js-stable/instance/map");

var _filterInstanceProperty2 = require("@babel/runtime-corejs3/core-js-stable/instance/filter");

var _flatMapInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/flat-map");

var _includesInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/includes");

var _findInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/find");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));

var _fs = require("fs");

var _path = require("path");

var _lodash = require("lodash");

var _fp = require("lodash/fp");

var _plugin = _interopRequireDefault(require("@start/plugin"));

var _linklocal = require("../utils/linklocal");

var _listWorkspacePackages = _interopRequireDefault(require("../utils/listWorkspacePackages"));

require("console.table");

var _default = (cosmicPath, reverse = false) => (0, _plugin.default)('LINK DEPENDENCIES', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  reporter.emit('message', `Looking for packages to link in ${cosmicPath}`);
  const packages = yield (0, _listWorkspacePackages.default)({
    cwd: cosmicPath
  });
  return new _promise.default((resolve, reject) => {
    const filteredPackages = (0, _mapInstanceProperty2(_lodash))((0, _filterInstanceProperty2(_lodash))(packages, p => (0, _fs.existsSync)((0, _path.join)('./node_modules', p.name))), p => ({
      from: (0, _path.join)('./node_modules', p.name),
      to: p.location,
      name: p.name
    }));
    const recursivePackages = (0, _flatMapInstanceProperty(_lodash))(filteredPackages, p => {
      var _context, _context2;

      const nodeModules = (0, _path.join)(p.to, './node_modules');
      if (!(0, _fs.existsSync)(nodeModules)) return [];
      const packageNames = (0, _map.default)(packages).call(packages, item => item.name);
      return (0, _map.default)(_context = (0, _filter.default)(_context2 = (0, _fs.readdirSync)(nodeModules)).call(_context2, (0, _includesInstanceProperty(_fp))(packageNames))).call(_context, name => ({
        from: (0, _path.join)(nodeModules, name),
        to: (0, _findInstanceProperty(_lodash))(packages, {
          name: name
        }),
        name: name
      }));
    });
    const method = reverse ? _linklocal.unlinkLinks : _linklocal.linkLinks;
    method((0, _concat.default)(filteredPackages).call(filteredPackages, recursivePackages), (err, links) => {
      if (err) return reject(err);
      reporter.emit('message', `Successfully ${reverse ? 'unlinked' : 'linked'} the following packages`);
      console.table(links);
      resolve();
    });
  });
}));

exports.default = _default;
module.exports = exports.default;