import process from 'webpack-worker/process'
import Papa from 'papaparse'
import JSZip from 'jszip'
import _ from 'lodash'

process((params, emit) => {
  emit(0)
  let zip = new JSZip()
  zip.loadAsync(params.data)
    .then((zipContent) => {
      zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Blocks-IPv4.csv')
        .async('string')
        .then(data => {
          emit(25)
          Papa.parse(data, {
            header: true,
            complete: ipv4 => {
              emit(50)
              zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv')
                .async('string')
                .then(data => {
                  emit(75)
                  Papa.parse(data, {
                    header: true,
                    complete: meta => {
                      emit(100)
                      let dict = {}
                      meta.data.forEach(geo => {
                        dict[geo.geoname_id] = geo.country_name
                      })
                      return {
                        ipv4: ipv4.data,
                        meta: _.sortBy(meta.data, 'country_name'),
                        dict: dict
                      }
                    }
                  })
                })
            }
          })
        })
    })
})

@@ -151,20 +151,12 @@
 %7B%0A  
-emit(0)%0A  le
+cons
 t zi
@@ -177,93 +177,48 @@
 )%0A  
-zip.loadAsync(params.data)%0A    .then((zip
+const countryBlocksZip = 'GeoLite2-
 Co
+u
 nt
-ent) =%3E %7B%0A      zip.file('GeoLite2-Count
 ry-C
@@ -258,24 +258,193 @@
 ks-IPv4.csv'
+%0A  const countryLocationsZip = 'GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv'%0A%0A  const parseFile = (zipFile) =%3E %7B%0A    return zip%0A        .file(zipFile
 )%0A        .a
@@ -496,17 +496,52 @@
     
-emit(25)%0A
+return new Promise((resolve, reject) =%3E %7B%0A  
     
@@ -557,32 +557,34 @@
 a.parse(data, %7B%0A
+  
             head
@@ -609,140 +609,61 @@
     
-complete: ipv4 =%3E %7B%0A              emit(50)%0A              zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv')
+  error: reject,%0A              complete: results =%3E %7B
 %0A   
@@ -671,39 +671,39 @@
             
-.async('string'
+resolve(results
 )%0A          
@@ -710,34 +710,21 @@
     
-  .then(data =%3E %7B%0A
+%7D%0A   
         
     
@@ -711,32 +711,36 @@
    %7D%0A           
+ %7D)%0A
           emit(7
@@ -737,15 +737,9 @@
     
-emit(75
+%7D
 )%0A  
@@ -748,105 +748,211 @@
     
-          Papa.parse(data, %7B%0A                    header: true,%0A        
+%7D)%0A  %7D%0A%0A  return zip%0A      .loadAsync(params.data)%0A      .then(zipContent =%3E Promise.all(%5B%0A        parseFile(countryBlocksZip),%0A        parseFile(countryLocationsZip)%0A
       
+%5D))%0A
       
-complete:
+.then((%5Bipv4,
  meta
+%5D)
  =%3E 
@@ -965,82 +965,24 @@
     
-              emit(100)%0A                      let dict = %7B%7D%0A              
+const dict = %7B%7D%0A
     
@@ -1012,30 +1012,16 @@
 eo =%3E %7B%0A
-              
         
@@ -1074,39 +1074,12 @@
     
-              %7D)%0A              
+%7D)%0A%0A
     
@@ -1091,30 +1091,16 @@
 eturn %7B%0A
-              
         
@@ -1118,30 +1118,16 @@
 4.data,%0A
-              
         
@@ -1185,157 +1185,31 @@
     
-              dict: dict%0A                      %7D%0A                    %7D%0A                  %7D)%0A                %7D)%0A            %7D%0A          %7D)%0A        %7D)%0A
+dict: dict%0A        %7D%0A  
     

refactor the worker code and make it properly return results in a promise