'use babel';

import fs from 'fs';
import readInTld from '../readInTld';
import {add as addToRegistry} from '../registry';


export function register() {
    const userHome = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];

    let tldPathes = [];
    // TODO: refresh on config change
    atom.config.get('autocomplete-jsp.tldSources').forEach(dir => {
            dir = dir.replace('~', userHome);

            fs.readdirSync(dir)
                .filter(fileName => fileName.endsWith('.tld'))
                .forEach(fileName => {
                    const path = `${dir.replace(/\/$/, '')}/${fileName}`;
                    tldPathes.push(path);
                });
        });

    // TODO: when a tld changes, we have to some how reload it..
    Promise.all(tldPathes.map(readInTld))
        .then(tldDescs => {
            tldDescs.forEach(tldDesc => {
                tldDesc.functions.forEach(fnDesc => {
                    addToRegistry({
                        element: fnDesc,
                        // TODO: not Infinity
                        liveTime: Infinity,
                    });
                });

                tldDesc.tags.forEach(tagDesc => {
                    addToRegistry({
                        element: tagDesc,
                        // TODO: not Infinity
                        liveTime: Infinity,
                    });
                });
            });
        })
        .catch(err => {
            atom.notifications.addWarning(err.msg, {
                dismissable: true,
                detail: `Caused by:\n${err.causedBy}`,
            });
        });
}

@@ -180,17 +180,16 @@
 ess.env%5B
-(
 process.
@@ -208,17 +208,16 @@
  'win32'
-)
  ? 'USER
@@ -240,160 +240,181 @@
 '%5D;%0A
-%0A
     
-le
+cons
 t tld
-Pathes = %5B%5D;%0A    // TODO: refresh on config change%0A    atom.config.get('autocomplete-jsp.tldSources').forEach(dir =%3E %7B%0A            dir = dir
+Sources = atom.config.get('autocomplete-jsp.tldSources');%0A%0A    // TODO: refresh on config change%0A    Promise.all(tldSources%0A            .map(path =%3E path = path
 .rep
@@ -432,18 +432,17 @@
 serHome)
-;%0A
+)
 %0A       
@@ -450,60 +450,101 @@
     
-fs.readdirSync(dir)%0A                .filter(
+.map(path =%3E new Promise((resolve, reject) =%3E%0A                fs.readdir(path, (err, 
 fileName
  =%3E 
@@ -543,63 +543,142 @@
 Name
+s)
  =%3E 
-fileName.endsWith('.tld'))%0A                .forEach
+%7B%0A                    if (err) %7B%0A                        return reject(err);%0A                    %7D%0A%0A%0A                    resolve
 (fil
@@ -682,22 +682,24 @@
 fileName
- =%3E %7B%0A
+s%0A      
         
@@ -712,29 +712,91 @@
     
-  const path
+.filter(name =%3E name.endsWith('.tld'))%0A                        .map(name
  =
+%3E
  %60$%7B
-dir
+path
 .rep
@@ -818,19 +818,15 @@
 %7D/$%7B
-fileN
+n
 ame%7D%60
-;
+)
 %0A   
@@ -846,27 +846,8 @@
     
-tldPathes.push(path
 );%0A 
@@ -855,33 +855,32 @@
               %7D)
-;
 %0A        %7D);%0A%0A  
@@ -876,118 +876,217 @@
     
-%7D);%0A%0A    // TODO: when a tld changes, we have to some how reload it..%0A    Promise.all(tldPathes.map(readInTld)
+    ))%0A        )%0A        // TODO: reload on change%0A        .then(result =%3E Promise.all(result%0A                .reduce((all, next) =%3E all.concat(next), %5B%5D)%0A                .map(readInTld)%0A            )%0A        
 )%0A  
@@ -1105,25 +1105,32 @@
 tldDescs =%3E 
-%7B
+tldDescs
 %0A           
@@ -1130,24 +1130,16 @@
         
-tldDescs
 .forEach
@@ -1199,26 +1199,24 @@
 ch(fnDesc =%3E
- %7B
 %0A           
@@ -1285,54 +1285,8 @@
 sc,%0A
-                        // TODO: not Infinity%0A
     
@@ -1339,33 +1339,32 @@
               %7D)
-;
 %0A               
@@ -1364,17 +1364,16 @@
         
-%7D
 );%0A%0A    
@@ -1411,26 +1411,24 @@
 h(tagDesc =%3E
- %7B
 %0A           
@@ -1498,54 +1498,8 @@
 sc,%0A
-                        // TODO: not Infinity%0A
     
@@ -1552,33 +1552,32 @@
               %7D)
-;
 %0A               
@@ -1569,33 +1569,32 @@
                 
-%7D
 );%0A            %7D
@@ -1586,33 +1586,32 @@
 ;%0A            %7D)
-;
 %0A        %7D)%0A    
@@ -1595,33 +1595,32 @@
      %7D)%0A        
-%7D
 )%0A        .catch
@@ -1626,18 +1626,16 @@
 h(err =%3E
- %7B
 %0A       
@@ -1784,17 +1784,16 @@
       %7D)
-;
 %0A       
@@ -1793,14 +1793,13 @@
         
-%7D
 );%0A%7D%0A

Load tld files completely async