import {
  SANKEY,
  WORDCLOUD,
  LINERADAR,
  AREARADAR,
  DIAGRAM,
  WAVE_INSTRUMENT,
  POINTERINSTRUMENT,
  SCATTER,
  AIRSCATTER,
  LINEBAR,
  GROUPLINE,
  GROUPAREA,
  GROUPBAR,
  TRANGROUPBAR,
  RINGPIE,
  PIE,
} from "@bdlite/chart-config/lib/chartTypes";
import {
  TransSankeySeries,
  TransWordCloudsSeries,
  TransRadarData,
  TransDiagramData,
  TransWaveInstrumentSeries,
  TransLineAndBarOptions,
  TransBaseChartOpt,
  TransLineBarOpt,
  ComputedGroupBarWidth,
  TransPieConfig,
} from "@/utils/comDataTrans.js";
import comConfig from "@/constants/config/comConfig.js";
import { cloneDeep } from "lodash-es";
export default (chartType, iconConfig) => {
  const {
    options: { radar, dataset, series },
  } = iconConfig;
  const { indicator } = radar || {};
  const { source } = dataset || [{}];
  switch (chartType) {
    case LINERADAR:
    case AREARADAR:
      return [{ indicator, data: series[0].data }];
    default:
      break;
  }
  if (chartType == LINERADAR || chartType === AREARADAR) {
    // 雷达图不支持dataset
    return [{ indicator, data: series[0].data }];
  } else if (chartType == DIAGRAM) {
    // 基础关系图不同
    const { nodes, links } = source[0];
    return [{ nodes, links }];
  } else if (chartType == POINTERINSTRUMENT) {
    // 基础关系图不同
    return [...series[0].data];
  } else {
    return (
      (iconConfig.options.dataset && iconConfig.options.dataset.source) ||
      iconConfig.options.dataset
    );
  }
};
const transStrToJson = (originData, defaultData) => {
  if (originData) {
    if (typeof originData === "string") {
      return JSON.parse(originData);
    } else {
      return originData;
    }
  }
  return defaultData;
};
export const transformData = (chartData) => {
  const { iconConfig, dataConfig, eventConfig, dataResult, chartType } =
    chartData;
  const chartConfig = {
    ...chartData,
    iconConfig: transStrToJson(iconConfig, cloneDeep(comConfig[chartType])),
    dataConfig: transStrToJson(dataConfig),
    eventConfig: transStrToJson(eventConfig, { linkage: [] }),
    dataResult: dataResult,
  };
  return chartConfig;
};

export const transformDataToOpt = (component, chartType, chartInstance) => {
  let options = cloneDeep(component.iconConfig.options);
  if (
    [
      GROUPLINE,
      GROUPAREA,
      GROUPBAR,
      TRANGROUPBAR,
      SCATTER,
      AIRSCATTER,
    ].includes(chartType)
  ) {
    options.series = TransBaseChartOpt(component);
    options.dataset = component.dataResult || cloneDeep(options.dataset);
    if ([GROUPBAR, TRANGROUPBAR].includes(chartType)) {
      options.series = ComputedGroupBarWidth(options.series, options.dataset);
    }
  } else if (chartType === SANKEY) {
    // sankey不支持dataset，需单独处理数据
    const { series, dataset } = TransSankeySeries(component);
    options.series = series;
    options.dataset = dataset;
  } else if (chartType === WORDCLOUD) {
    options.series = TransWordCloudsSeries(component);
  } else if ([LINEBAR].includes(chartType)) {
    const { series, dataset } = TransLineBarOpt(component);
    options.series = series;
    options.dataset = dataset || [];
  } else if ([LINERADAR, AREARADAR].includes(chartType)) {
    const { radar, series, legend } = TransRadarData(component, chartInstance);
    options.radar = radar;
    options.series = series;
    options.legend = legend;
  } else if (chartType === DIAGRAM) {
    // 横向柱状图层级格式和其他有区别
    const { series } = TransDiagramData(component);
    options.series = series;
  } else if ([WAVE_INSTRUMENT, POINTERINSTRUMENT].includes(chartType)) {
    options.series = TransWaveInstrumentSeries(component);
  } else if ([PIE, RINGPIE].includes(chartType)) {
    const { dataResult, series } = TransPieConfig(component, chartInstance);
    options.series = series;
    options.dataset = dataResult || cloneDeep(options.dataset);
  } else {
    options.dataset = component.dataResult || cloneDeep(options.dataset);
  }
  return options;
};
export const transformXAxisByChartTyps = (options, chartType, instance) => {
  const { dataset } = options;
  if (
    [
      GROUPLINE,
      GROUPAREA,
      GROUPBAR,
      SCATTER,
      AIRSCATTER,
      LINEBAR,
      TRANGROUPBAR,
    ].includes(chartType)
  ) {
    const nextDataset = Array.isArray(dataset) ? dataset[0] : dataset;
    const { xAxis, grid } = TransLineAndBarOptions(
      { xAxis: options.xAxis, grid: options.grid, dataset: nextDataset },
      instance
    );
    options.xAxis = xAxis;
    options.grid = grid;
  }
  return options;
};
