const fs = require('fs')
const path = require('path')
const mime = require('mime-types')


function getDirectories(srcpath) {
  return fs.readdirSync(srcpath).filter(function(file) {
    return fs.statSync(path.join(srcpath, file)).isDirectory()
  })
}

function createFileList(dir) {
  const str = dir.substr(dir.lastIndexOf('/') + 1) + '$'
  const rootpath = dir.replace(new RegExp(str), '')
  const paths = getFilepaths(dir, [], rootpath)
  return paths.map(path => {
    return createFile(path, path.replace(rootpath, ''))
  })
}

function getFilepaths(dir, files_) {
  files_ = files_ || []
  const files = fs.readdirSync(dir)
  files.map(file => `${dir}/${file}`)
    .map(path => isDirectory(path)
      ? getFilepaths(path, files_)
      : files_.push(path)
    )
  return files_
}

function isDirectory(path) {
  const pathStat = fs.lstatSync(path)
  let isDir = pathStat.isDirectory()
  if (pathStat.isSymbolicLink()) {
    try {
      var targetPath = fs.realpathSync(path)
      isDir = fs.lstatSync(targetPath).isDirectory()
    } catch (err) {
      isDir = false
    }
  }
  return isDir
}

function addFileList(input, file_paths) {
  if (typeof file_paths === 'string')
    file_paths = [file_paths]
  else if (!Array.isArray(file_paths)) {
    throw new Error('file_paths needs to be a file path string or an Array of file path strings')
  }

  const file_list = file_paths.map(fp => createFile(fp))
  file_list.__proto__ = Object.create(FileList.prototype)

  Object.defineProperty(input, 'files', {
    value: file_list,
    writable: false,
  })

  return input
}

function createFile(file_path, relativePath) {
  const file = fs.statSync(file_path)

  const browserFile = new File(
    [new fs.readFileSync(file_path)],
    path.basename(file_path),
    { lastModified: file.mtimeMs }
  )
  browserFile.webkitRelativePath = relativePath || file_path
  browserFile.type = mime.lookup(file_path) || ''
  return browserFile
}


module.exports = {
  addFileList,
  createFile,
  createFileList,
  getDirectories
}
/**
 * for use in test suites using File & FileList browser APIs in jsdom environment
 */

const fs = require('fs')
const path = require('path')
const mime = require('mime-types')


function getDirectories(srcpath) {
  return fs.readdirSync(srcpath).filter(function(file) {
    return fs.statSync(path.join(srcpath, file)).isDirectory()
  })
}

function createFileList(dir) {
  const str = dir.substr(dir.lastIndexOf('/') + 1) + '$'
  const rootpath = dir.replace(new RegExp(str), '')
  const paths = getFilepaths(dir, [], rootpath)
  return paths.map(path => {
    return createFile(path, path.replace(rootpath, ''))
  })
}

function getFilepaths(dir, files_) {
  files_ = files_ || []
  const files = fs.readdirSync(dir)
  files.map(file => `${dir}/${file}`)
    .map(path => isDirectory(path)
      ? getFilepaths(path, files_)
      : files_.push(path)
    )
  return files_
}

function isDirectory(path) {
  const pathStat = fs.lstatSync(path)
  let isDir = pathStat.isDirectory()
  if (pathStat.isSymbolicLink()) {
    try {
      var targetPath = fs.realpathSync(path)
      isDir = fs.lstatSync(targetPath).isDirectory()
    } catch (err) {
      isDir = false
    }
  }
  return isDir
}

function addFileList(input, file_paths) {
  if (typeof file_paths === 'string')
    file_paths = [file_paths]
  else if (!Array.isArray(file_paths)) {
    throw new Error('file_paths needs to be a file path string or an Array of file path strings')
  }

  const file_list = file_paths.map(fp => createFile(fp))
  file_list.__proto__ = Object.create(FileList.prototype)

  Object.defineProperty(input, 'files', {
    value: file_list,
    writable: false,
  })

  return input
}

function createFile(file_path, relativePath) {
  const file = fs.statSync(file_path)

  const browserFile = new File(
    [new fs.readFileSync(file_path)],
    path.basename(file_path),
    { lastModified: file.mtimeMs }
  )
  browserFile.webkitRelativePath = relativePath || file_path
  browserFile.type = mime.lookup(file_path) || ''
  return browserFile
}


module.exports = {
  addFileList,
  createFile,
  createFileList,
  getDirectories
}
add comment clarifying env/FileList