import "../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import { customizeHttp } from "../api/http.mjs";
import "../store/modules/designStore/designStore.mjs";
import "../settings/designSetting.mjs";
import { newFunctionHandle, intervalUnitHandle } from "../utils/utils.mjs";
import "../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.mjs";
import { isPreview } from "../utils/router.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 "../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.mjs";
import "../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.mjs";
import { ChartFrameEnum } from "../packages/index.d.mjs";
import { RequestDataTypeEnum } from "../enums/httpEnum.mjs";
import { setOption } from "../packages/public/chart.mjs";
import { useChartDataPondFetch } from "./useChartDataPondFetch.hook.mjs";
import "../../node_modules/.pnpm/echarts@5.4.3/node_modules/echarts/index.mjs";
import "../store/modules/langStore/langStore.mjs";
import { ref, toRefs, toRaw } from "../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.mjs";
import { watch } from "../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.mjs";
const useChartDataFetch = (targetComponent, useChartEditStore2, updateCallback) => {
  const vChartRef = ref(null);
  let fetchInterval = 0;
  const { addGlobalDataInterface } = useChartDataPondFetch();
  const { chartFrame } = targetComponent.chartConfig;
  const echartsUpdateHandle = (dataset) => {
    if (chartFrame === ChartFrameEnum.ECHARTS) {
      if (vChartRef.value) {
        setOption(vChartRef.value, { dataset });
      }
    }
  };
  const requestIntervalFn = () => {
    const chartEditStore = useChartEditStore2();
    const {
      requestOriginUrl,
      requestIntervalUnit: globalUnit,
      requestInterval: globalRequestInterval
    } = toRefs(chartEditStore.getRequestGlobalConfig);
    const {
      requestDataType,
      requestUrl,
      requestIntervalUnit: targetUnit,
      requestInterval: targetInterval
    } = toRefs(targetComponent.request);
    if (requestDataType.value !== RequestDataTypeEnum.AJAX)
      return;
    try {
      if (requestUrl == null ? void 0 : requestUrl.value) {
        const completePath = requestOriginUrl && requestOriginUrl.value + requestUrl.value;
        if (!completePath)
          return;
        clearInterval(fetchInterval);
        const fetchFn = async () => {
          const res = await customizeHttp(toRaw(targetComponent.request), toRaw(chartEditStore.getRequestGlobalConfig));
          if (res) {
            try {
              const filter = targetComponent.filter;
              const { data } = res;
              echartsUpdateHandle(newFunctionHandle(data, res, filter));
              if (updateCallback) {
                updateCallback(newFunctionHandle(data, res, filter));
              }
            } catch (error) {
              console.error(error);
            }
          }
        };
        watch(
          () => targetComponent.request.requestParams,
          () => {
            fetchFn();
          },
          {
            immediate: true,
            deep: true
          }
        );
        const time = targetInterval && targetInterval.value ? targetInterval.value : globalRequestInterval.value;
        const unit = targetInterval && targetInterval.value ? targetUnit.value : globalUnit.value;
        if (time) {
          fetchInterval = setInterval(fetchFn, intervalUnitHandle(time, unit));
        } else {
          fetchFn();
        }
      }
    } catch (error) {
      console.log(error);
    }
  };
  if (isPreview()) {
    targetComponent.request.requestDataType === RequestDataTypeEnum.Pond ? addGlobalDataInterface(targetComponent, useChartEditStore2, updateCallback || echartsUpdateHandle) : requestIntervalFn();
  } else {
    requestIntervalFn();
  }
  return { vChartRef };
};
export {
  useChartDataFetch
};
