/* 漏斗图，其逻辑跟饼图一致 */
import Pie from './pie'

class Funnel extends Pie {
  constructor(layer, screen) {
    super(layer, screen)
  }

  reduceData(paint) {
    if (!paint.series || !this.originalData || !this.originalData.length) {
      this.data = []
      return
    }
    this.colors = paint.colors || []
    let result = []
    let series = paint.series
    series.forEach(s => {
      let t = []
      let colorIndex = 0
      if (
        (s.nameField && s.valueField) ||
        (this.originalData[0].name && this.originalData[0].value)
      ) {
        let nameField = s.nameField || 'name'
        let valueField = s.valueField || 'value'
        this.originalData.forEach(d => {
          t.push({
            name: d[nameField] || '',
            value: d[valueField] || 0
          })
        })
      } else {
        for (let key in this.originalData[0]) {
          if (typeof +this.originalData[0][key] === 'number') {
            t.push({
              name: key,
              value: this.originalData[0][key]
            })
          }
        }
      }
      t = t.sort((a, b) => s.inverse ? (a.value - b.value) : (b.value - a.value))
      t.forEach((item, index) => {
        item.itemStyle = {
          color: this.colors[index]
        }
      })
      result.push(t)
    })
    this.data = result
  }
}

export default Funnel
