"use strict";

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

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

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

  _bluebird = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _objects() {
  const data = require("../../../scope/objects");

  _objects = function () {
    return data;
  };

  return data;
}

function _utils() {
  const data = require("../../../utils");

  _utils = function () {
    return data;
  };

  return data;
}

function _objectsWithoutConsumer() {
  const data = _interopRequireDefault(require("./exceptions/objects-without-consumer"));

  _objectsWithoutConsumer = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _init = (0, _bluebird().coroutine)(function* (absPath = process.cwd(), noGit = false, reset = false, resetHard = false, force = false) {
    if (reset || resetHard) {
      yield _consumer().Consumer.reset(absPath, resetHard, noGit);
    }

    const consumer = yield _consumer().Consumer.create(absPath, noGit);

    if (!force) {
      yield throwForOutOfSyncScope(consumer);
    }

    return consumer.write();
  });

  function init() {
    return _init.apply(this, arguments);
  }

  return init;
}();
/**
 * throw an error when .bitmap is empty but a scope has objects.
 * a user may got into this state for reasons such as:
 * 1. deleting manually .bitmap hoping to re-start Bit from scratch. (probably unaware of `--reset-hard` flag).
 * 2. switching to a branch where Bit wasn't initialized
 * in which case, it's better to stop and show an error describing what needs to be done.
 * it can always be ignored by entering `--force` flag.
 */


exports.default = _default;

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

function _throwForOutOfSyncScope() {
  _throwForOutOfSyncScope = (0, _bluebird().coroutine)(function* (consumer) {
    if (!consumer.bitMap.isEmpty()) return;
    const scopePath = consumer.scope.getPath();

    const objectsPath = _objects().Repository.getPathByScopePath(scopePath);

    const dirExist = yield _fsExtra().default.pathExists(objectsPath);
    if (!dirExist) return;
    const hasObjects = !(yield (0, _utils().isDirEmpty)(objectsPath));

    if (hasObjects) {
      throw new (_objectsWithoutConsumer().default)(scopePath);
    }
  });
  return _throwForOutOfSyncScope.apply(this, arguments);
}