import { computed, Ref } from 'vue';
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d';
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';

// 获取当前对象数据
export const useTargetData = () => {
  const chartEditStore = useChartEditStore();
  const targetData: Ref<CreateComponentType | CreateComponentGroupType> = computed(() => {
    const list = chartEditStore.getComponentList;
    const targetIndex = chartEditStore.fetchTargetIndex();
    const selectId = chartEditStore.getTargetChart.selectId;
    let data;
    if (targetIndex != -1) {
      if (selectId[0] === list[targetIndex].id) {
        data = list[targetIndex];
      } else {
        list[targetIndex].groupList.forEach((element) => {
          if (element.id === selectId[0]) {
            data = element;
          }
        });
      }
    }
    return data;
  });
  return { targetData, chartEditStore };
};
