export function parseFiles(files) {
  for (let i = 0; i < files.length; i++) {
    parseFile(files[i])
  }
  return files
}

export function combineFilesWithArticles(articles, files) {
  let articleMap = new Map()

  articles.forEach(article => {
    article.files = []
    articleMap.set(article.id, article)
  })
  files.forEach(file => {
    articleMap.get(file.id).files.push(parseFile(file))
  })
}

export function parseFile(file) {
  file.path = 'https://cdn.nfp.is' + file.path
  file.magnet = null
  file.meta = JSON.parse(file.meta || '{}') || {}
  if (file.meta.torrent) {
    file.magnet = 'magnet:?'
      + 'xl=' + file.size
      + '&dn=' + encodeURIComponent(file.meta.torrent.name)
      + '&xt=urn:btih:' + file.meta.torrent.hash
      + file.meta.torrent.announce.map(item => ('&tr=' + encodeURIComponent(item))).join('')
    file.meta.torrent = { name: file.meta.torrent.name, files: file.meta.torrent.files }
  }
  return file
}