var y = Object.defineProperty;
var w = (i, r, a) => r in i ? y(i, r, { enumerable: !0, configurable: !0, writable: !0, value: a }) : i[r] = a;
var p = (i, r, a) => w(i, typeof r != "symbol" ? r + "" : r, a);
import { ref as s, onMounted as g } from "vue";
import * as v from "echarts";
import { nanoid as C } from "nanoid";
const A = (i, r, a) => {
  const l = s(), c = s(!1);
  class f {
    constructor() {
      p(this, "_iframe");
      this._iframe = l.value;
    }
    fastEval(n) {
      const t = this._iframe.contentWindow.document.getElementsByTagName("head")[0], e = this._iframe.contentWindow.document.createElement("script");
      e.setAttribute("type", "text/javascript");
      const o = this._iframe.contentWindow.document.createElement("style");
      o.innerHTML = `
            *{
                margin:0;
                padding:0;
            }
            body, html {
                height:100%;
                overflow: hidden;
                background-color: transparent;
            }
            #renderDom {
                width:100%;
                height:100%;
                background-color: transparent;
                outline: none !important;
            }
        `, e.innerHTML = `
        try {
            ${n};
        } catch(e) {
            handleException(e);
        }`, t.innerHTML = "", t.appendChild(o), t.appendChild(e);
    }
    async loadScriptAsync(n) {
      return new Promise((t) => {
        const e = document.createElement("script");
        e.src = n, e.onload = () => {
          t();
        }, document.head.appendChild(e);
      });
    }
    async compileAndRunAsync(n) {
      try {
        if (n.includes("window.") || n.includes("parent.") || n.includes("top."))
          return;
        let t = "";
        c.value = !0;
        const e = this._iframe.contentWindow;
        if (e.myChart) {
          try {
            e.myChart.dispose();
          } catch {
          }
          e.myChart = null, e.option = null;
        }
        delete e.myChart, delete e.option, delete e.renderDom, delete e.initFunction;
        let o = e.document.getElementById("renderDom");
        if (o || (o = e.document.createElement("div"), o.id = "renderDom", e.document.body.appendChild(o)), e.echarts = v, !o || (e.renderDom = o, !n))
          return;
        t += `
                        window.initFunction = async function() {
                        window.myChart  = echarts.init(renderDom, null, {
                          renderer: 'svg'
                        });
                        window.option;
                    `, t += n, t += `

                            option && myChart.setOption(option);`, t += `

                        }`;
        try {
          this.fastEval(t);
        } catch {
        }
        try {
          await e.initFunction();
        } catch {
        }
        if (!e.option)
          return;
        c.value = !1;
      } catch {
        c.value = !1;
      }
    }
  }
  const m = (u) => {
    window.SHJDatasourceV2({
      tId: C(),
      sources: r,
      callback: ({ data: n }) => {
        try {
          window.SHJParseEvent(a, "dataListener", n);
          const t = `var echartsData = ${JSON.stringify(n[0].data[0].echartsData)};`;
          d.value.compileAndRunAsync(t + `
` + u);
        } catch {
          window.SHJParseEvent(a, "dataListener", null);
        }
      }
    });
  }, d = s();
  return g(() => {
    d.value = new f(), m(i.code);
  }), { iframeRef: l, loadingStatus: c, renderingComponent: d, initEcharts: m };
};
export {
  A as useEcharts
};
