import { merge, isObject } from 'lodash-es';
import { keepDecimals } from '@/utils';

export function getLineSeries(option) {
  return merge(
    {
      type: 'line',
      itemStyle: {},
      lineStyle: {
        width: 2,
      },
      symbol: 'emptyCircle',
      showSymbol: false,
      showAllSymbol: false,
      emphasis: {
        disable: false,
        focus: 'series',
        itemStyle: {
          color: 'inherit',
        },
      },
      blur: {
        itemStyle: {
          opacity: 0.15,
        },
        lineStyle: {
          opacity: 0.15,
        },
      },
    },
    option
  );
}

export function getBarSeries(option) {
  return merge(
    {
      type: 'bar',
      barMaxWidth: 24,
      blur: {
        itemStyle: {
          opacity: 0.6,
        },
      },
      emphasis: {
        disable: false,
        focus: 'series',
        itemStyle: {
          color: 'inherit',
        },
      },
      label: {
        show: false,
        color: 'rgba(84, 88, 120, 1)',
        formatter(params) {
          const { value, encode, dimensionNames } = params;
          let val = value;
          if (Array.isArray(value)) {
            val = value[encode.y[0]];
          } else if (isObject(value)) {
            val = value[dimensionNames[encode.y[0]]];
          }
          return keepDecimals(val);
        },
      },
    },
    option
  );
}

export function getPieSeries(option) {
  return merge(
    {
      type: 'pie',
      center: ['50%', '50%'],
      radius: ['40%', '60%'],
      top: 0,
      bottom: 0,
      labelLine: {
        show: true,
      },
      colorBy: 'series',
      label: {
        show: true,
        position: 'inner',
        formatter(params) {
          const { name, value, percent } = params;
          const str = `${name}
        ${keepDecimals(percent)}%`;
          return percent > 5 ? str : '';
        },
        color: 'rgba(0, 0, 0, 0.65)',
        fontSize: 12,
        lineHeight: 14,
        fontWeight: 600,
      },
    },
    option
  );
}

export function getSeries(chartType, option) {
  if (chartType === 'line') {
    return getLineSeries(option);
  }
  if (chartType === 'bar') {
    return getBarSeries(option);
  }
  if (chartType === 'pie') {
    return getPieSeries(option);
  }
  return getLineSeries(option);
}
