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

<script setup>
import { computed, watch, ref } from 'vue';
import VChart from 'vue-echarts';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { PieChart } 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 } = computed(() => config.value || { includes: [] });

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

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

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

watch(
  () => props.chartConfig.option.type,
  newData => {
    try {
      if (newData === 'nomal') {
        props.chartConfig.option.series[0].radius = '70%';
        props.chartConfig.option.series[0].roseType = false;
      } else if (newData === 'ring') {
        props.chartConfig.option.series[0].radius = ['40%', '65%'];
        props.chartConfig.option.series[0].roseType = false;
      } else {
        props.chartConfig.option.series[0].radius = '70%';
        props.chartConfig.option.series[0].roseType = true;
      }
    } catch (error) {
      console.log(error);
    }
  },
  { deep: false, immediate: true }
);
const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore);
</script>
