'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

exports.default = remove;

var _scope;

function _load_scope() {
  return _scope = require('../../../scope');
}

var _bitId;

function _load_bitId() {
  return _bitId = require('../../../bit-id');
}

var _constants;

function _load_constants() {
  return _constants = require('../../../constants');
}

var _hooks;

function _load_hooks() {
  return _hooks = _interopRequireDefault(require('../../../hooks'));
}

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var HooksManagerInstance = (_hooks || _load_hooks()).default.getInstance();

function remove(_ref, headers) {
  var _this = this;

  var path = _ref.path,
      ids = _ref.ids,
      force = _ref.force;

  var bitIds = (_bitId || _load_bitId()).BitIds.deserialize(ids);
  var args = { path, bitIds, force };
  HooksManagerInstance.triggerHook((_constants || _load_constants()).PRE_REMOVE_REMOTE, args, headers);
  return (0, (_scope || _load_scope()).loadScope)(path).then(function (scope) {
    return scope.removeMany(bitIds, force).then(function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(res) {
        var hookArgs;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                hookArgs = {
                  removedComponentsIds: res.removedComponentIds.serialize(),
                  missingComponentsIds: res.missingComponents.serialize(),
                  dependentBitsIds: res.dependentBits,
                  force,
                  scopePath: path,
                  componentsIds: bitIds.serialize(),
                  scopeName: scope.scopeJson.name
                };
                _context.next = 3;
                return HooksManagerInstance.triggerHook((_constants || _load_constants()).POST_REMOVE_REMOTE, hookArgs, headers);

              case 3:
                return _context.abrupt('return', res.serialize());

              case 4:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, _this);
      }));

      return function (_x) {
        return _ref2.apply(this, arguments);
      };
    }());
  });
}