'use strict';

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

var scanDirectoryInternal = function () {
  var _ref = _asyncToGenerator(function* (path, recursive, validate, result) {
    var itemStat = yield stat(path);
    if (itemStat.isFile()) {
      result.files.push(path);
    } else if (itemStat.isDirectory() && recursive !== 0) {
      result.directories.push(path);
    }
    if (!itemStat.isDirectory() || recursive === 0) {
      return;
    }
    var contents = yield readdir(path);
    yield (0, _pMap2.default)(contents, function () {
      var _ref2 = _asyncToGenerator(function* (item) {
        var itemPath = _path2.default.join(path, item);
        if (validate(itemPath)) {
          yield scanDirectoryInternal(itemPath, recursive === 1 ? 0 : 2, validate, result);
        }
      });

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

  return function scanDirectoryInternal(_x, _x2, _x3, _x4) {
    return _ref.apply(this, arguments);
  };
}();

var scanDirectory = function () {
  var _ref3 = _asyncToGenerator(function* (path) {
    var givenRecursive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
    var givenValidate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

    (0, _assert2.default)(path && typeof path === 'string', 'path must be a valid string');
    (0, _assert2.default)(!givenValidate || typeof givenValidate === 'function', 'validate must be a valid function');

    var recursive = !!givenRecursive;
    var validate = givenValidate || function (itemPath) {
      return _path2.default.basename(itemPath).substr(0, 1) !== '.';
    };

    var result = { files: [], directories: [] };
    yield scanDirectoryInternal(path, recursive ? 2 : 1, validate, result);
    return result;
  });

  return function scanDirectory(_x6) {
    return _ref3.apply(this, arguments);
  };
}();

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _pMap = require('p-map');

var _pMap2 = _interopRequireDefault(_pMap);

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _sbPromisify = require('sb-promisify');

var _sbPromisify2 = _interopRequireDefault(_sbPromisify);

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var stat = (0, _sbPromisify2.default)(_fs2.default.stat);
var readdir = (0, _sbPromisify2.default)(_fs2.default.readdir);

exports.default = scanDirectory;