const _ = require('lodash')
const Promise = require('bluebird')

const { intelInput, intelExtract } = require('./intelli')
const findCandidates = require('./find-candidates')
const findSuggestions = require('./find-suggestions')
const extractLines = require('./extract-lines')
const summary = require('./summary')
const techConfig = require('../config')

function run(reporterType, techType, dirPath, ...filePaths) {
  const runnerConfig = techConfig[techType]
  if (_.isEmpty(runnerConfig)) {
    throw new Error(`Unsupported techType: ${techType}`)
  }

  return Promise.allKeys({
    intelliByFile: Promise.seq([
      intelInput.bind(null, [dirPath], runnerConfig),
      (filesByDirectory) => intelExtract(filesByDirectory, runnerConfig),
    ]),
    candidatesByFile: Promise.seq([
      extractLines.bind(null, filePaths),
      ({ files: linesByFile }) => findCandidates(linesByFile),
    ]),
  }).then((data) => summary(
    reporterType,
    findSuggestions(_.extend(data, {
      config: runnerConfig,
    }))
  ))
}

module.exports = {
  run,
}

@@ -573,50 +573,11 @@
 .all
-Keys(%7B%0A    intelliByFile: Promise.seq
 (%5B%0A
-  
     
@@ -586,28 +586,17 @@
 telInput
-.bind(null, 
+(
 %5BdirPath
@@ -611,24 +611,22 @@
 rConfig)
-,%0A      
+.then(
 (filesBy
@@ -638,16 +638,24 @@
 tory) =%3E
+ (%0A     
  intelEx
@@ -695,135 +695,257 @@
 fig)
-,
 %0A    
-%5D
+)
 ),%0A    
-candidatesByFile: Promise.seq(%5B%0A      extractLines.bind(null, filePaths),%0A      (%7B files: linesByFile %7D) =%3E findC
+extractLines(filePaths).then((%7B files: linesByFile %7D) =%3E (%0A      findCandidates(linesByFile)%0A    )),%0A  %5D).then((promiseResults) =%3E %7B%0A    const %5BintelliByFile, candidatesByFile%5D = promiseResults%0A%0A    return %7B%0A      intelliByFile,%0A      c
 andi
@@ -953,30 +953,21 @@
 ates
-(lines
 ByFile
-)
 ,%0A    
-%5D),
+%7D
 %0A  %7D
@@ -982,16 +982,22 @@
 data) =%3E
+ (%0A   
  summary
@@ -1002,16 +1002,18 @@
 ry(%0A    
+ 
+ 
 reporter
@@ -1018,16 +1018,18 @@
 erType,%0A
+  
     find
@@ -1044,25 +1044,45 @@
 ons(
-_.extend(
+%0A        _.extend(%0A          
 data, %7B%0A
     
@@ -1077,16 +1077,22 @@
 data, %7B%0A
+      
       co
@@ -1115,18 +1115,46 @@
 ig,%0A    
-%7D)
+      %7D%0A        )%0A      )%0A    
 )%0A  ))%0A%7D

Fix Promise usage.