export const legend = (option) => {
  let config = {
    show: option.legendShow,
    orient: option.orient,
    top: option.legendTop,
    type: option.legendType,
    textStyle: {
      fontSize: option.legendTextStyle.fontSize,
      color: option.legendFontColor
    }
  }
  if (option.legendAlign == 'left') {
    config.left = option.legendLeft
  }
  if (option.legendAlign == 'right') {
    config.right = option.legendRight
  }
  if (option.legendAlign == 'center') {
    config.left = 'center'
  }
  return config
}

export const title = (option) => {
  let config = {
    show: option.isTitleShow || false,
    text: option.titleText,
    top: option.titleTop,
    textStyle: {
      color: option.textStyle.fontColor,
      fontSize: option.textStyle.fontSize,
      fontWeight: option.textStyle.fontWeight,
    }
  }
  if (option.titleAlign == 'left') {
    config.left = option.titleLeft
  }
  if (option.titleAlign == 'right') {
    config.right = option.titleRight
  }
  if (option.titleAlign == 'center') {
    config.left = 'center'
  }
  return config
}

export const grid = (option) => {
  return {
    containLabel: true,
    left: option.gridLeft + '%',
    top: option.gridTop + '%',
    right: option.gridRight + '%',
    bottom: option.gridBottom + '%'
  }
}
export const countLabel = (option) => {
  return {
    show: option.labelSwitch,
    color: option.labelColor,
    fontSize: option.labelSize,
    position: option.labelPosition,
    textBorderWidth: option.labelBorderWidth,
    textBorderColor: option.labelBorderColor,
    rotate: option.labelRotate,
    valueAnimation: true,
    formatter(params) {
      let item = option.colorList[params.seriesIndex]
      let suffix = item ? (item.percent ? '%' : '') : ''
      if (option.labelThousandPoint) {
        let num = params.value
        let result = []
        let counter = 0
        num = parseFloat(num)
        if (!num) return 0
        let flag = num > 0
        num = Math.abs(num)
        num = '' + num
        let point = num.split('.')[1]
        num = num.split('.')[0].split('')
        for (let i = num.length - 1; i >= 0; i--) {
          counter++;
          result.unshift(num[i]);
          if (!(counter % 3) && i != 0) {
            result.unshift(',');
          }
        }
        if (result[0] == '-') {
          result.shift()
        }
        if (!flag) {
          result.unshift('-')
        }
        if (point && point.length) {
          result.push('.' + point)
        }
        result = result.join('');
        return result + suffix;
      } else {
        return params.value + suffix
      }
    }
  }
}
export const countLine = option => {
  return {
    width: option.lineWidth || 1,
    type: option.lineType || 'solid',
  }
}

export const countLegendSelect = (option, chartOption) => {
  let colorList = option.colorList
  let legendData = chartOption.legend.data
  chartOption.legend.selected = legendData.reduce((r, item, index) => {
    let setting = colorList[index]
    if (setting) {
      r[item] = !setting.unselect
    }
    return r
  }, {})
}

export const countXlabel = option => {
  return {
    fontSize: option.xlabelFontSize,
    color: option.xlabelColor,
    interval: option.xlabelSwitchAll ? "0" : "auto",
    rotate: option.xlabelRotate,
  };
}
