"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _groupArray() {
  const data = _interopRequireDefault(require("group-array"));

  _groupArray = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

function _() {
  const data = require("..");

  _ = function () {
    return data;
  };

  return data;
}

function _enrichContextFromGlobal() {
  const data = _interopRequireDefault(require("../../hooks/utils/enrich-context-from-global"));

  _enrichContextFromGlobal = function () {
    return data;
  };

  return data;
}

function _remotes() {
  const data = _interopRequireDefault(require("../../remotes/remotes"));

  _remotes = function () {
    return data;
  };

  return data;
}

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

function _deprecateMany() {
  _deprecateMany = (0, _bluebird().coroutine)(function* (scope, bitIds) {
    _logger().default.debug(`scope.deprecateMany, ids: ${bitIds.toString()}`);

    return _deprecationMany(scope, bitIds, _deprecateSingle);
  });
  return _deprecateMany.apply(this, arguments);
}

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

function _undeprecateMany() {
  _undeprecateMany = (0, _bluebird().coroutine)(function* (scope, bitIds) {
    _logger().default.debug(`scope.undeprecateMany, ids: ${bitIds.toString()}`);

    return _deprecationMany(scope, bitIds, _undeprecateSingle);
  });
  return _undeprecateMany.apply(this, arguments);
}

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

function _deprecateRemote() {
  _deprecateRemote = (0, _bluebird().coroutine)(function* (remotes, scope, bitIds) {
    return _deprecationRemote(remotes, scope, bitIds, true);
  });
  return _deprecateRemote.apply(this, arguments);
}

function undeprecateRemote(_x8, _x9, _x10) {
  return _undeprecateRemote.apply(this, arguments);
}

function _undeprecateRemote() {
  _undeprecateRemote = (0, _bluebird().coroutine)(function* (remotes, scope, bitIds) {
    return _deprecationRemote(remotes, scope, bitIds, false);
  });
  return _undeprecateRemote.apply(this, arguments);
}

function _deprecationMany(_x11, _x12, _x13) {
  return _deprecationMany2.apply(this, arguments);
}

function _deprecationMany2() {
  _deprecationMany2 = (0, _bluebird().coroutine)(function* (scope, ids, deprecationAction) {
    const {
      missingComponents,
      foundComponents
    } = yield scope.filterFoundAndMissingComponents(ids);
    const bitIdsP = foundComponents.map(bitId => deprecationAction(scope, bitId));
    const bitIds = yield Promise.all(bitIdsP);
    yield scope.objects.persist();
    const missingComponentsStrings = missingComponents.map(id => id.toStringWithoutVersion());
    return {
      bitIds,
      missingComponents: missingComponentsStrings
    };
  });
  return _deprecationMany2.apply(this, arguments);
}

function _deprecationRemote(_x14, _x15, _x16, _x17) {
  return _deprecationRemote2.apply(this, arguments);
}

function _deprecationRemote2() {
  _deprecationRemote2 = (0, _bluebird().coroutine)(function* (remotes, scope, bitIds, deprecate) {
    const groupedBitsByScope = (0, _groupArray().default)(bitIds, 'scope');
    const context = {};
    (0, _enrichContextFromGlobal().default)(context);
    const deprecateP = Object.keys(groupedBitsByScope).map(
    /*#__PURE__*/
    function () {
      var _ref = (0, _bluebird().coroutine)(function* (scopeName) {
        const resolvedRemote = yield remotes.resolve(scopeName, scope);
        const idsStr = groupedBitsByScope[scopeName].map(id => id.toStringWithoutVersion());
        const deprecateResult = deprecate ? yield resolvedRemote.deprecateMany(idsStr, context) : yield resolvedRemote.undeprecateMany(idsStr, context);
        return deprecateResult;
      });

      return function (_x22) {
        return _ref.apply(this, arguments);
      };
    }());
    return Promise.all(deprecateP);
  });
  return _deprecationRemote2.apply(this, arguments);
}

function _deprecateSingle(_x18, _x19) {
  return _deprecateSingle2.apply(this, arguments);
}

function _deprecateSingle2() {
  _deprecateSingle2 = (0, _bluebird().coroutine)(function* (scope, bitId) {
    const component = yield scope.getModelComponent(bitId);
    component.deprecated = true;
    scope.objects.add(component);
    return bitId.toStringWithoutVersion();
  });
  return _deprecateSingle2.apply(this, arguments);
}

function _undeprecateSingle(_x20, _x21) {
  return _undeprecateSingle2.apply(this, arguments);
}

function _undeprecateSingle2() {
  _undeprecateSingle2 = (0, _bluebird().coroutine)(function* (scope, bitId) {
    const component = yield scope.getModelComponent(bitId);
    component.deprecated = false;
    scope.objects.add(component);
    return bitId.toStringWithoutVersion();
  });
  return _undeprecateSingle2.apply(this, arguments);
}