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

<script setup>
import { computed, ref } from 'vue';
import VChart from 'vue-echarts';
import ecStat from 'echarts-stat';
import { use, registerTransform } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { ScatterChart, LineChart } from 'echarts/charts';
import { UniversalTransition, LabelLayout } from 'echarts/features';
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,
  LegendComponent,
  TooltipComponent,
  TransformComponent,
  VisualMapComponent
} 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,
  ScatterChart,
  LineChart,
  GridComponent,
  LegendComponent,
  TooltipComponent,
  TransformComponent,
  VisualMapComponent,
  UniversalTransition,
  LabelLayout
]);

registerTransform(ecStat.transform.regression);

const replaceMergeArr = ref();

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

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