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

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

const config = ref(null);

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

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

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

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

const replaceMergeArr = ref();

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

// dataset 无法变更条数的补丁
watch(
  () => props.chartConfig.option.dataset,
  (newData, oldData) => {
    try {
      if (!isObject(newData) || !('dimensions' in newData)) return;
      if (Array.isArray(newData?.dimensions)) {
        const seriesArr = [];
        for (let i = 0; i < newData.dimensions.length - 1; i++) {
          seriesArr.push(seriesItem);
        }
        replaceMergeArr.value = ['series'];
        props.chartConfig.option.series = seriesArr;
        nextTick(() => {
          replaceMergeArr.value = [];
        });
      }
    } catch (error) {
      console.log(error);
    }
  },
  {
    deep: false
  }
);

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