import { ref as m, shallowRef as L, onMounted as P, onBeforeUnmount as Z } from "vue";
import { useResizeObserver as I } from "@vueuse/core";
import { debounce as O, cloneDeep as S, isArray as n } from "lodash";
import b from "is-electron";
import * as f from "echarts";
import { nanoid as k } from "nanoid";
import { HoverLoop as V } from "../utils/hoverLoop.mjs";
import "echarts-wordcloud";
import "echarts-liquidfill";
const D = (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 d = e.map((r) => {
    if (r === "customize")
      if (n(l.geo)) {
        const c = JSON.parse(l.geo.find((h) => h.map === "customize").geoData);
        return f.registerMap(r, c);
      } else {
        const c = JSON.parse(l.geo.geoData);
        return f.registerMap(r, c);
      }
    else
      return fetch((b() ? "gismap/" : "/gismap/") + r + ".json").then((c) => c.json()).then((c) => {
        f.registerMap(r, c);
      }).catch((c) => {
      });
  });
  return Promise.all(d);
}, M = async (l) => {
  l.bmap && await import(
    /* @vite-ignore */
    "echarts/extension/bmap/bmap"
  );
}, A = ({ basicOption: l, useEvents: e, customRenderData: d, emit: r }) => {
  const c = k(), h = m(!0), t = L(null), v = d, u = m(null), i = m(null), y = (s, g) => {
    try {
      t.value || (t.value = f.init(document.getElementById(c), null, { renderer: g || "svg" }));
      const o = S(s);
      if (o) {
        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 J = o.dataZoom[0].endValue, H = o.dataZoom[0].startValue, w = () => {
              i.value = setInterval(() => {
                o.dataZoom[0].endValue > a - 2 ? (o.dataZoom[0].endValue = J, o.dataZoom[0].startValue = H) : (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), w();
            }), w();
          } 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 V(t.value, o);
          }), u.value = new V(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), window.SHJParseEvent(e, "chart-click", a));
        }), t.value.on("dblclick", (a) => {
          e && n(e) && e.length > 0 && (r("chart-dblclick", a), window.SHJParseEvent(e, "chart-dblclick", a));
        }), t.value.on("mousedown", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mousedown", a), window.SHJParseEvent(e, "chart-mousedown", a));
        }), t.value.on("mouseup", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mouseup", a), window.SHJParseEvent(e, "chart-mouseup", a));
        }), t.value.on("mouseover", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mouseover", a), window.SHJParseEvent(e, "chart-mouseover", a));
        }), t.value.on("mouseout", (a) => {
          e && n(e) && e.length > 0 && (r("chart-mouseout", a), window.SHJParseEvent(e, "chart-mouseout", a));
        }), t.value.on("contextmenu", (a) => {
          e && n(e) && e.length > 0 && (r("chart-contextmenu", a), window.SHJParseEvent(e, "chart-contextmenu", a));
        }), h.value = !1;
      }
    } catch {
      try {
        t.value.dispose();
      } catch {
      }
      t.value = null;
    }
  }, p = O((s) => {
    h.value = !0, s = S(s), D(s).then(() => {
      M(s).then(() => {
        v(s);
      });
    }).catch((g) => {
    });
  }, 100);
  return P(() => {
    p(l), I(document.getElementById(c), (s) => {
      t.value && t.value.resize();
    });
  }), Z(() => {
    u.value && u.value.closeSwitchTooltip(), i.value && clearInterval(i.value);
    try {
      t.value && t.value.dispose();
    } catch {
    }
  }), {
    id: c,
    status: h,
    chart: t,
    setOption: y,
    renderChart: p,
    renderData: v
  };
};
export {
  A as useChart
};
