

export function getXAxis(data: any) {
  const xAxis: any = {
    type: data.type || 'category',
    name: data.name || '',
    data: data.data || [],
    show: data.showFlag !== false,
    axisLabel: {
      show: data.showFlag !== false,
      showMaxLabel: true,
      interval: 'auto',
      rotate: data.rotate || 0,
      formatter: data.axisLabel || '{value}',
    },
    gridIndex: data.gridIndex || 0,
  }
  if (data.max !== null && data.max !== undefined) xAxis.max = data.max
  if (data.min !== null && data.min !== undefined) xAxis.min = data.min
  if (data.maxInterval !== undefined) xAxis.maxInterval = data.maxInterval
  if (data.minInterval !== undefined) xAxis.minInterval = data.minInterval
  return xAxis
}

export function getYAxis(data: any) {
  const yAxis: any = {
    type: data.type || 'value',
    name: data.name || '',
    nameLocation: data.nameLocation || 'end',
    scale: true,
    show: data.showFlag !== false,
    axisLabel: {
      show: data.showFlag !== false,
      formatter: data.axisLabel || '{value}',
    },
    axisLine: {
      show: true,
      lineStyle: {
        color: data.color || '#000',
      },
    },
    splitLine: {
      show: false
    },
    position: data.position || 'left',
    gridIndex: data.gridIndex || 0,
    offset: data.offset || 0,
  }
  if (data.max !== null && data.max !== undefined) yAxis.max = data.max
  if (data.min !== null && data.min !== undefined) yAxis.min = data.min
  if (data.maxInterval !== undefined) yAxis.maxInterval = data.maxInterval
  if (data.minInterval !== undefined) yAxis.minInterval = data.minInterval
  if (data.splitNumber !== undefined) yAxis.splitNumber = data.splitNumber
  return yAxis
}

export function getLineSeries(data: any) {
  const series = {
    type: 'line',
    name: data.name || '',
    data: data.data || [],
    yAxisIndex: data.yAxisIndex || 0,
    xAxisIndex: data.xAxisIndex || 0,
    connectNulls: data.connectNulls || false,
    symbol: data.symbol || 'none',
    symbolSize: data.symbolSize || 0,
    lineStyle:
      data.color || data.lineType
        ? {
            color: data.color,
            type: data.lineType,
          }
        : undefined,
    markPoint: data.markPoint,
    areaStyle: data.areaStyle,
  }
  return series
}

export function getBarSeries (data: any) {
  const serise = {
    type: 'bar',
    name: data.name || '',
    data: data.data || [],
    emphasis: {
      focus: 'series'
    },
    label: {
      show: data.labelFlag !== false,
      position: data.position || 'top'
    },
    yAxisIndex: data.yAxisIndex || 0,
    xAxisIndex: data.xAxisIndex || 0,
    stack: data.stack || '',
    showEmptyData: false
  }
  return serise
}


export function getBaseOption(data: any) {
  data.series.map((ele: any) => {
    if (ele.type !== 'line' || !ele?.data) return
    if (ele.data.length === 1) {
      ele.symbolSize = 4
      ele.symbol = 'circle'
    } else if (data.xAxis?.[0]?.type === 'category') {
      ele.data.forEach((res: any, index: number) => {
        if (typeof res !== 'object' || res === null || !('value' in res) || res.value !== null)
          return
        const prevPoint = ele.data?.[index - 1]
        if (
          index > 0 &&
          prevPoint &&
          typeof prevPoint === 'object' &&
          'value' in prevPoint &&
          prevPoint.value !== null
        ) {
          prevPoint.symbolSize = 4
          prevPoint.symbol = 'circle'
        }
        const nextPoint = ele.data?.[index + 1]
        if (
          ele.data &&
          index < ele.data.length - 1 &&
          nextPoint &&
          typeof nextPoint === 'object' &&
          'value' in nextPoint &&
          nextPoint.value !== null
        ) {
          nextPoint.symbolSize = 4
          nextPoint.symbol = 'circle'
        }
      })
    }
  })
  const option: any = {
    series: data.series,
    yAxis: Array.isArray(data.yAxis) ? data.yAxis : data.yAxis ? [data.yAxis] : undefined,
    xAxis: Array.isArray(data.xAxis) ? data.xAxis : data.xAxis ? [data.xAxis] : undefined,
  }
  if (data.tooltip) {
    option.tooltip = data.tooltip
  }
  if (data.tooltipFormatter) {
    option.tooltip = {
      ...option.tooltip,
      trigger: 'axis',
      formatter: data.tooltipFormatter,
    }
  }
  if (data.visualMap) option.visualMap = data.visualMap
  if (data.toolbox) option.toolbox = data.toolbox
  if (data.grid) option.grid = data.grid
  if (data.legend) option.legend = data.legend
  if (data.color) option.color = data.color
  if (data.dataZoom) option.dataZoom = data.dataZoom
  if (data.title) {
    option.title = Array.isArray(data.title)
      ? data.title.map(convertTitle)
      : [convertTitle(data.title)]
  }
  return option
}

function convertTitle(title: any) {
  return {
    text: title.text,
    subtext: title.subtext,
    show: title.show ?? true,
    backgroundColor: title.backgroundColor || '#5FCFCB',
    textStyle: {
      color: title.textStyle?.color || '#fff',
      fontSize: title.textStyle?.fontSize || '18',
      ...title.textStyle,
    },
    left: title.left || 'center',
    top: title.top || 0,
  }
}