import { basename, resolve } from 'path'
import { isFunction, isString, isObject } from 'lodash/lang'

export default function collection(state) {
  const { collection } = state

  if (!collection) { return undefined }
  
  const args = collection.map(val => {
    if (val.length === 1 && isString(val[0])) {
      return {
        name: basename(val[0]),
        cwd: val[0],
      }
    }

    if (val.length === 1 && isObject(val[0])) {
      return val[0]
    }

    if (val.length === 1 && isString(val[0]) && isString(val[1])) {
      return {
        name: val[0],
        cwd: resolve(val[1], state.context || process.cwd()) ,
      }
    }

    if (val.length === 1 && isString(val[0]) && isObject(val[1])) {
      return {
        name: val[0],
        ...(val[1])
      }
    }

    return val
  })

  return args.filter(i => i.length > 0)
}
