'use strict';

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

var _regenerator;

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

var _bluebird;

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

/**
 * propagate from the given directory up to the root to find the consumer
 */
var getConsumerInfo = exports.getConsumerInfo = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4(absPath) {
    var pathHasBitMap = function () {
      var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(path) {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return (_fsExtra || _load_fsExtra()).default.exists((_path2 || _load_path()).join(path, (_constants || _load_constants()).BIT_MAP));

              case 2:
                _context.t0 = _context.sent;

                if (_context.t0) {
                  _context.next = 5;
                  break;
                }

                _context.t0 = (_fsExtra || _load_fsExtra()).default.exists((_path2 || _load_path()).join(path, (_constants || _load_constants()).OLD_BIT_MAP));

              case 5:
                return _context.abrupt('return', _context.t0);

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

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

    var pathHasScopeDir = function () {
      var _ref3 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(path) {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return (_fsExtra || _load_fsExtra()).default.exists(composeBitHiddenDirPath(path));

              case 2:
                _context2.t0 = _context2.sent;

                if (_context2.t0) {
                  _context2.next = 5;
                  break;
                }

                _context2.t0 = (_fsExtra || _load_fsExtra()).default.exists(composeBitGitHiddenDirPath(path));

              case 5:
                return _context2.abrupt('return', _context2.t0);

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

      return function pathHasScopeDir(_x3) {
        return _ref3.apply(this, arguments);
      };
    }();

    var getConsumerConfigIfExists = function () {
      var _ref4 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(path) {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.prev = 0;
                _context3.next = 3;
                return (_workspaceConfig || _load_workspaceConfig()).default.load(path);

              case 3:
                return _context3.abrupt('return', _context3.sent);

              case 6:
                _context3.prev = 6;
                _context3.t0 = _context3['catch'](0);

                if (!(_context3.t0 instanceof (_exceptions || _load_exceptions()).BitConfigNotFound)) {
                  _context3.next = 10;
                  break;
                }

                return _context3.abrupt('return', null);

              case 10:
                throw _context3.t0;

              case 11:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this, [[0, 6]]);
      }));

      return function getConsumerConfigIfExists(_x4) {
        return _ref4.apply(this, arguments);
      };
    }();

    var searchPaths, i, _path, _hasScope, _consumerConfig, _hasBitMap, consumerExists, buildPropagationPaths;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            buildPropagationPaths = function buildPropagationPaths() {
              var paths = [];
              var pathParts = absPath.split((_path2 || _load_path()).sep);

              pathParts.forEach(function (val, index) {
                var part = pathParts.slice(0, index).join('/');
                if (!part) return;
                paths.push(part);
              });

              return paths.reverse();
            };

            searchPaths = buildPropagationPaths();

            searchPaths.unshift(absPath);
            i = 0;

          case 4:
            if (!(i < searchPaths.length)) {
              _context4.next = 21;
              break;
            }

            _path = searchPaths[i];
            _context4.next = 8;
            return pathHasScopeDir(_path);

          case 8:
            _hasScope = _context4.sent;
            _context4.next = 11;
            return getConsumerConfigIfExists(_path);

          case 11:
            _consumerConfig = _context4.sent;
            _context4.next = 14;
            return pathHasBitMap(_path);

          case 14:
            _hasBitMap = _context4.sent;
            // eslint-disable-line no-await-in-loop
            consumerExists = _hasScope && _consumerConfig || _hasBitMap;

            if (!consumerExists) {
              _context4.next = 18;
              break;
            }

            return _context4.abrupt('return', {
              path: _path,
              hasScope: _hasScope,
              consumerConfig: _consumerConfig,
              hasBitMap: _hasBitMap
            });

          case 18:
            i += 1;
            _context4.next = 4;
            break;

          case 21:
            return _context4.abrupt('return', null);

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

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

exports.pathHasBitJson = pathHasBitJson;
exports.pathHasLocalScope = pathHasLocalScope;

var _path2;

function _load_path() {
  return _path2 = _interopRequireWildcard(require('path'));
}

var _fsExtra;

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

var _constants;

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

var _workspaceConfig;

function _load_workspaceConfig() {
  return _workspaceConfig = _interopRequireDefault(require('./config/workspace-config'));
}

var _exceptions;

function _load_exceptions() {
  return _exceptions = require('./config/exceptions');
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function composeBitHiddenDirPath(path) {
  return (_path2 || _load_path()).join(path, (_constants || _load_constants()).BIT_HIDDEN_DIR);
} /**
   * 
   * @deprecated
   * @TODO deprecated and should be removed from here and use fs-propogate-until instead...
   */


function composeBitGitHiddenDirPath(path) {
  return (_path2 || _load_path()).join(path, (_constants || _load_constants()).DOT_GIT_DIR, (_constants || _load_constants()).BIT_GIT_DIR);
}

function composeBitJsonPath(path) {
  return (_path2 || _load_path()).join(path, (_constants || _load_constants()).BIT_JSON);
}

/**
 * determine whether given path has a bit.Json
 */
function pathHasBitJson(path) {
  return (_fsExtra || _load_fsExtra()).default.existsSync(composeBitJsonPath(path));
}

/**
 * determine whether given path has .bit
 */
function pathHasLocalScope(path) {
  return (_fsExtra || _load_fsExtra()).default.existsSync(composeBitHiddenDirPath(path));
}