var fetch = require('node-fetch')
var fs = require('fs')
var path = require('path')

var log = console.log
var WORDLISTS = ['english', 'french', 'italian', 'japanese', 'spanish']

function update () {
  download().then(function (wordlists) {
    var promises = Object.keys(wordlists).map(function (name) { return save(name, wordlists[name]) })
    return Promise.all(promises)
  })
}

function download () {
  var wordlists = {}

  var promises = WORDLISTS.map(function (name) {
    return fetchRaw(name).then(toJSON).then(function (wordlist) { wordlists[name] = wordlist })
  })

  return Promise.all(promises).then(function () { return wordlists })
}

function fetchRaw (name) {
  var url = 'https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/' + name + '.txt'
  log('download ' + url)

  return fetch(url).then(function (response) { return response.text() })
}

function toJSON (content) {
  return content.trim().split('\n').map(function (word) { return word.trim() })
}

function save (name, wordlist) {
  var location = path.join(__dirname, '..', 'wordlists', name + '.json')
  var content = JSON.stringify(wordlist, null, 2) + '\n'
  log('save ' + wordlist.length + ' words to ' + location)

  return new Promise(function (resolve, reject) {
    fs.writeFile(location, content, function (err) {
      if (err) reject(err)
      else resolve()
    })
  })
}

module.exports = { update: update, download: download }

@@ -117,16 +117,68 @@
 ISTS = %5B
+%0A  'chinese_simplified',%0A  'chinese_traditional',%0A  
 'english
@@ -179,16 +179,18 @@
 nglish',
+%0A 
  'french
@@ -191,16 +191,18 @@
 french',
+%0A 
  'italia
@@ -204,16 +204,18 @@
 talian',
+%0A 
  'japane
@@ -218,16 +218,18 @@
 panese',
+%0A 
  'spanis
@@ -230,16 +230,17 @@
 spanish'
+%0A
 %5D%0A%0Afunct

Update wordlists in util/wordlists.js