import * as echarts from 'echarts';

export interface Params {
  title: string;
  titleColor: string;
  legend: boolean;
}

export function getOption(
  { title, titleColor, legend = false }: Params,
  fields: {
    name: string;
    color: string;
    data: number;
  }[]
): echarts.EChartsOption {
  return {
    tooltip: {
      trigger: 'item',
    },
    title: {
      text: title,
      left: 'left',
      top: '8%',
      textStyle: {
        fontSize: '18px',
        fontWeight: 'normal',
        color: titleColor,
      },
    },
    legend: legend
      ? {
          top: '44%',
          left: 'right',
          orient: 'vertical',
        }
      : {
          show: false,
        },
    color: fields.map(field => field.color),
    series: [
      {
        name: '',
        type: 'pie',
        radius: ['45%', '70%'],
        avoidLabelOverlap: false,
        center: legend ? ['30%', '60%'] : ['35%', '60%'],
        label: {
          show: false,
          position: 'center',
        },
        // emphasis: {
        //   label: {
        //     show: false,
        //     fontSize: 40,
        //     fontWeight: 'bold',
        //   },
        // },
        emphasis: {
          focus: 'series',
        },
        labelLine: {
          show: false,
        },
        data: fields.map(f => ({
          name: f.name,
          value: f.data,
        })),
      },
    ],
  };
}
