var C = Object.defineProperty;
var S = (t, o, n) => o in t ? C(t, o, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[o] = n;
var a = (t, o, n) => S(t, typeof o != "symbol" ? o + "" : o, n);
import { ref as f, onMounted as b, onBeforeUnmount as k } from "vue";
import { useResizeObserver as w } from "@vueuse/core";
import { Engine as T, Scene as L, Color4 as l, Color3 as c, CubeTexture as g, ImportMeshAsync as M, Vector3 as m, Tools as x, MeshBuilder as p, StandardMaterial as D, Texture as R } from "@babylonjs/core";
import "@babylonjs/loaders";
const U = (t, o) => {
  const n = f(), u = f(!0);
  class v {
    // eslint-disable-next-line no-useless-constructor
    constructor(e) {
      // optional, but needed due to interface definitions
      a(this, "loadingUIBackgroundColor");
      this.loadingUIText = e;
    }
    displayLoadingUI() {
      n.value && (u.value = !0);
    }
    hideLoadingUI() {
      n.value && (u.value = !1);
    }
  }
  class d {
    constructor(e, s, r) {
      a(this, "scene");
      a(this, "engine");
      a(this, "gs");
      a(this, "skybox");
      this.canvas = e, this.option = s, this.events = r;
      try {
        this.engine = new T(this.canvas, !0, {}, !1), this.engine.loadingScreen = new v(""), window.addEventListener("resize", () => {
          this.engine.resize();
        }), this.scene = this.createScene(), this.setSkybox();
      } catch {
      }
    }
    createScene() {
      return this.engine.displayLoadingUI(), this.scene = new L(this.engine), this.option.scene.colorShow ? this.scene.clearColor = l.FromHexString(
        this.option.scene.color || "#000000"
      ) : this.scene.clearColor = new l(0, 0, 0, 0), this.scene.fogEnabled = this.option.scene.fogEnabled, this.scene.fogMode = this.option.scene.fogMode || 1, this.scene.fogColor = c.FromHexString(
        this.option.scene.fogColor || "#ffffff"
      ), this.scene.fogEnd = this.option.scene.fogEnd || 60, this.scene.fogStart = this.option.scene.fogStart || 1, this.scene.fogDensity = this.option.scene.fogDensity || 0.1, t.scene.environmentTexture ? (this.scene.environmentTexture = g.CreateFromPrefilteredData(
        t.scene.environmentTexture,
        this.scene
      ), this.scene.environmentIntensity = t.scene.environmentIntensity) : this.scene.environmentTexture = null, this.scene.createDefaultCamera(!0, !0, !0), M(this.option.meshs.url, this.scene).then((e) => {
        this.createCamera(this.option.camera, this.scene), this.gs = e.meshes[0], this.updateMeshSize(this.option.meshs), this.engine.hideLoadingUI();
      }), this.scene;
    }
    updateScene(e) {
      e.colorShow ? this.scene.clearColor = l.FromHexString(
        e.color || "#000000"
      ) : this.scene.clearColor = new l(0, 0, 0, 0), this.scene.fogEnabled = e.fogEnabled, this.scene.fogMode = e.fogMode || 1, this.scene.fogColor = c.FromHexString(
        e.fogColor || "#ffffff"
      ), this.scene.fogEnd = e.fogEnd || 60, this.scene.fogStart = e.fogStart || 1, this.scene.fogDensity = e.fogDensity || 0.1, t.scene.environmentTexture ? (this.scene.environmentTexture = g.CreateFromPrefilteredData(
        t.scene.environmentTexture,
        this.scene
      ), this.scene.environmentIntensity = t.scene.environmentIntensity) : this.scene.environmentTexture = null;
    }
    createCamera(e, s) {
      s.createDefaultCamera(!0, !0, !0), this.scene.activeCamera && this.updateCamera(e);
    }
    updateMeshSize(e) {
      const { position: s, scaling: r, rotation: h } = e;
      this.gs.position.set(
        s[0] || 0,
        s[1] || 0,
        s[2] || 0
      ), this.gs.scaling.set(r[0] || 1, r[1] || 1, r[2] || 1), this.gs.rotation = new m(
        h[0] || 0,
        h[1] || 0,
        h[2] || 0
      );
    }
    updateCamera(e) {
      const s = this.scene.activeCamera;
      s && (s.target = new m(...e.target), s.beta = x.ToRadians(e.beta), s.alpha = x.ToRadians(e.alpha), e.autoRadius || (s.radius = e.radius || 1), s.useAutoRotationBehavior = e.autoRotation, s.zoomToMouseLocation = e.zoomToMouseLocation, s.wheelDeltaPercentage = e.wheelDeltaPercentage || 0.01, s.lowerRadiusLimit = e.lowerRadiusLimit, s.upperRadiusLimit = e.upperRadiusLimit, s.minZ = 0.01, s.attachControl(n.value, !0));
    }
    setSkybox() {
      if (t.skybox)
        if (t.skybox.enable) {
          const { size: e } = t.skybox;
          this.skybox || (this.skybox = p.CreateBox("skyBox", { size: e }, this.scene));
          const s = new D("skyBox", this.scene);
          s.backFaceCulling = !1, s.reflectionTexture = g.CreateFromPrefilteredData(
            t.scene.environmentTexture,
            this.scene
          ), s.reflectionTexture.coordinatesMode = R.SKYBOX_MODE, s.diffuseColor = new c(1, 1, 1), s.specularColor = new c(1, 1, 1), s.disableLighting = !0, this.skybox.material = s, this.skybox.isPickable = !1;
        } else
          this.skybox && (this.skybox.dispose(), this.skybox = null);
    }
    run() {
      return this.engine.runRenderLoop(() => {
        this.scene.render();
      }), this;
    }
    dispose() {
      this.scene && this.engine && (this.scene.dispose(), this.engine.dispose(), this.gs = null);
    }
  }
  const i = f();
  return b(() => {
    t.meshs && (i.value = new d(
      n.value,
      t,
      o
    ).run(), w(n.value, () => {
      i.value && i.value.engine.resize();
    }));
  }), k(() => {
    i.value && i.value.dispose();
  }), {
    canvasRef: n,
    babylon: i,
    GsBabylonScene: d,
    babylonLoadingStatus: u
  };
};
export {
  U as useBabylonScene
};
