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