import '../polyfills/is_array.js'

/** compute getColor func
 *
 * @param color -
 *  a string, everything gets the same color
 *  an Array, this will compute a d3 scaleOrdinal to get the color (e.g. for group based data)
 *  a mapping (object), the color for the given value will be returned
 *  a callable - will just return as is
**/
export default ({
  color,
  groupCol
}) => {

  if (typeof color === 'string') return () => color

  else if (Array.isArray(color)) {
    const _getColor = d3.scaleOrdinal(color)
    return d => _getColor(d[groupCol] || d)
  }

  else if (color.constructor === Object) {
    if (!groupCol) {
      throw new Error('need groupCol for this color func')
    }
    return d => color[d[groupCol]]
  }

  else if (typeof color === 'function') return color

  else {
    throw new Error('can\'t compute color function from '+color)
  }
}

@@ -367,17 +367,25 @@
 groupCol
+,%0A  xCol
 %0A
-
 %7D) =%3E %7B%0A
@@ -558,16 +558,38 @@
 pCol%5D %7C%7C
+ d%5BxCol%5D %7C%7C d.index %7C%7C
  d)%0A  %7D%0A
@@ -718,16 +718,16 @@
 )%0A    %7D%0A
-
     retu
@@ -751,16 +751,27 @@
 roupCol%5D
+ %7C%7C d%5BxCol%5D
 %5D%0A  %7D%0A%0A 

improve handling for color arrays