import { jsxs as Y, jsx as x } from "react/jsx-runtime";
import { e as ue, u as ce, a as ae, c as fe, i as de, E as ve, B as pe, b as me, d as he, A as be } from "../../AICharacter-B2JS_ruH.js";
import { g as we, _ as Q } from "../../index-k8aZRcq6.js";
import * as i from "react";
import { useState as B, useRef as $, useEffect as O, useMemo as Ee, Suspense as ye } from "react";
import { aq as _e } from "../../three.module-BPupfU8l.js";
function T(t, e, n) {
  var r, o, l, u, c;
  e == null && (e = 100);
  function s() {
    var d = Date.now() - u;
    d < e && d >= 0 ? r = setTimeout(s, e - d) : (r = null, n || (c = t.apply(l, o), l = o = null));
  }
  var v = function() {
    l = this, o = arguments, u = Date.now();
    var d = n && !r;
    return r || (r = setTimeout(s, e)), d && (c = t.apply(l, o), l = o = null), c;
  };
  return v.clear = function() {
    r && (clearTimeout(r), r = null);
  }, v.flush = function() {
    r && (c = t.apply(l, o), l = o = null, clearTimeout(r), r = null);
  }, v;
}
T.debounce = T;
var xe = T;
const X = /* @__PURE__ */ we(xe);
function Oe(t) {
  let {
    debounce: e,
    scroll: n,
    polyfill: r,
    offsetSize: o
  } = t === void 0 ? {
    debounce: 0,
    scroll: !1,
    offsetSize: !1
  } : t;
  const l = r || (typeof window > "u" ? class {
  } : window.ResizeObserver);
  if (!l)
    throw new Error("This browser does not support ResizeObserver out of the box. See: https://github.com/react-spring/react-use-measure/#resize-observer-polyfills");
  const [u, c] = B({
    left: 0,
    top: 0,
    width: 0,
    height: 0,
    bottom: 0,
    right: 0,
    x: 0,
    y: 0
  }), s = $({
    element: null,
    scrollContainers: null,
    resizeObserver: null,
    lastBounds: u
  }), v = e ? typeof e == "number" ? e : e.scroll : null, d = e ? typeof e == "number" ? e : e.resize : null, b = $(!1);
  O(() => (b.current = !0, () => void (b.current = !1)));
  const [w, E, m] = Ee(() => {
    const a = () => {
      if (!s.current.element)
        return;
      const {
        left: z,
        top: R,
        width: L,
        height: M,
        bottom: D,
        right: j,
        x: N,
        y: P
      } = s.current.element.getBoundingClientRect(), p = {
        left: z,
        top: R,
        width: L,
        height: M,
        bottom: D,
        right: j,
        x: N,
        y: P
      };
      s.current.element instanceof HTMLElement && o && (p.height = s.current.element.offsetHeight, p.width = s.current.element.offsetWidth), Object.freeze(p), b.current && !Pe(s.current.lastBounds, p) && c(s.current.lastBounds = p);
    };
    return [a, d ? X(a, d) : a, v ? X(a, v) : a];
  }, [c, o, v, d]);
  function f() {
    s.current.scrollContainers && (s.current.scrollContainers.forEach((a) => a.removeEventListener("scroll", m, !0)), s.current.scrollContainers = null), s.current.resizeObserver && (s.current.resizeObserver.disconnect(), s.current.resizeObserver = null);
  }
  function h() {
    s.current.element && (s.current.resizeObserver = new l(m), s.current.resizeObserver.observe(s.current.element), n && s.current.scrollContainers && s.current.scrollContainers.forEach((a) => a.addEventListener("scroll", m, {
      capture: !0,
      passive: !0
    })));
  }
  const g = (a) => {
    !a || a === s.current.element || (f(), s.current.element = a, s.current.scrollContainers = Z(a), h());
  };
  return ze(m, !!n), ge(E), O(() => {
    f(), h();
  }, [n, m, E]), O(() => f, []), [g, u, w];
}
function ge(t) {
  O(() => {
    const e = t;
    return window.addEventListener("resize", e), () => void window.removeEventListener("resize", e);
  }, [t]);
}
function ze(t, e) {
  O(() => {
    if (e) {
      const n = t;
      return window.addEventListener("scroll", n, {
        capture: !0,
        passive: !0
      }), () => void window.removeEventListener("scroll", n, !0);
    }
  }, [t, e]);
}
function Z(t) {
  const e = [];
  if (!t || t === document.body)
    return e;
  const {
    overflow: n,
    overflowX: r,
    overflowY: o
  } = window.getComputedStyle(t);
  return [n, r, o].some((l) => l === "auto" || l === "scroll") && e.push(t), [...e, ...Z(t.parentElement)];
}
const Re = ["x", "y", "top", "bottom", "left", "right", "width", "height"], Pe = (t, e) => Re.every((n) => t[n] === e[n]);
var Ce = Object.defineProperty, Le = Object.defineProperties, Se = Object.getOwnPropertyDescriptors, q = Object.getOwnPropertySymbols, Me = Object.prototype.hasOwnProperty, De = Object.prototype.propertyIsEnumerable, U = (t, e, n) => e in t ? Ce(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n, G = (t, e) => {
  for (var n in e || (e = {}))
    Me.call(e, n) && U(t, n, e[n]);
  if (q)
    for (var n of q(e))
      De.call(e, n) && U(t, n, e[n]);
  return t;
}, je = (t, e) => Le(t, Se(e));
function ee(t, e, n) {
  if (!t)
    return;
  if (n(t) === !0)
    return t;
  let r = e ? t.return : t.child;
  for (; r; ) {
    const o = ee(r, e, n);
    if (o)
      return o;
    r = e ? null : r.sibling;
  }
}
function te(t) {
  try {
    return Object.defineProperties(t, {
      _currentRenderer: {
        get() {
          return null;
        },
        set() {
        }
      },
      _currentRenderer2: {
        get() {
          return null;
        },
        set() {
        }
      }
    });
  } catch {
    return t;
  }
}
const F = te(i.createContext(null));
class re extends i.Component {
  render() {
    return /* @__PURE__ */ i.createElement(F.Provider, {
      value: this._reactInternals
    }, this.props.children);
  }
}
const { ReactCurrentOwner: J, ReactCurrentDispatcher: K } = i.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
function Ne() {
  const t = i.useContext(F);
  if (t === null)
    throw new Error("its-fine: useFiber must be called within a <FiberProvider />!");
  const e = i.useId();
  return i.useMemo(() => {
    for (const r of [J == null ? void 0 : J.current, t, t == null ? void 0 : t.alternate]) {
      if (!r)
        continue;
      const o = ee(r, !1, (l) => {
        let u = l.memoizedState;
        for (; u; ) {
          if (u.memoizedState === e)
            return !0;
          u = u.next;
        }
      });
      if (o)
        return o;
    }
  }, [t, e]);
}
function ke() {
  var t, e;
  const n = Ne(), [r] = i.useState(() => /* @__PURE__ */ new Map());
  r.clear();
  let o = n;
  for (; o; ) {
    const l = (t = o.type) == null ? void 0 : t._context;
    l && l !== F && !r.has(l) && r.set(l, (e = K == null ? void 0 : K.current) == null ? void 0 : e.readContext(te(l))), o = o.return;
  }
  return r;
}
function Be() {
  const t = ke();
  return i.useMemo(
    () => Array.from(t.keys()).reduce(
      (e, n) => (r) => /* @__PURE__ */ i.createElement(e, null, /* @__PURE__ */ i.createElement(n.Provider, je(G({}, r), {
        value: t.get(n)
      }))),
      (e) => /* @__PURE__ */ i.createElement(re, G({}, e))
    ),
    [t]
  );
}
const k = {
  onClick: ["click", !1],
  onContextMenu: ["contextmenu", !1],
  onDoubleClick: ["dblclick", !1],
  onWheel: ["wheel", !0],
  onPointerDown: ["pointerdown", !0],
  onPointerUp: ["pointerup", !0],
  onPointerLeave: ["pointerleave", !0],
  onPointerMove: ["pointermove", !0],
  onPointerCancel: ["pointercancel", !0],
  onLostPointerCapture: ["lostpointercapture", !0]
};
function Te(t) {
  const {
    handlePointer: e
  } = he(t);
  return {
    priority: 1,
    enabled: !0,
    compute(n, r, o) {
      r.pointer.set(n.offsetX / r.size.width * 2 - 1, -(n.offsetY / r.size.height) * 2 + 1), r.raycaster.setFromCamera(r.pointer, r.camera);
    },
    connected: void 0,
    handlers: Object.keys(k).reduce((n, r) => ({
      ...n,
      [r]: e(r)
    }), {}),
    update: () => {
      var n;
      const {
        events: r,
        internal: o
      } = t.getState();
      (n = o.lastEvent) != null && n.current && r.handlers && r.handlers.onPointerMove(o.lastEvent.current);
    },
    connect: (n) => {
      var r;
      const {
        set: o,
        events: l
      } = t.getState();
      l.disconnect == null || l.disconnect(), o((u) => ({
        events: {
          ...u.events,
          connected: n
        }
      })), Object.entries((r = l.handlers) != null ? r : []).forEach(([u, c]) => {
        const [s, v] = k[u];
        n.addEventListener(s, c, {
          passive: v
        });
      });
    },
    disconnect: () => {
      const {
        set: n,
        events: r
      } = t.getState();
      if (r.connected) {
        var o;
        Object.entries((o = r.handlers) != null ? o : []).forEach(([l, u]) => {
          if (r && r.connected instanceof HTMLElement) {
            const [c] = k[l];
            r.connected.removeEventListener(c, u);
          }
        }), n((l) => ({
          events: {
            ...l.events,
            connected: void 0
          }
        }));
      }
    }
  };
}
const Fe = /* @__PURE__ */ i.forwardRef(function({
  children: e,
  fallback: n,
  resize: r,
  style: o,
  gl: l,
  events: u = Te,
  eventSource: c,
  eventPrefix: s,
  shadows: v,
  linear: d,
  flat: b,
  legacy: w,
  orthographic: E,
  frameloop: m,
  dpr: f,
  performance: h,
  raycaster: g,
  camera: a,
  scene: z,
  onPointerMissed: R,
  onCreated: L,
  ...M
}, D) {
  i.useMemo(() => ue(_e), []);
  const j = Be(), [N, P] = Oe({
    scroll: !0,
    debounce: {
      scroll: 50,
      resize: 0
    },
    ...r
  }), p = i.useRef(null), I = i.useRef(null);
  i.useImperativeHandle(D, () => p.current);
  const A = ce(R), [H, ne] = i.useState(!1), [V, oe] = i.useState(!1);
  if (H)
    throw H;
  if (V)
    throw V;
  const S = i.useRef(null);
  ae(() => {
    const C = p.current;
    P.width > 0 && P.height > 0 && C && (S.current || (S.current = fe(C)), S.current.configure({
      gl: l,
      events: u,
      shadows: v,
      linear: d,
      flat: b,
      legacy: w,
      orthographic: E,
      frameloop: m,
      dpr: f,
      performance: h,
      raycaster: g,
      camera: a,
      scene: z,
      size: P,
      // Pass mutable reference to onPointerMissed so it's free to update
      onPointerMissed: (...y) => A.current == null ? void 0 : A.current(...y),
      onCreated: (y) => {
        y.events.connect == null || y.events.connect(c ? de(c) ? c.current : c : I.current), s && y.setEvents({
          compute: (W, _) => {
            const se = W[s + "X"], ie = W[s + "Y"];
            _.pointer.set(se / _.size.width * 2 - 1, -(ie / _.size.height) * 2 + 1), _.raycaster.setFromCamera(_.pointer, _.camera);
          }
        }), L == null || L(y);
      }
    }), S.current.render(/* @__PURE__ */ i.createElement(j, null, /* @__PURE__ */ i.createElement(ve, {
      set: oe
    }, /* @__PURE__ */ i.createElement(i.Suspense, {
      fallback: /* @__PURE__ */ i.createElement(pe, {
        set: ne
      })
    }, e)))));
  }), i.useEffect(() => {
    const C = p.current;
    if (C)
      return () => me(C);
  }, []);
  const le = c ? "none" : "auto";
  return /* @__PURE__ */ i.createElement("div", Q({
    ref: I,
    style: {
      position: "relative",
      width: "100%",
      height: "100%",
      overflow: "hidden",
      pointerEvents: le,
      ...o
    }
  }, M), /* @__PURE__ */ i.createElement("div", {
    ref: N,
    style: {
      width: "100%",
      height: "100%"
    }
  }, /* @__PURE__ */ i.createElement("canvas", {
    ref: p,
    style: {
      display: "block"
    }
  }, n)));
}), Ie = /* @__PURE__ */ i.forwardRef(function(e, n) {
  return /* @__PURE__ */ i.createElement(re, null, /* @__PURE__ */ i.createElement(Fe, Q({}, e, {
    ref: n
  })));
}), Xe = ({
  animation: t,
  modelUrl: e,
  onAudioEnd: n,
  aside: r,
  speakCount: o = 0,
  emotion: l,
  zoom: u,
  position: c,
  loadingText: s,
  modelConfigs: v,
  currentVrm: d,
  setCurrentVrm: b,
  onProgressChange: w,
  onLoadErr: E,
  scale: m,
  sceneConfigs: f
}) => {
  const [h, g] = B(0), [a, z] = B();
  return O(() => {
    w && w(h);
  }, [h]), /* @__PURE__ */ Y(
    "div",
    {
      className: "virtual-lg:rounded-3xl virtual-flex virtual-relative virtual-items-center virtual-justify-center virtual-h-full virtual-w-full",
      children: [
        /* @__PURE__ */ Y(
          "div",
          {
            className: `virtual-absolute virtual-top-1/2 virtual-left-1/2 virtual--translate-x-1/2 virtual--translate-y-1/2 virtual-rounded-2xl virtual-px-4 virtual-py-2 virtual-bg-black/30 virtual-flex virtual-items-center virtual-justify-center virtual-pointer-events-none virtual-flex-col ${e === "" || h >= 100 ? "virtual-fadeOut" : ""}`,
            children: [
              /* @__PURE__ */ x(
                "p",
                {
                  className: "virtual-font-wenhei virtual-text-lg virtual-text-white virtual-animate-flicker virtual-text-center virtual-w-fit",
                  children: `${s ?? "Your Virtual is Dressing Up..."}`
                }
              ),
              /* @__PURE__ */ x("div", { className: "virtual-bg-white/20 virtual-h-[4px] virtual-w-[80%] virtual-rounded-3xl virtual-mb-2 virtual-mt-4", children: /* @__PURE__ */ x(
                "div",
                {
                  className: "virtual-bg-white virtual-h-[4px] virtual-rounded-3xl",
                  style: {
                    width: `${h}%`
                  }
                }
              ) })
            ]
          }
        ),
        /* @__PURE__ */ x(ye, { fallback: null, children: /* @__PURE__ */ x(
          Ie,
          {
            camera: {
              position: [0, 3, 15],
              zoom: u || 2
            },
            linear: (f == null ? void 0 : f.linear) ?? !0,
            flat: (f == null ? void 0 : f.flat) ?? !0,
            shadows: (f == null ? void 0 : f.shadows) ?? !0,
            children: !!e && /* @__PURE__ */ x(
              be,
              {
                animation: t,
                url: e,
                speakCount: o,
                onAudioEnd: n,
                onLoad: (R) => {
                  g(R);
                },
                zoom: u ?? 2,
                aside: r,
                emotion: l,
                position: c,
                modelConfigs: v,
                currentVrm: d || a,
                setCurrentVrm: b || z,
                onLoadErr: E,
                scale: m
              }
            )
          }
        ) })
      ]
    }
  );
};
export {
  Xe as CharacterScene
};
