<template>
  <v-chart ref="vChartRef" :theme="themeColor" :option="option" :manual-update="isPreview()" autoresize></v-chart>
</template>

<script setup>
import { ref, computed, watch, nextTick } from 'vue';
import VChart from 'vue-echarts';
import { isObject, cloneDeep } from 'lodash-es';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
//引入柱状图 折线图
import { BarChart, LineChart } from 'echarts/charts';
import { mergeTheme } from '../../../chart';
import { useChartDataFetch } from '../../../hooks/useChartDataFetch.hook';
import { useChartEditStore } from '@/store/modules/chart/chartEditStore';
import { isPreview } from '../../../../../utils/chart-util';
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components';

const config = ref(null);

import('./config.js').then(module => {
  config.value = module;
});

// 计算属性
const { includes, barSeriesItem } = computed(() => config.value || { includes: [], barSeriesItem: {} });

const props = defineProps({
  themeSetting: {
    type: Object,
    required: true
  },
  themeColor: {
    type: Object,
    required: true
  },
  chartConfig: {
    type: Object,
    required: true
  }
});

use([DatasetComponent, CanvasRenderer, BarChart, LineChart, GridComponent, TooltipComponent, LegendComponent]);

const replaceMergeArr = ref();

const option = computed(() => {
  return mergeTheme(props.chartConfig.option, props.themeSetting, includes);
});

watch(
  () => props.chartConfig.option.dataset,
  (newData, oldData) => {
    try {
      if (!isObject(newData) || !('dimensions' in newData)) return;
      if (Array.isArray(newData?.dimensions)) {
        const seriesArr = [];
        // 对oldData进行判断，防止传入错误数据之后对旧维度判断产生干扰
        // 此处计算的是dimensions的Y轴维度，若是dimensions.length为0或1，则默认为1，排除X轴维度干扰
        const oldDimensions = Array.isArray(oldData?.dimensions) && oldData.dimensions.length >= 1 ? oldData.dimensions.length : 1;
        const newDimensions = newData.dimensions.length >= 1 ? newData.dimensions.length : 1;
        const dimensionsGap = newDimensions - oldDimensions;
        if (dimensionsGap < 0) {
          props.chartConfig.option.series.splice(newDimensions - 1);
        } else if (dimensionsGap > 0) {
          if (!oldData || !oldData?.dimensions || !Array.isArray(oldData?.dimensions) || !oldData?.dimensions.length) {
            props.chartConfig.option.series = [];
          }
          for (let i = 0; i < dimensionsGap; i++) {
            seriesArr.push(cloneDeep(barSeriesItem));
          }
          props.chartConfig.option.series.push(...seriesArr);
        }
        replaceMergeArr.value = ['series'];
        nextTick(() => {
          replaceMergeArr.value = [];
        });
      }
    } catch (error) {
      console.log(error);
    }
  },
  {
    deep: false
  }
);

const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore);
</script>
