"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _fs = _interopRequireDefault(require("fs"));

var _path = _interopRequireDefault(require("path"));

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

let iterateDirectory = (srcPath, targetPath, extensions, flatten, cb) => {
  _fs.default.readdirSync(srcPath).forEach(fileOrDir => {
    let fromPath = _path.default.join(srcPath, fileOrDir);

    let toPath = targetPath;

    if (flatten !== 'flatten' || !_fs.default.statSync(fromPath).isDirectory()) {
      toPath = _path.default.join(targetPath, fileOrDir);
    }

    if (_fs.default.statSync(fromPath).isDirectory()) {
      if (!_fs.default.existsSync(toPath)) {
        _fs.default.mkdirSync(toPath);
      }

      iterateDirectory(fromPath, toPath, extensions, flatten, cb);
    } else {
      if (extensions) {
        let {
          ext
        } = _path.default.parse(fromPath);

        if (extensions.indexOf(ext) !== -1) {
          cb(fromPath, toPath);
        }
      } else {
        cb(fromPath, toPath);
      }
    }
  });
};

var _default = iterateDirectory;
exports.default = _default;