/*
 * @Author: nxy
 * @Date: 2024-08-07 13:47:07
 */
import { useChartEditStore } from '@/store/modules/chart/chartEditStore';

// store 相关
export const useStore = localStorageInfo => {
  const chartEditStore = useChartEditStore();
  chartEditStore.requestGlobalConfig = localStorageInfo['requestGlobalConfig'];
};

/**
 * 数据池接口处理
 */
export const useChartDataPondFetch = () => {
  const mittDataPondMap = new Map();
  // 新增全局接口
  const addGlobalDataInterface = (targetComponent, useChartEditStore, updateCallback) => {
    const chartEditStore = useChartEditStore();
    const { requestDataPond } = chartEditStore.getRequestGlobalConfig;

    // 组件对应的数据池 Id
    const requestDataPondId = '111' || targetComponent.request.requestDataPondId;
    // 新增数据项
    const mittPondIdArr = mittDataPondMap.get(requestDataPondId) || [];
    mittPondIdArr.push({
      updateCallback: updateCallback,
      filter: targetComponent.filter
    });
    mittDataPondMap.set(requestDataPondId, mittPondIdArr);
  };

  // 初始化数据池
  const initDataPond = requestGlobalConfig => {
    const { requestDataPond } = requestGlobalConfig;
    // 根据 mapId 查找对应的数据池配置
    for (let pondKey of mittDataPondMap.keys()) {
      const requestDataPondItem = requestDataPond.find(item => item.dataPondId === pondKey);
      if (requestDataPondItem) {
        newPondItemInterval(requestGlobalConfig, requestDataPondItem, mittDataPondMap.get(pondKey));
      }
    }
  };

  return {
    addGlobalDataInterface,
    initDataPond
  };
};
