import { ref as g, shallowRef as O, onMounted as b, onBeforeUnmount as k } from "vue";
import { useResizeObserver as D } from "@vueuse/core";
import { debounce as M, cloneDeep as L, isArray as n } from "lodash";
import T from "is-electron";
import * as h from "echarts";
import { nanoid as x } from "nanoid";
import "echarts-wordcloud";
import "echarts-liquidfill";
import { SHJParseEvent as m } from "../../../../commons/plugins/event/index.mjs";
import { HoverLoop as Z } from "../utils/hoverLoop.mjs";
const z = (l) => {
  const e = [];
  l.geo && (n(l.geo) ? e.push(...l.geo.map((r) => r.map)) : e.push(l.geo.map)), l.series && n(l.series) && l.series.forEach((r) => {
    r.map && e.push(r.map);
  });
  const p = e.map((r) => {
    if (r === "customize")
      if (n(l.geo)) {
        const c = JSON.parse(l.geo.find((f) => f.map === "customize").geoData);
        return h.registerMap(r, c);
      } else {
        const c = JSON.parse(l.geo.geoData);
        return h.registerMap(r, c);
      }
    else
      return fetch((T() ? "gismap/" : "/gismap/") + r + ".json").then((c) => c.json()).then((c) => {
        h.registerMap(r, c);
      }).catch((c) => {
      });
  });
  return Promise.all(p);
}, B = async (l) => {
  l.bmap && await import(
    /* @vite-ignore */
    "echarts/extension/bmap/bmap"
  );
}, U = ({ basicOption: l, useEvents: e, customRenderData: p, emits: r }) => {
  const c = x(), f = g(!0), t = O(null), w = p, u = g(null), i = g(null), I = (s, S) => {
    try {
      t.value || (t.value = h.init(document.getElementById(c), null, { renderer: S || "svg" }));
      const o = L(s);
      if (o) {
        if (o.tooltip && o.tooltip.isFormatter) {
          let a = `
                    ${o.tooltip.formatterCode}
        
                    // 在这里调用 filter 函数，并传递 data 参数
                    return custom(params);
                `;
          a = a.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""), o.tooltip.formatter = (v) => {
            try {
              return new Function("params", a)(v);
            } catch {
              return "";
            }
          };
        }
        if (t.value.setOption(o, !0), o.hoverLoop)
          if (i.value && clearInterval(i.value), u.value && u.value.closeSwitchTooltip(), o.hoverLoop.type === "方式二") {
            let a = 0;
            o.dataset && o.dataset.source && (a = o.dataset.source.length), o.series[0] && o.series[0].data && (a = o.series[0].data.length);
            const v = o.dataZoom[0].endValue, d = o.dataZoom[0].startValue, V = () => {
              i.value = setInterval(() => {
                o.dataZoom[0].endValue > a - 2 ? (o.dataZoom[0].endValue = v, o.dataZoom[0].startValue = d) : (o.dataZoom[0].endValue = o.dataZoom[0].endValue + 1, o.dataZoom[0].startValue = o.dataZoom[0].startValue + 1), t.value.setOption(o);
              }, o.hoverLoop.delay2);
            };
            t.value.on("mousemove", function() {
              i.value && clearInterval(i.value);
            }), t.value.on("mouseout", function() {
              i.value && clearInterval(i.value), V();
            }), V();
          } else o.hoverLoop.type === "方式一" && (t.value.on("mousemove", function() {
            u.value && u.value.closeSwitchTooltip();
          }), t.value.on("mouseout", function() {
            u.value && u.value.closeSwitchTooltip(), u.value = new Z(t.value, o);
          }), u.value = new Z(t.value, o));
        else
          i.value && clearInterval(i.value), u.value && u.value.closeSwitchTooltip();
        t.value.on("click", (a) => {
          e && n(e) && e.length > 0 && (r("chart-click", a), m.parseEvents(e, "chart-click", a));
        }), t.value.on("dblclick", (a) => {
          e && n(e) && e.length > 0 && (r("chart-dblclick", a), m.parseEvents(e, "chart-dblclick", a));
        }), t.value.on("mousedown", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mousedown", a), m.parseEvents(e, "chart-mousedown", a));
        }), t.value.on("mouseup", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mouseup", a), m.parseEvents(e, "chart-mouseup", a));
        }), t.value.on("mouseover", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mouseover", a), m.parseEvents(e, "chart-mouseover", a));
        }), t.value.on("mouseout", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mouseout", a), m.parseEvents(e, "chart-mouseout", a));
        }), t.value.on("contextmenu", (a) => {
          e && n(e) && e.length > 0 && (r("chart-contextmenu", a), m.parseEvents(e, "chart-contextmenu", a));
        }), f.value = !1;
      }
    } catch {
      try {
        t.value.dispose();
      } catch {
      }
      t.value = null;
    }
  }, y = M((s) => {
    f.value = !0, s = L(s), z(s).then(() => {
      B(s).then(() => {
        w(s);
      });
    }).catch((S) => {
    });
  }, 100);
  return b(() => {
    y(l), D(document.getElementById(c), (s) => {
      t.value && t.value.resize();
    });
  }), k(() => {
    u.value && u.value.closeSwitchTooltip(), i.value && clearInterval(i.value);
    try {
      t.value && t.value.dispose();
    } catch {
    }
  }), {
    id: c,
    status: f,
    chart: t,
    setOption: I,
    renderChart: y,
    renderData: w
  };
};
export {
  U as useChart
};
