'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.pathHasConsumer = pathHasConsumer;
exports.pathHasBitMap = pathHasBitMap;
exports.pathHasBitJson = pathHasBitJson;
exports.pathHasLocalScope = pathHasLocalScope;
exports.locateConsumer = locateConsumer;

var _path;

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

var _fs;

function _load_fs() {
  return _fs = _interopRequireWildcard(require('fs'));
}

var _constants;

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

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 composeBitHiddenDirPath(path) {
  return (_path || _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 (_path || _load_path()).join(path, (_constants || _load_constants()).DOT_GIT_DIR, (_constants || _load_constants()).BIT_GIT_DIR);
}

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

/**
 * determine whether given path has a consumer
 */
function pathHasConsumer(path) {
  return ((_fs || _load_fs()).existsSync(composeBitHiddenDirPath(path)) || (_fs || _load_fs()).existsSync(composeBitGitHiddenDirPath(path))) && (_fs || _load_fs()).existsSync(composeBitJsonPath(path));
}

/**
 * determine whether given path has a bitmap
 */
function pathHasBitMap(path) {
  return (_fs || _load_fs()).existsSync((_path || _load_path()).join(path, (_constants || _load_constants()).BIT_MAP)) || (_fs || _load_fs()).existsSync((_path || _load_path()).join(path, (_constants || _load_constants()).OLD_BIT_MAP));
}
/**
 * determine whether given path has a bit.Json
 */
function pathHasBitJson(path) {
  return (_fs || _load_fs()).existsSync(composeBitJsonPath(path));
}

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

/**
 * recursively propogate the FS directory structure to find a box.
 */
function locateConsumer(absPath) {
  function buildPropogationPaths() {
    var paths = [];
    var pathParts = absPath.split((_path || _load_path()).sep);

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

    return paths.reverse();
  }

  if (pathHasConsumer(absPath) || pathHasBitMap(absPath)) return absPath;
  var searchPaths = buildPropogationPaths();
  return searchPaths.find(function (searchPath) {
    return pathHasConsumer(searchPath) || pathHasBitMap(searchPath);
  });
}