'use strict';

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

var _regenerator;

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

var _bluebird;

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

/**
 * throw an error when .bitmap is deleted 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.
 */
var throwForOutOfSyncScope = function () {
  var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(absPath, noGit) {
    var _BitMap$getBitMapLoca, bitMapLocation, scopePath, objectsPath, dirExist, hasObjects;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _BitMap$getBitMapLoca = (_bitMap || _load_bitMap()).default.getBitMapLocation(absPath), bitMapLocation = _BitMap$getBitMapLoca.currentLocation;

            if (!bitMapLocation) {
              _context2.next = 3;
              break;
            }

            return _context2.abrupt('return');

          case 3:
            scopePath = (_consumer || _load_consumer()).Consumer._getScopePath(absPath, noGit);
            objectsPath = (_objects || _load_objects()).Repository.getPathByScopePath(scopePath);
            _context2.next = 7;
            return (_fsExtra || _load_fsExtra()).default.pathExists(objectsPath);

          case 7:
            dirExist = _context2.sent;

            if (dirExist) {
              _context2.next = 10;
              break;
            }

            return _context2.abrupt('return');

          case 10:
            _context2.next = 12;
            return (0, (_utils || _load_utils()).isDirEmpty)(objectsPath);

          case 12:
            hasObjects = !_context2.sent;

            if (!hasObjects) {
              _context2.next = 15;
              break;
            }

            throw new (_objectsWithoutConsumer || _load_objectsWithoutConsumer()).default(scopePath);

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

  return function throwForOutOfSyncScope(_x6, _x7) {
    return _ref2.apply(this, arguments);
  };
}();

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _consumer;

function _load_consumer() {
  return _consumer = require('../../../consumer');
}

var _bitMap;

function _load_bitMap() {
  return _bitMap = _interopRequireDefault(require('../../../consumer/bit-map/bit-map'));
}

var _objects;

function _load_objects() {
  return _objects = require('../../../scope/objects');
}

var _utils;

function _load_utils() {
  return _utils = require('../../../utils');
}

var _objectsWithoutConsumer;

function _load_objectsWithoutConsumer() {
  return _objectsWithoutConsumer = _interopRequireDefault(require('./exceptions/objects-without-consumer'));
}

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

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
    var absPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.cwd();
    var noGit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var reset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var resetHard = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
    var force = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
    var overrideBitJson, consumer;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            overrideBitJson = false;

            if (!(reset || resetHard)) {
              _context.next = 7;
              break;
            }

            _context.next = 4;
            return (_consumer || _load_consumer()).Consumer.reset(absPath, resetHard, noGit);

          case 4:
            overrideBitJson = true;
            _context.next = 10;
            break;

          case 7:
            if (force) {
              _context.next = 10;
              break;
            }

            _context.next = 10;
            return throwForOutOfSyncScope(absPath, noGit);

          case 10:
            _context.next = 12;
            return (_consumer || _load_consumer()).Consumer.create(absPath, noGit);

          case 12:
            consumer = _context.sent;
            return _context.abrupt('return', consumer.write({ overrideBitJson }));

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

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

  return init;
}();