"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.js");
const http = require("../api/http.js");
require("../store/modules/designStore/designStore.js");
require("../settings/designSetting.js");
const utils = require("../utils/utils.js");
require("../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.js");
const router = require("../utils/router.js");
require("../../node_modules/.pnpm/vue3-lazyload@0.2.5-beta_a6dezdz3je235ci4e3tu4rayna/node_modules/vue3-lazyload/dist/vue3-lazyload.esm.js");
require("../components/GoSkeleton/index.vue.js");
require("../components/GoLoading/index.js");
require("../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.js");
require("../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.js");
const index_d = require("../packages/index.d.js");
const httpEnum = require("../enums/httpEnum.js");
const chart = require("../packages/public/chart.js");
const useChartDataPondFetch_hook = require("./useChartDataPondFetch.hook.js");
require("../../node_modules/.pnpm/echarts@5.4.3/node_modules/echarts/index.js");
require("../store/modules/langStore/langStore.js");
const reactivity_esmBundler = require("../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js");
const runtimeCore_esmBundler = require("../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js");
const useChartDataFetch = (targetComponent, useChartEditStore2, updateCallback) => {
  const vChartRef = reactivity_esmBundler.ref(null);
  let fetchInterval = 0;
  const { addGlobalDataInterface } = useChartDataPondFetch_hook.useChartDataPondFetch();
  const { chartFrame } = targetComponent.chartConfig;
  const echartsUpdateHandle = (dataset) => {
    if (chartFrame === index_d.ChartFrameEnum.ECHARTS) {
      if (vChartRef.value) {
        chart.setOption(vChartRef.value, { dataset });
      }
    }
  };
  const requestIntervalFn = () => {
    const chartEditStore = useChartEditStore2();
    const {
      requestOriginUrl,
      requestIntervalUnit: globalUnit,
      requestInterval: globalRequestInterval
    } = reactivity_esmBundler.toRefs(chartEditStore.getRequestGlobalConfig);
    const {
      requestDataType,
      requestUrl,
      requestIntervalUnit: targetUnit,
      requestInterval: targetInterval
    } = reactivity_esmBundler.toRefs(targetComponent.request);
    if (requestDataType.value !== httpEnum.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 http.customizeHttp(reactivity_esmBundler.toRaw(targetComponent.request), reactivity_esmBundler.toRaw(chartEditStore.getRequestGlobalConfig));
          if (res) {
            try {
              const filter = targetComponent.filter;
              const { data } = res;
              echartsUpdateHandle(utils.newFunctionHandle(data, res, filter));
              if (updateCallback) {
                updateCallback(utils.newFunctionHandle(data, res, filter));
              }
            } catch (error) {
              console.error(error);
            }
          }
        };
        runtimeCore_esmBundler.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, utils.intervalUnitHandle(time, unit));
        } else {
          fetchFn();
        }
      }
    } catch (error) {
      console.log(error);
    }
  };
  if (router.isPreview()) {
    targetComponent.request.requestDataType === httpEnum.RequestDataTypeEnum.Pond ? addGlobalDataInterface(targetComponent, useChartEditStore2, updateCallback || echartsUpdateHandle) : requestIntervalFn();
  } else {
    requestIntervalFn();
  }
  return { vChartRef };
};
exports.useChartDataFetch = useChartDataFetch;
