/** §6.16 ECharts 品牌色与默认 grid */
export const CHART_COLORS = ['#0f7a62', '#71717a', '#4da08c', '#76b8a8', '#a0d0c4']

const AXIS_LABEL = { color: '#71717a', fontSize: 12 }
const AXIS_LINE = { lineStyle: { color: '#e4e4e7' } }
const SPLIT_LINE = { lineStyle: { color: '#e4e4e7' } }

export function buildLineChartOption({ labels, series, legend = true, yAxisFormatter }) {
  return {
    color: CHART_COLORS,
    grid: { left: 40, right: 16, top: legend ? 36 : 16, bottom: 24 },
    legend: legend
      ? { top: 0, textStyle: { fontSize: 12, color: '#71717a' } }
      : undefined,
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: labels,
      axisLabel: AXIS_LABEL,
      axisLine: AXIS_LINE,
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        ...AXIS_LABEL,
        formatter: yAxisFormatter,
      },
      axisLine: AXIS_LINE,
      splitLine: SPLIT_LINE,
    },
    series: series.map((item, index) => ({
      name: item.name,
      type: 'line',
      smooth: true,
      data: item.data,
      color: CHART_COLORS[index % CHART_COLORS.length],
      lineStyle: { width: 2 },
      showSymbol: false,
    })),
  }
}
