'use babel';

const {
    BufferedNodeProcess,
    CompositeDisposable
} = require('atom'); // eslint-disable-line import/no-unresolved

const { readdir } = require('fs');
const path = require('path');
const P = require('promalom');

const AvailableCodemods = require('./available-codemods');

module.exports = {
    subscriptions: null,

    activate(state) { // eslint-disable-line no-unused-vars
        // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
        this.subscriptions = new CompositeDisposable();

        // Register command that toggles this view
        this.subscriptions.add(atom.commands.add('atom-workspace', {
            'codemod:list': () => this.showCodemodList()
        }));
    },

    deactivate() {
        this.subscriptions.dispose();
    },

    serialize() {

    },

    showCodemodList() {
        const jsCodemod = {
            displayName: 'js-codemod',
            path: path.join(
                __dirname,
                '..',
                'codemods',
                'js-codemod',
                'transforms'
            )
        };

        const reactCodemod = {
            displayName: 'react-codemod',
            path: path.join(
                __dirname,
                '..',
                'codemods',
                'react-codemod',
                'transforms'
            )
        };

        Promise.all([
            jsCodemod,
            reactCodemod
        ].map((codemod) => {
            return P.promisify(readdir)(codemod.path).then(files => {
                return {
                    displayName: codemod.displayName,
                    path: codemod.path,
                    files
                };
            });
        })).then(transforms => {
            return transforms.reduce((acc, current) => {
                return acc.concat(current.files
                    .filter(file => file.indexOf('__') === -1)
                    .map(file => {
                        return {
                            file,
                            displayName: `${current.displayName} / ${file.split('.')[0]}`,
                            path: current.path
                        };
                    })
                );
            }, []);
        }).then(codemods => {
            const availableCodemodsView = new AvailableCodemods(codemods, item => {
                this.runCodemode(item);
                availableCodemodsView.cancel();
            });
        });
    },

    runCodemode(transform) {
        const jscodeshift = path.join(
            __dirname,
            '..',
            'node_modules',
            '.bin',
            'jscodeshift'
        );

        new BufferedNodeProcess({ // eslint-disable-line no-new
            command: jscodeshift,
            args: [
                '-t',
                path.join(transform.path, transform.file),
                atom.workspace.getActivePaneItem().getPath()
            ],
            stdout: output => {
                console.log('stdout', output);

                const div = document.createElement('div');
                div.innerText = output;

                const panel = atom.workspace.addModalPanel({ item: div });

                setTimeout(() => {
                    panel.destroy();
                }, 1000);
            },
            stderr: output => {
                console.log('stdout', output);

                const div = document.createElement('div');
                div.innerText = output;

                const panel = atom.workspace.addModalPanel({ item: div });

                setTimeout(() => {
                    panel.destroy();
                }, 1000);
            },
            exit: returnCode => console.log('Return Code', returnCode)
        });
    }
};

@@ -1381,32 +1381,273 @@
   )%0A        %7D;%0A%0A
+        function listCodemods(codemods) %7B%0A            const availableCodemodsView = new AvailableCodemods(codemods, item =%3E %7B%0A                this.runCodemode(item);%0A                availableCodemodsView.cancel();%0A            %7D);%0A        %7D%0A%0A
         Promise.
@@ -1718,41 +1718,18 @@
 map(
-(
 codemod
-)
  =%3E
- %7B%0A            return
  P.p
@@ -1778,33 +1778,9 @@
  =%3E 
-%7B%0A                return 
+(
 %7B%0A  
@@ -1785,32 +1785,24 @@
             
- 
-       
 displayName:
@@ -1831,32 +1831,24 @@
             
-        
 path: codemo
@@ -1863,32 +1863,24 @@
             
-       
- 
 files%0A      
@@ -1885,44 +1885,11 @@
     
-        %7D;%0A            %7D);%0A        %7D
+%7D))
 )).t
@@ -1909,29 +1909,8 @@
 s =%3E
- %7B%0A            return
  tra
@@ -1945,33 +1945,8 @@
 ) =%3E
- %7B%0A                return
  acc
@@ -1963,28 +1963,24 @@
 rrent.files%0A
-    
             
@@ -2042,20 +2042,16 @@
         
-   
- 
 .map(fil
@@ -2059,51 +2059,11 @@
  =%3E 
+(
 %7B%0A
-                        return %7B%0A        
     
@@ -2100,32 +2100,24 @@
             
- 
-       
 displayName:
@@ -2183,32 +2183,24 @@
             
-        
 path: curren
@@ -2226,80 +2226,29 @@
     
-        %7D;%0A                    %7D)%0A                );%0A            %7D
+%7D)%0A            )
 , %5B%5D)
-;
 %0A   
@@ -2256,227 +2256,28 @@
     
-%7D
+)
 ).then(
-codemods =%3E %7B%0A            const availableCodemodsView = new AvailableCodemods(codemods, item =%3E %7B%0A                this.runCodemode(item);%0A                availableCodemodsView.cancel();%0A            %7D);%0A        %7D
+listCodemods
 );%0A 

fix linting