import "../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import { customizeHttp } from "../api/http.mjs";
import { intervalUnitHandle, newFunctionHandle } from "../utils/utils.mjs";
import "../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.mjs";
import "../enums/pageEnum.mjs";
import "../router/index.mjs";
import "../store/modules/designStore/designStore.mjs";
import "../../node_modules/.pnpm/vue3-lazyload@0.2.5-beta_a6dezdz3je235ci4e3tu4rayna/node_modules/vue3-lazyload/dist/vue3-lazyload.esm.mjs";
import "../components/GoSkeleton/index.vue.mjs";
import "../components/GoLoading/index.mjs";
import "../settings/designSetting.mjs";
import { computed, watch } from "../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.mjs";
import { toRaw } from "../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.mjs";
const mittDataPondMap = /* @__PURE__ */ new Map();
const newPondItemInterval = (requestGlobalConfig, requestDataPondItem, dataPondMapItem) => {
  if (!dataPondMapItem)
    return;
  let fetchInterval = 0;
  clearInterval(fetchInterval);
  const fetchFn = async () => {
    try {
      const res = await customizeHttp(toRaw(requestDataPondItem.value.dataPondRequestConfig), toRaw(requestGlobalConfig));
      if (res) {
        try {
          dataPondMapItem.forEach((item) => {
            item.updateCallback(newFunctionHandle(res == null ? void 0 : res.data, res, item.filter));
          });
        } catch (error) {
          console.error(error);
          return error;
        }
      }
    } catch (error) {
      return error;
    }
  };
  watch(
    () => requestDataPondItem.value.dataPondRequestConfig.requestParams.Params,
    () => {
      fetchFn();
    },
    {
      immediate: false,
      deep: true
    }
  );
  fetchFn();
  const targetInterval = requestDataPondItem.value.dataPondRequestConfig.requestInterval;
  const targetUnit = requestDataPondItem.value.dataPondRequestConfig.requestIntervalUnit;
  const globalRequestInterval = requestGlobalConfig.requestInterval;
  const globalUnit = requestGlobalConfig.requestIntervalUnit;
  const time = targetInterval ? targetInterval : globalRequestInterval;
  const unit = targetInterval ? targetUnit : globalUnit;
  if (time)
    fetchInterval = setInterval(fetchFn, intervalUnitHandle(time, unit));
};
const useChartDataPondFetch = () => {
  const addGlobalDataInterface = (targetComponent, useChartEditStore2, updateCallback) => {
    const chartEditStore = useChartEditStore2();
    chartEditStore.getRequestGlobalConfig;
    const requestDataPondId = targetComponent.request.requestDataPondId;
    const mittPondIdArr = mittDataPondMap.get(requestDataPondId) || [];
    mittPondIdArr.push({
      updateCallback,
      filter: targetComponent.filter
    });
    mittDataPondMap.set(requestDataPondId, mittPondIdArr);
  };
  const clearMittDataPondMap = () => {
    mittDataPondMap.clear();
  };
  const initDataPond = (useChartEditStore2) => {
    const { requestGlobalConfig } = useChartEditStore2();
    const chartEditStore = useChartEditStore2();
    for (let pondKey of mittDataPondMap.keys()) {
      const requestDataPondItem = computed(() => {
        return requestGlobalConfig.requestDataPond.find((item) => item.dataPondId === pondKey);
      });
      if (requestDataPondItem) {
        newPondItemInterval(chartEditStore.requestGlobalConfig, requestDataPondItem, mittDataPondMap.get(pondKey));
      }
    }
  };
  return {
    addGlobalDataInterface,
    clearMittDataPondMap,
    initDataPond
  };
};
export {
  useChartDataPondFetch
};
