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

<script setup>
import { ref, computed, watch } from 'vue';
import VChart from 'vue-echarts';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { RadarChart } 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, RadarChart, GridComponent, TooltipComponent, LegendComponent]);

const vChartRef = ref();

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

const dataSetHandle = dataset => {
  if (dataset.seriesData) {
    props.chartConfig.option.series[0].data = dataset.seriesData;
    // @ts-ignore
    props.chartConfig.option.legend.data = dataset.seriesData.map(i => i.name);
  }
  if (dataset.radarIndicator) {
    props.chartConfig.option.radar.indicator = dataset.radarIndicator;
  }
  if (vChartRef.value && isPreview()) {
    vChartRef.value.setOption(props.chartConfig.option);
  }
};

watch(
  () => props.chartConfig.option.dataset,
  newData => {
    try {
      dataSetHandle(newData);
    } catch (error) {
      console.log(error);
    }
  },
  {
    deep: false
  }
);

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