"use strict";

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

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

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

var _index = require("./index");

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

let removeDirectory = dirPath => {
  _fs.default.readdirSync(dirPath).forEach(fileOrDir => {
    let fileOrDirPath = _path.default.join(dirPath, fileOrDir);

    if (_fs.default.statSync(fileOrDirPath).isDirectory()) {
      removeDirectory(fileOrDirPath);
    } else {
      _fs.default.unlinkSync(fileOrDirPath);
    }
  });

  _fs.default.rmdirSync(dirPath);
};

let copyFile = (srcPath, targetPath, isCopy = true, changeExt) => {
  let {
    ext,
    dir,
    name,
    root
  } = _path.default.parse(targetPath);

  if (changeExt[ext]) {
    ext = changeExt[ext];
    targetPath = _path.default.format({
      ext,
      dir,
      name,
      root
    }); //eslint-disable-line
  }

  let readStream = _fs.default.createReadStream(srcPath);

  let writeStream = _fs.default.createWriteStream(targetPath);

  readStream.pipe(writeStream);
  writeStream.on('finish', () => {
    if (!isCopy) {
      _fs.default.unlinkSync(srcPath);
    }
  });
};

let iterateDirectory = (srcPath, targetPath, isCopy, extensions, flatten, changeExt) => {
  _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, isCopy, extensions, flatten, changeExt);
    } else {
      if (extensions) {
        let {
          ext
        } = _path.default.parse(fromPath);

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

let copy = (srcPath, targetPath, isCopy, exts, flatten, changeExt = {}) => {
  if (_fs.default.statSync(srcPath).isDirectory()) {
    if (!_fs.default.existsSync(targetPath)) {
      _fs.default.mkdirSync(targetPath);
    }

    let {
      name
    } = _path.default.parse(srcPath);

    let originPath = targetPath;

    if (flatten !== 'flatten') {
      originPath = _path.default.join(targetPath, name);

      if (!_fs.default.existsSync(originPath)) {
        _fs.default.mkdirSync(originPath);
      }
    }

    iterateDirectory(srcPath, originPath, isCopy, exts, flatten, changeExt);
  } else {
    /* direct file copy issue there but that feature not needed i think*/
    copyFile(srcPath, targetPath, isCopy, changeExt);
  }

  if (!isCopy) {
    removeDirectory(srcPath);
  }

  (0, _index.log)(isCopy ? 'Folder/file are copied!' : 'Folder/file are moved!');
};

exports.copy = copy;