import { defineComponent as R, ref as g, onMounted as K, onBeforeUnmount as Q, computed as X, watch as b, openBlock as d, createElementBlock as u, normalizeClass as H, normalizeStyle as f, createCommentVNode as S, Fragment as W, renderList as M, createElementVNode as B, toDisplayString as k, unref as Y } from "vue";
import { cloneDeep as v, isEqual as h } from "lodash";
import z from "gsap";
import { useFps as Z } from "@vueuse/core";
import { World as x } from "./map3d/map.mjs";
import { initWatch as ee } from "./watch.mjs";
import { autoInstallFont as L, jsonToCssStyle as E } from "../../utils/json2css.mjs";
import "three";
import "d3-geo";
import "three/examples/jsm/controls/OrbitControls";
import "three/examples/jsm/loaders/GLTFLoader";
import "three/examples/jsm/loaders/DRACOLoader.js";
import { createHistory as te } from "../../utils/mini3d/utils/CreateHistory.mjs";
import "three/examples/jsm/libs/lil-gui.module.min";
import "three/examples/jsm/utils/BufferGeometryUtils";
import "three/examples/jsm/renderers/CSS3DRenderer";
const oe = { class: "value" }, ae = ["onClick"], le = {
  key: 3,
  class: "fps"
}, ne = R({ name: "zv-scene-map3d" }), ke = /* @__PURE__ */ R({
  ...ne,
  props: {
    basicOption: {},
    sources: {},
    useEvents: {},
    uuid: {}
  },
  emits: [
    "on-loaded",
    "on-animated",
    "on-drill-down",
    "on-area-mouseover",
    "on-area-mouseout",
    "on-area-click",
    "on-bar-click",
    "on-scatter-click",
    "on-flyline-click",
    "on-return",
    "on-data-change"
  ],
  setup(T, { expose: $, emit: V }) {
    const i = V, a = T, l = g(), O = g(!1), C = g(!1), w = "zerov-map3d-scene", I = "zerov-map3d-scene-wrap", r = new te(), p = g(!1), y = (e = !0) => {
      l.value.cleanAllBar(), l.value.cleanAllScatter(), l.value.cleanAllFlyLine(), l.value.createAllRegionalLevel(), a.sources && a.sources.length > 0 && window.SHJDatasourceV2({
        tId: a.uuid,
        sources: a.sources,
        callback: (o) => {
          try {
            const t = a.basicOption.widgets && a.basicOption.widgets.find((s) => s._sourceId === o.id);
            t && t.type === "bar" && l.value.createBar(t, o.data[0].data, !0, e), t && t.type === "scatter" && l.value.createScatter(t, o.data[0].data, !0, e), t && t.type === "flyline" && l.value.createFlyLine(t, o.data[0].data, !0, e), t && t.type === "regionalLevel" && l.value.createRegionalLevel(t, o.data[0].data, !0, e), e && (l.value.createBarTimeLine(), l.value.createScatterTimeLine(), l.value.createFlyLineTimeLine(), l.value.createRegionalLevelTimeLine()), i("on-data-change", o), window.SHJParseEvent(a.useEvents, "on-data-change", o);
          } catch {
            i("on-data-change", null), window.SHJParseEvent(a.useEvents, "on-data-change", null);
          }
        }
      });
    }, N = () => {
      document.getElementById(w) && document.getElementById(I).removeChild(document.getElementById(w));
      const e = document.createElement("canvas");
      e.id = w, e.className = "zerov-map-3d", e.style.width = "100%", e.style.height = "100%", document.getElementById(I).appendChild(e);
    }, m = (e) => (O.value = !1, C.value = !1, l.value && (l.value.destroy(), l.value = null), N(), new x(document.getElementById(w), { geoProjectionCenter: [108.55, 36.32] }, e, {
      onAreaMouseover: (t) => {
        i("on-area-mouseover", t), window.SHJParseEvent(a.useEvents, "on-area-mouseover", t);
      },
      onAreaMouseout: (t) => {
        i("on-area-mouseout", t), window.SHJParseEvent(a.useEvents, "on-area-mouseout", t);
      },
      onLoadChild: (t) => {
        if (i("on-area-click", t), window.SHJParseEvent(a.useEvents, "on-area-click", t), t.adcode !== r.present && a.basicOption.scene.isDrilling && a.basicOption.scene.defaultMapAdcode !== 1) {
          i("on-drill-down", t), window.SHJParseEvent(a.useEvents, "on-drill-down", t);
          const s = v(a.basicOption);
          s.scene.defaultMapAdcode = t.adcode, l.value = m(s), r.push(t.adcode), r.past.length <= 0 ? p.value = !1 : p.value = !0;
        }
      },
      onMapSuccess: () => {
        O.value = !0, i("on-loaded"), window.SHJParseEvent(a.useEvents, "on-loaded", null);
      },
      onMapAnimationSuccess: () => {
        y(), C.value = !0, i("on-animated"), window.SHJParseEvent(a.useEvents, "on-animated", null);
      },
      onClickBar: (t) => {
        i("on-bar-click", t), window.SHJParseEvent(a.useEvents, "on-bar-click", t);
      },
      onClickScatter: (t) => {
        i("on-scatter-click", t), window.SHJParseEvent(a.useEvents, "on-scatter-click", t);
      },
      onClickFlyline: (t) => {
        i("on-flyline-click", t), window.SHJParseEvent(a.useEvents, "on-flyline-click", t);
      }
    }));
    K(() => {
      setTimeout(() => {
        l.value = m(a.basicOption), r.push(a.basicOption.scene.defaultMapAdcode), ee(a, l, m, y, j);
      }, 0);
    }), Q(() => {
      l.value && l.value.destroy();
    });
    const P = () => {
      i("on-return", { adcode: r.present }), window.SHJParseEvent(a.useEvents, "on-return", { adcode: r.present }), r.undo();
      const e = v(a.basicOption);
      e.scene.defaultMapAdcode = r.present, l.value = m(e), r.past.length <= 0 ? p.value = !1 : p.value = !0;
    }, j = () => {
      r.empty(), r.push(a.basicOption.scene.defaultMapAdcode), l.value = m(a.basicOption), r.past.length <= 0 ? p.value = !1 : p.value = !0;
    }, D = (e, o, t) => {
      z.timeline().add(z.to(l.value.camera.instance.position, {
        duration: 2,
        delay: 0,
        x: e,
        y: o,
        z: t,
        ease: "circ.out"
      }));
    };
    $({
      refresh: () => y(),
      refreshView: () => y(),
      refreshData: () => y(),
      // 返回上一级
      goBack: () => P(),
      // 更新相机位置
      setCameraPosition: (e, o, t) => D(e, o, t)
    });
    const U = Z(), F = g(), _ = (e) => {
      if (F.value && e) {
        L(a.basicOption.backButtonCss.fontFamily);
        const o = `url(${e.backgroundImage})`;
        return {
          ...E(e),
          left: a.basicOption.backButtonLeft + "%",
          bottom: a.basicOption.backButtonBottom + "%",
          "background-image": o
        };
      }
      return {};
    }, c = g(v(a.basicOption.widgets)), n = X(() => {
      try {
        const e = c.value.filter((o) => o.type === "regionalLevel");
        return e.length > 0 ? (L(e[0].labelStyle.fontFamily), e[0].isHide ? null : e[0]) : null;
      } catch {
        return null;
      }
    });
    b(() => v(a.basicOption.widgets), (e, o) => {
      try {
        !h(e, o) && l.value && (c.value = v(a.basicOption.widgets));
      } catch {
      }
    }, {
      deep: !0
    }), b(() => v(c.value.filter((e) => e.type === "bar")), (e, o) => {
      try {
        !h(e, o) && l.value && e.length === o.length && e.forEach((t) => {
          l.value.createBar(t, void 0, !1);
        });
      } catch {
      }
    }, {
      deep: !0
    }), b(() => v(c.value.filter((e) => e.type === "scatter")), (e, o) => {
      try {
        !h(e, o) && l.value && e.length === o.length && e.forEach((t) => {
          l.value.createScatter(t, void 0, !1);
        });
      } catch {
      }
    }, {
      deep: !0
    }), b(() => v(c.value.filter((e) => e.type === "flyline")), (e, o) => {
      try {
        !h(e, o) && l.value && e.length === o.length && e.forEach((t) => {
          l.value.createFlyLine(t, void 0, !1);
        });
      } catch {
      }
    }, {
      deep: !0
    }), b(() => v(c.value.filter((e) => e.type === "regionalLevel")), (e, o) => {
      try {
        !h(e, o) && l.value && e.length === o.length && e.forEach((t) => {
          l.value.createRegionalLevel(t, void 0, !1);
        });
      } catch {
      }
    }, {
      deep: !0
    });
    const q = (e) => {
      try {
        e.isHide === void 0 ? e.isHide = !0 : e.isHide = !e.isHide;
      } catch {
      }
    }, G = () => {
      try {
        const e = c.value.every((o) => !o.isHide);
        c.value.forEach((o) => o.isHide = e);
      } catch {
      }
    }, J = (e) => {
      L(a.basicOption.widgetControlStyle.fontFamily);
      const o = `url(${e.backgroundImage})`, t = `url(${A(e.hover.backgroundImage)})`, s = `url(${A(e.active.backgroundImage)})`;
      return {
        ...E(e),
        ...E(e.hover, "hover"),
        ...E(e.active, "active"),
        "background-image": o,
        "--hover-background-image": t,
        "--active-background-image": s
      };
    }, A = (e) => !e.startsWith("http") && !e.startsWith("//") ? "../" + e : e;
    return (e, o) => (d(), u("div", {
      id: "zerov-map3d-scene-wrap",
      class: H(["map-3d-wrap", { show: O.value }])
    }, [
      C.value && p.value && e.basicOption.backButton ? (d(), u("div", {
        key: 0,
        ref_key: "buttonRef",
        ref: F,
        class: "return-btn",
        style: f(_(e.basicOption.backButtonCss)),
        onClick: P
      }, " 返回上一级 ", 4)) : S("", !0),
      n.value ? (d(), u("div", {
        key: 1,
        class: "regional-level",
        style: f({
          left: n.value.labelStyle.left + "%",
          top: n.value.labelStyle.top + "%",
          flexDirection: n.value.labelStyle.direction,
          gap: n.value.labelStyle.gap + "px",
          fontSize: n.value.labelStyle.fontSize + "px",
          fontFamily: n.value.labelStyle.fontFamily,
          fontStyle: n.value.labelStyle.fontStyle,
          color: n.value.labelStyle.color,
          "--color-width": n.value.labelStyle.colorStyle.width + "px",
          "--color-height": n.value.labelStyle.colorStyle.height + "px",
          "--color-borderRadius": n.value.labelStyle.colorStyle.borderRadius + "px"
        })
      }, [
        (d(!0), u(W, null, M(n.value.rules, (t, s) => (d(), u("div", {
          key: s,
          class: "item"
        }, [
          B("div", {
            class: "color",
            style: f({ backgroundColor: t.color })
          }, null, 4),
          B("span", oe, k(t.label), 1)
        ]))), 128))
      ], 4)) : S("", !0),
      e.basicOption.widgets.length > 0 && e.basicOption.widgetControlStyle && e.basicOption.widgetControlStyle.show ? (d(), u("div", {
        key: 2,
        class: "widget-control",
        style: f({
          left: e.basicOption.widgetControlStyle.left + "%",
          top: e.basicOption.widgetControlStyle.top + "%",
          flexDirection: e.basicOption.widgetControlStyle.direction,
          gap: e.basicOption.widgetControlStyle.gap + "px",
          "--widget-width": e.basicOption.widgetControlStyle.width + "px",
          "--widget-height": e.basicOption.widgetControlStyle.height + "px"
        })
      }, [
        B("div", {
          class: H(["control-button", {
            active: c.value.every((t) => !t.isHide)
          }]),
          style: f(J(e.basicOption.widgetControlStyle)),
          onClick: G
        }, k(c.value.every((t) => !t.isHide) ? "隐藏" : "显示") + "/全部 ", 7),
        (d(!0), u(W, null, M(c.value, (t) => (d(), u("div", {
          key: t.id,
          class: H(["control-button", {
            active: !t.isHide
          }]),
          style: f(J(e.basicOption.widgetControlStyle)),
          onClick: (s) => q(t)
        }, k(t.name || "-"), 15, ae))), 128))
      ], 4)) : S("", !0),
      e.basicOption.debugger ? (d(), u("div", le, " fps:" + k(Y(U)), 1)) : S("", !0)
    ], 2));
  }
});
export {
  ke as default
};
