'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.pathHas = pathHas;
exports.pathHasAll = pathHasAll;
exports.propogateUntil = propogateUntil;

var _path;

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

var _fs;

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

var _findUp;

function _load_findUp() {
  return _findUp = _interopRequireDefault(require('find-up'));
}

var _constants;

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

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

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 composePath(patternPath, patterns) {
  return patterns.map(function (pattern) {
    return (_path || _load_path()).join(patternPath, pattern);
  });
}

/**
 * determine whether given path matches patterns
 */
function pathHas(patterns) {
  return function (absPath) {
    var state = false;
    var paths = composePath(absPath, patterns);
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      for (var _iterator = paths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var potentialPath = _step.value;

        if (state) return state;
        state = (_fs || _load_fs()).existsSync(potentialPath);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }

    return state;
  };
}

/**
 * determine whether given path have all files/dirs
 */
function pathHasAll(patterns) {
  return function (absPath) {
    var state = true;
    var paths = composePath(absPath, patterns);
    var _iteratorNormalCompletion2 = true;
    var _didIteratorError2 = false;
    var _iteratorError2 = undefined;

    try {
      for (var _iterator2 = paths[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
        var potentialPath = _step2.value;

        if (!state) return false;
        state = (_fs || _load_fs()).existsSync(potentialPath);
      }
    } catch (err) {
      _didIteratorError2 = true;
      _iteratorError2 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion2 && _iterator2.return) {
          _iterator2.return();
        }
      } finally {
        if (_didIteratorError2) {
          throw _iteratorError2;
        }
      }
    }

    return state;
  };
}

/**
 * propogates the FS from given path and until passing pattern function test.
 * @name propogateUntil
 * @param {string} fromPath
 * @param {(path: string) => boolean} pattern
 * @returns {string|null} first path to pass the test.
 * @example
 * ```js
 *  propogateUntil('/usr/local/var', (path) => path.indexOf('/usr') !== -1);
 *  // => '/usr/local/var'
 * ```
 */
function propogateUntil(fromPath) {
  if (!fromPath) return null;
  var filePath = (_findUp || _load_findUp()).default.sync([(_constants || _load_constants()).OBJECTS_DIR, (_path || _load_path()).join((_constants || _load_constants()).BIT_HIDDEN_DIR, (_constants || _load_constants()).OBJECTS_DIR), (_path || _load_path()).join((_constants || _load_constants()).DOT_GIT_DIR, (_constants || _load_constants()).BIT_GIT_DIR, (_constants || _load_constants()).OBJECTS_DIR)], { cwd: fromPath });
  if (!filePath) return null;
  return (_path || _load_path()).dirname(filePath);
}