/**
 * @author xiaoping
 * @email edwardhjp@gmail.com
 * @create date 2017-05-17 11:12:57
 * @modify date 2017-05-19 09:55:33
 * @desc [矩形树图options]
 */

import {
  setInitOpts, setUndef, setLabel, setItem
} from '@/utils/options'

// 设置图表系列
const setSeries = (ret, options) => {
  let item = options.series || {}
  ret.series[0] = Object.assign({}, item, {
    type: item.type || 'treemap',
    silent: setUndef(item.silent, true),
    roam: item.roam || false,
    nodeClick: item.nodeClick || false,
    breadcrumb: item.breadcrumbShow || false,
  })
  ret.series[0].label = setLabel(item)
  ret.series[0].itemStyle = setItem(item)
}

// 设置图表数据
const setData = (ret, options) => {
  ret.series[0].data = [] // 初始化data
  if (options.datas) {
    let datas = {}
    if (Array.isArray(options.datas)) { // 如果是array就是series的data
      datas.series = options.datas
    } else {
      datas = options.datas
    }
    if (datas.series) {
      let data = datas.series || []
      let series = ret.series[0]
      series.data = data.map((item, index) => {
        let obj = {
          name: item.name,
          value: item.value,
        }
        if (item.color) {
          options.color && options.color.splice(index, 1, item.color)
        }
        return obj
      })
    }
  }
}

const setOptions = (options) => {
  let ret = setInitOpts(options)
  setSeries(ret, options)
  setData(ret, options)
  return options.custom ? options : ret
}

export {
  setOptions,
}
