/* @flow */
import objectReduce from 'fast-loops/lib/objectReduce'
import { combineRules } from 'fela'

function safeRule(ruleOrObject: Function | Object): Function {
  return typeof ruleOrObject === 'function' ? ruleOrObject : () => ruleOrObject
}

export default function combineMultiRules(
  ...multiRules: Array<Function | Object>
): Function {
  return (props, renderer) => {
    return objectReduce(
      multiRules,
      (resultStyleMap, multiRule) => ({
        ...resultStyleMap,
        ...objectReduce(
          safeRule(multiRule)(props, renderer),
          (styleMap, rule, name) => ({
            ...styleMap,
            [name]: resultStyleMap[name]
              ? combineRules(resultStyleMap[name], safeRule(rule))
              : safeRule(rule),
          }),
          {}
        ),
      }),
      {}
    )
  }
}

@@ -60,16 +60,69 @@
 Reduce'%0A
+import arrayReduce from 'fast-loops/lib/arrayReduce'%0A
 import %7B
@@ -438,22 +438,21 @@
  return 
-object
+array
 Reduce(%0A

use arrayReduce to ensure only valid arrays are used