import { includes, mapValues, isObject, isArray } from 'lodash'

import {
  COMMAND_KEY,
  ENV_KEY,
  DEPENDENCIES_KEY,
  BEFORE_KEY,
  OPTIONAL_BEFORE_KEY,
  AFTER_KEY,
  OPTIONAL_AFTER_KEY,
  ANYTIME_KEY
} from './configKeys'
import { SEP } from './paramHelpers'
import { fatalError } from './logging'

export default function expandConfig (config) {
  if (includes(Object.keys(config).join(''), SEP)) {
    fatalError(`Task names cannot include a "${SEP}" character!`)
  }

  return mapValues(config, expandTask)
}

function expandTask (task) {
  const expandedTask = {
    [COMMAND_KEY]: [],
    [ENV_KEY]: {},
    [DEPENDENCIES_KEY]: {
      [BEFORE_KEY]: [],
      [OPTIONAL_BEFORE_KEY]: [],
      [AFTER_KEY]: [],
      [OPTIONAL_AFTER_KEY]: [],
      [ANYTIME_KEY]: []
    }
  }
  if (isObject(task)) {
    if (DEPENDENCIES_KEY in task) {
      if (!isObject(task[DEPENDENCIES_KEY])) {
        expandedTask[DEPENDENCIES_KEY][BEFORE_KEY] = task[DEPENDENCIES_KEY]
      }
      for (let property of [BEFORE_KEY, OPTIONAL_BEFORE_KEY, AFTER_KEY, OPTIONAL_AFTER_KEY, ANYTIME_KEY]) {
        if (!isArray(task[DEPENDENCIES_KEY][property])) {
          task[DEPENDENCIES_KEY][property] = [task[DEPENDENCIES_KEY][property]]
        }
      }
    }
  } else {
    expandedTask[COMMAND_KEY] = task
  }

  return expandedTask
}

@@ -967,24 +967,79 @@
 EY%5D%0A      %7D%0A
+      // Ensure each dependencies property is an array%0A
       for (l
@@ -1155,17 +1155,25 @@
 isArray(
-t
+expandedT
 ask%5BDEPE
@@ -1210,17 +1210,25 @@
         
-t
+expandedT
 ask%5BDEPE
@@ -1254,17 +1254,25 @@
 rty%5D = %5B
-t
+expandedT
 ask%5BDEPE

fix bad logic