import H from "../utils/JSAsyncLoader.mjs";
import { useState as d, useEffect as v } from "react";
function $(F) {
  const [m, y] = d(!1), [D, b] = d(), [i, G] = d(), [g, L] = d(!1), [l, S] = d(), [r, M] = d([]), [c, V] = d(0);
  function W(o) {
    if (o.viewType != "3DView") {
      console.error("bimface view type is not 3DView");
      return;
    }
    y(!0), l && N(o.viewToken);
  }
  const N = (o) => {
    if (!D) {
      let a = l, t = new window.Glodon.Bimface.Application.WebApplication3DConfig();
      t.domElement = a, t.Toolbars = [];
      const e = new window.Glodon.Bimface.Application.WebApplication3D(t);
      e.addView(o), b(e);
      const n = e.getViewer();
      n.addEventListener(window.Glodon.Bimface.Viewer.Viewer3DEvent.ViewAdded, function() {
        window.onresize = function() {
          n.resize(document.documentElement.clientWidth, document.documentElement.clientHeight - 40);
        }, n.setCameraAnimation(!1), n.render(), n.setNavigationMode(window.Glodon.Bimface.Viewer.NavigationMode3D.Walk), n.render(), L(!0);
      }), n.addEventListener(window.Glodon.Bimface.Viewer.Viewer3DEvent.ModelAdded, () => {
        V((f) => f + 1);
      }), G(n);
    }
  };
  v(() => {
    g && r.forEach((o) => {
      i.loadModel({
        viewToken: o
      });
    });
  }, [g, r]), v(() => {
  }, [c]), v(() => {
    if (!l || !r.length)
      return;
    const o = r.pop();
    o && A(o), M([...r]);
  }, [l]);
  function I(o) {
    console.log(o);
  }
  function A(o) {
    V(0);
    const a = () => {
      let t = new window.BimfaceSDKLoaderConfig();
      t.viewToken = o, window.BimfaceSDKLoader.load(t, W, I);
    };
    if (!window.BimfaceSDKLoaderConfig) {
      H("https://static.bimface.com/api/BimfaceSDKLoader/BimfaceSDKLoader@latest-release.js", a);
      return;
    }
    a();
  }
  const K = (o, a) => {
    if (!i || !g || !l) {
      console.error("BIMFace viewer is not ready.");
      return;
    }
    const t = l.getElementsByTagName("canvas")[0], e = document.createElement("canvas");
    e.width = t.width, e.height = t.height;
    const n = e.getContext("2d");
    return n && (n.fillStyle = "#fff", n.fillRect(0, 0, e.width, e.height), n.drawImage(t, 0, 0, t.width, t.height)), e.toDataURL(o, a);
  }, k = (o) => {
    if (!i) {
      console.error("viewer is not ready");
      return;
    }
    if (c && r.length + 1 === c || !r.length)
      i.getAllModels().map((e) => i._models[e.modelId]).forEach((e) => {
        let n = new window.Glodon.Web.Graphics.Color(o);
        e.overrideAllComponentsColor && e.overrideAllComponentsColor(n);
      }), i.enableWireframe(!0), i.getLightManager().getAllFillLights().forEach((e) => e.enableLight(!0)), i.render();
    else {
      console.error("viewer is not render complete");
      return;
    }
  }, x = (o) => {
    if (i)
      if (c && r.length + 1 === c || !r.length)
        i.getAllModels().map((e) => i._models[e.modelId]).forEach((e) => {
          Object.keys(o).forEach((n) => {
            let f = [{
              categoryId: n
            }];
            e.getComponentsByConditions(f, function(B) {
              let w = new window.Glodon.Web.Graphics.Color(o[n]);
              e.overrideComponentsColorById && e.overrideComponentsColorById(B, w);
            }, function() {
              window.alert("error");
            });
          });
        }), i.enableWireframe(!1), i.getLightManager().getAllFillLights().forEach((e) => e.enableLight(!1)), i.render();
      else {
        console.error("viewer is not render complete");
        return;
      }
  };
  function P(o, a, t) {
    const e = i.getModel();
    e == null || e.getBoundingBox(({
      currentBoundingBox: n
    }) => {
      const f = n.max.x - n.min.x, B = n.max.y - n.min.y, w = f / B;
      let p, u;
      w > 1 ? (p = a.width, u = p / w) : (u = a.height, p = u * w);
      const h = document.createElement("div");
      h.setAttribute("style", `width: ${p}px;height: ${u}px`), document.body.appendChild(h);
      const E = new window.Glodon.Bimface.Plugins.NavigationMap.MapViewerConfig();
      E.domElement = h;
      const T = new window.Glodon.Bimface.Plugins.NavigationMap.MapViewer(E), s = new window.Glodon.Bimface.Plugins.NavigationMap.NavigationMapConfig();
      s.viewer = i, s.mapViewer = T, s.type = "SetProfile", s.height = o, s.successCallback = (R) => {
        setTimeout(() => {
          const C = new Image();
          C.src = h.getElementsByTagName("image")[0].getAttribute("href") || "", C.onload = () => {
            t(C);
          };
        }, 200);
      }, new window.Glodon.Bimface.Plugins.NavigationMap.NavigationMap(s);
    });
  }
  return {
    BimfaceViewer: m && window.Glodon.Bimface.Viewer,
    BimfaceSection: m && window.Glodon.Bimface.Plugins.Section,
    BimfaceMark: m && window.Glodon.Bimface.Plugins.Marker3D,
    BimfaceViewerEvents: m && window.Glodon.Bimface.Viewer.Viewer3DEvent,
    viewer: i,
    viewerAdded: g,
    sDKLoaded: m,
    loadedModelCount: c,
    loadModels: (o) => {
      if (o != null && o.length || console.error("viewTokens is Empty"), !i && l) {
        const a = o[0];
        A(a);
      }
      M(o);
    },
    setContainerDom: S,
    getSnapshot: K,
    setComponentsColorByCatgryId: x,
    setAllComponentsColor: k,
    navigationMapByHeight: P
  };
}
export {
  $ as useBimface
};
