'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.undeprecateRemote = exports.deprecateRemote = exports.undeprecateMany = exports.deprecateMany = undefined;

var _regenerator;

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

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var deprecateMany = exports.deprecateMany = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(scope, bitIds) {
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            (_logger || _load_logger()).default.debug(`scope.deprecateMany, ids: ${bitIds.toString()}`);
            return _context.abrupt('return', _deprecationMany(scope, bitIds, _deprecateSingle));

          case 2:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function deprecateMany(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

var undeprecateMany = exports.undeprecateMany = function () {
  var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(scope, bitIds) {
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            (_logger || _load_logger()).default.debug(`scope.undeprecateMany, ids: ${bitIds.toString()}`);
            return _context2.abrupt('return', _deprecationMany(scope, bitIds, _undeprecateSingle));

          case 2:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

  return function undeprecateMany(_x3, _x4) {
    return _ref2.apply(this, arguments);
  };
}();

var deprecateRemote = exports.deprecateRemote = function () {
  var _ref3 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(scope, bitIds) {
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt('return', _deprecationRemote(scope, bitIds, true));

          case 1:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this);
  }));

  return function deprecateRemote(_x5, _x6) {
    return _ref3.apply(this, arguments);
  };
}();

var undeprecateRemote = exports.undeprecateRemote = function () {
  var _ref4 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4(scope, bitIds) {
    return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt('return', _deprecationRemote(scope, bitIds, false));

          case 1:
          case 'end':
            return _context4.stop();
        }
      }
    }, _callee4, this);
  }));

  return function undeprecateRemote(_x7, _x8) {
    return _ref4.apply(this, arguments);
  };
}();

var _deprecationMany = function () {
  var _ref5 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee5(scope, ids, deprecationAction) {
    var _ref6, missingComponents, foundComponents, bitIdsP, bitIds, missingComponentsStrings;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            _context5.next = 2;
            return scope.filterFoundAndMissingComponents(ids);

          case 2:
            _ref6 = _context5.sent;
            missingComponents = _ref6.missingComponents;
            foundComponents = _ref6.foundComponents;
            bitIdsP = foundComponents.map(function (bitId) {
              return deprecationAction(scope, bitId);
            });
            _context5.next = 8;
            return Promise.all(bitIdsP);

          case 8:
            bitIds = _context5.sent;
            _context5.next = 11;
            return scope.objects.persist();

          case 11:
            missingComponentsStrings = missingComponents.map(function (id) {
              return id.toStringWithoutVersion();
            });
            return _context5.abrupt('return', { bitIds, missingComponents: missingComponentsStrings });

          case 13:
          case 'end':
            return _context5.stop();
        }
      }
    }, _callee5, this);
  }));

  return function _deprecationMany(_x9, _x10, _x11) {
    return _ref5.apply(this, arguments);
  };
}();

var _deprecationRemote = function () {
  var _ref7 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee7(scope, bitIds, deprecate) {
    var _this = this;

    var groupedBitsByScope, remotes, context, deprecateP;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            groupedBitsByScope = (0, (_groupArray || _load_groupArray()).default)(bitIds, 'scope');
            _context7.next = 3;
            return (0, (_scopeRemotes || _load_scopeRemotes()).getScopeRemotes)(scope);

          case 3:
            remotes = _context7.sent;
            context = {};

            (0, (_enrichContextFromGlobal || _load_enrichContextFromGlobal()).default)(context);
            deprecateP = Object.keys(groupedBitsByScope).map(function () {
              var _ref8 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee6(scopeName) {
                var resolvedRemote, idsStr, deprecateResult;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee6$(_context6) {
                  while (1) {
                    switch (_context6.prev = _context6.next) {
                      case 0:
                        _context6.next = 2;
                        return remotes.resolve(scopeName, scope);

                      case 2:
                        resolvedRemote = _context6.sent;
                        idsStr = groupedBitsByScope[scopeName].map(function (id) {
                          return id.toStringWithoutVersion();
                        });

                        if (!deprecate) {
                          _context6.next = 10;
                          break;
                        }

                        _context6.next = 7;
                        return resolvedRemote.deprecateMany(idsStr, context);

                      case 7:
                        _context6.t0 = _context6.sent;
                        _context6.next = 13;
                        break;

                      case 10:
                        _context6.next = 12;
                        return resolvedRemote.undeprecateMany(idsStr, context);

                      case 12:
                        _context6.t0 = _context6.sent;

                      case 13:
                        deprecateResult = _context6.t0;
                        return _context6.abrupt('return', deprecateResult);

                      case 15:
                      case 'end':
                        return _context6.stop();
                    }
                  }
                }, _callee6, _this);
              }));

              return function (_x15) {
                return _ref8.apply(this, arguments);
              };
            }());
            return _context7.abrupt('return', Promise.all(deprecateP));

          case 8:
          case 'end':
            return _context7.stop();
        }
      }
    }, _callee7, this);
  }));

  return function _deprecationRemote(_x12, _x13, _x14) {
    return _ref7.apply(this, arguments);
  };
}();

var _deprecateSingle = function () {
  var _ref9 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee8(scope, bitId) {
    var component;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            _context8.next = 2;
            return scope.getModelComponent(bitId);

          case 2:
            component = _context8.sent;

            component.deprecated = true;
            scope.objects.add(component);
            return _context8.abrupt('return', bitId.toStringWithoutVersion());

          case 6:
          case 'end':
            return _context8.stop();
        }
      }
    }, _callee8, this);
  }));

  return function _deprecateSingle(_x16, _x17) {
    return _ref9.apply(this, arguments);
  };
}();

var _undeprecateSingle = function () {
  var _ref10 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee9(scope, bitId) {
    var component;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            _context9.next = 2;
            return scope.getModelComponent(bitId);

          case 2:
            component = _context9.sent;

            component.deprecated = false;
            scope.objects.add(component);
            return _context9.abrupt('return', bitId.toStringWithoutVersion());

          case 6:
          case 'end':
            return _context9.stop();
        }
      }
    }, _callee9, this);
  }));

  return function _undeprecateSingle(_x18, _x19) {
    return _ref10.apply(this, arguments);
  };
}();

var _groupArray;

function _load_groupArray() {
  return _groupArray = _interopRequireDefault(require('group-array'));
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../logger/logger'));
}

var _bitId;

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

var _;

function _load_() {
  return _ = require('..');
}

var _scopeRemotes;

function _load_scopeRemotes() {
  return _scopeRemotes = require('../scope-remotes');
}

var _enrichContextFromGlobal;

function _load_enrichContextFromGlobal() {
  return _enrichContextFromGlobal = _interopRequireDefault(require('../../hooks/utils/enrich-context-from-global'));
}

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