import { jsxs as N, jsx as b } from "react/jsx-runtime";
import { e as ce, u as ue, 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-CUQD0zZ0.js";
import { g as we, _ as Q } from "../../index-k8aZRcq6.js";
import * as l from "react";
import { useState as B, useRef as $, useEffect as C, useMemo as Ee, Suspense as ge } from "react";
import { aq as ye, V as _e } from "../../three.module-BPupfU8l.js";
function T(t, e, n) {
  var r, o, s, c, u;
  e == null && (e = 100);
  function i() {
    var f = Date.now() - c;
    f < e && f >= 0 ? r = setTimeout(i, e - f) : (r = null, n || (u = t.apply(s, o), s = o = null));
  }
  var d = function() {
    s = this, o = arguments, c = Date.now();
    var f = n && !r;
    return r || (r = setTimeout(i, e)), f && (u = t.apply(s, o), s = o = null), u;
  };
  return d.clear = function() {
    r && (clearTimeout(r), r = null);
  }, d.flush = function() {
    r && (u = t.apply(s, o), s = o = null, clearTimeout(r), r = null);
  }, d;
}
T.debounce = T;
var Ce = T;
const X = /* @__PURE__ */ we(Ce);
function xe(t) {
  let {
    debounce: e,
    scroll: n,
    polyfill: r,
    offsetSize: o
  } = t === void 0 ? {
    debounce: 0,
    scroll: !1,
    offsetSize: !1
  } : t;
  const s = r || (typeof window > "u" ? class {
  } : window.ResizeObserver);
  if (!s)
    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 [c, u] = B({
    left: 0,
    top: 0,
    width: 0,
    height: 0,
    bottom: 0,
    right: 0,
    x: 0,
    y: 0
  }), i = $({
    element: null,
    scrollContainers: null,
    resizeObserver: null,
    lastBounds: c
  }), d = e ? typeof e == "number" ? e : e.scroll : null, f = e ? typeof e == "number" ? e : e.resize : null, h = $(!1);
  C(() => (h.current = !0, () => void (h.current = !1)));
  const [w, E, m] = Ee(() => {
    const a = () => {
      if (!i.current.element)
        return;
      const {
        left: O,
        top: F,
        width: P,
        height: S,
        bottom: M,
        right: D,
        x: j,
        y: z
      } = i.current.element.getBoundingClientRect(), p = {
        left: O,
        top: F,
        width: P,
        height: S,
        bottom: M,
        right: D,
        x: j,
        y: z
      };
      i.current.element instanceof HTMLElement && o && (p.height = i.current.element.offsetHeight, p.width = i.current.element.offsetWidth), Object.freeze(p), h.current && !Pe(i.current.lastBounds, p) && u(i.current.lastBounds = p);
    };
    return [a, f ? X(a, f) : a, d ? X(a, d) : a];
  }, [u, o, d, f]);
  function v() {
    i.current.scrollContainers && (i.current.scrollContainers.forEach((a) => a.removeEventListener("scroll", m, !0)), i.current.scrollContainers = null), i.current.resizeObserver && (i.current.resizeObserver.disconnect(), i.current.resizeObserver = null);
  }
  function g() {
    i.current.element && (i.current.resizeObserver = new s(m), i.current.resizeObserver.observe(i.current.element), n && i.current.scrollContainers && i.current.scrollContainers.forEach((a) => a.addEventListener("scroll", m, {
      capture: !0,
      passive: !0
    })));
  }
  const x = (a) => {
    !a || a === i.current.element || (v(), i.current.element = a, i.current.scrollContainers = Z(a), g());
  };
  return ze(m, !!n), Oe(E), C(() => {
    v(), g();
  }, [n, m, E]), C(() => v, []), [x, c, w];
}
function Oe(t) {
  C(() => {
    const e = t;
    return window.addEventListener("resize", e), () => void window.removeEventListener("resize", e);
  }, [t]);
}
function ze(t, e) {
  C(() => {
    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((s) => s === "auto" || s === "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 Le = Object.defineProperty, Fe = Object.defineProperties, Se = Object.getOwnPropertyDescriptors, q = Object.getOwnPropertySymbols, Me = Object.prototype.hasOwnProperty, De = Object.prototype.propertyIsEnumerable, U = (t, e, n) => e in t ? Le(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) => Fe(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 I = te(l.createContext(null));
class re extends l.Component {
  render() {
    return /* @__PURE__ */ l.createElement(I.Provider, {
      value: this._reactInternals
    }, this.props.children);
  }
}
const { ReactCurrentOwner: J, ReactCurrentDispatcher: K } = l.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
function Ne() {
  const t = l.useContext(I);
  if (t === null)
    throw new Error("its-fine: useFiber must be called within a <FiberProvider />!");
  const e = l.useId();
  return l.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, (s) => {
        let c = s.memoizedState;
        for (; c; ) {
          if (c.memoizedState === e)
            return !0;
          c = c.next;
        }
      });
      if (o)
        return o;
    }
  }, [t, e]);
}
function ke() {
  var t, e;
  const n = Ne(), [r] = l.useState(() => /* @__PURE__ */ new Map());
  r.clear();
  let o = n;
  for (; o; ) {
    const s = (t = o.type) == null ? void 0 : t._context;
    s && s !== I && !r.has(s) && r.set(s, (e = K == null ? void 0 : K.current) == null ? void 0 : e.readContext(te(s))), o = o.return;
  }
  return r;
}
function Be() {
  const t = ke();
  return l.useMemo(
    () => Array.from(t.keys()).reduce(
      (e, n) => (r) => /* @__PURE__ */ l.createElement(e, null, /* @__PURE__ */ l.createElement(n.Provider, je(G({}, r), {
        value: t.get(n)
      }))),
      (e) => /* @__PURE__ */ l.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: s
      } = t.getState();
      s.disconnect == null || s.disconnect(), o((c) => ({
        events: {
          ...c.events,
          connected: n
        }
      })), Object.entries((r = s.handlers) != null ? r : []).forEach(([c, u]) => {
        const [i, d] = k[c];
        n.addEventListener(i, u, {
          passive: d
        });
      });
    },
    disconnect: () => {
      const {
        set: n,
        events: r
      } = t.getState();
      if (r.connected) {
        var o;
        Object.entries((o = r.handlers) != null ? o : []).forEach(([s, c]) => {
          if (r && r.connected instanceof HTMLElement) {
            const [u] = k[s];
            r.connected.removeEventListener(u, c);
          }
        }), n((s) => ({
          events: {
            ...s.events,
            connected: void 0
          }
        }));
      }
    }
  };
}
const Ie = /* @__PURE__ */ l.forwardRef(function({
  children: e,
  fallback: n,
  resize: r,
  style: o,
  gl: s,
  events: c = Te,
  eventSource: u,
  eventPrefix: i,
  shadows: d,
  linear: f,
  flat: h,
  legacy: w,
  orthographic: E,
  frameloop: m,
  dpr: v,
  performance: g,
  raycaster: x,
  camera: a,
  scene: O,
  onPointerMissed: F,
  onCreated: P,
  ...S
}, M) {
  l.useMemo(() => ce(ye), []);
  const D = Be(), [j, z] = xe({
    scroll: !0,
    debounce: {
      scroll: 50,
      resize: 0
    },
    ...r
  }), p = l.useRef(null), V = l.useRef(null);
  l.useImperativeHandle(M, () => p.current);
  const A = ue(F), [H, ne] = l.useState(!1), [W, oe] = l.useState(!1);
  if (H)
    throw H;
  if (W)
    throw W;
  const L = l.useRef(null);
  ae(() => {
    const R = p.current;
    z.width > 0 && z.height > 0 && R && (L.current || (L.current = fe(R)), L.current.configure({
      gl: s,
      events: c,
      shadows: d,
      linear: f,
      flat: h,
      legacy: w,
      orthographic: E,
      frameloop: m,
      dpr: v,
      performance: g,
      raycaster: x,
      camera: a,
      scene: O,
      size: z,
      // 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(u ? de(u) ? u.current : u : V.current), i && y.setEvents({
          compute: (Y, _) => {
            const ie = Y[i + "X"], le = Y[i + "Y"];
            _.pointer.set(ie / _.size.width * 2 - 1, -(le / _.size.height) * 2 + 1), _.raycaster.setFromCamera(_.pointer, _.camera);
          }
        }), P == null || P(y);
      }
    }), L.current.render(/* @__PURE__ */ l.createElement(D, null, /* @__PURE__ */ l.createElement(ve, {
      set: oe
    }, /* @__PURE__ */ l.createElement(l.Suspense, {
      fallback: /* @__PURE__ */ l.createElement(pe, {
        set: ne
      })
    }, e)))));
  }), l.useEffect(() => {
    const R = p.current;
    if (R)
      return () => me(R);
  }, []);
  const se = u ? "none" : "auto";
  return /* @__PURE__ */ l.createElement("div", Q({
    ref: V,
    style: {
      position: "relative",
      width: "100%",
      height: "100%",
      overflow: "hidden",
      pointerEvents: se,
      ...o
    }
  }, S), /* @__PURE__ */ l.createElement("div", {
    ref: j,
    style: {
      width: "100%",
      height: "100%"
    }
  }, /* @__PURE__ */ l.createElement("canvas", {
    ref: p,
    style: {
      display: "block"
    }
  }, n)));
}), Ve = /* @__PURE__ */ l.forwardRef(function(e, n) {
  return /* @__PURE__ */ l.createElement(re, null, /* @__PURE__ */ l.createElement(Ie, Q({}, e, {
    ref: n
  })));
}), qe = ({
  animation: t,
  modelUrl: e,
  onAudioEnd: n,
  aside: r,
  speakCount: o = 0,
  emotion: s,
  zoom: c,
  position: u,
  loadingText: i,
  modelConfigs: d,
  currentVrm: f,
  setCurrentVrm: h,
  onProgressChange: w,
  onLoadErr: E,
  scale: m
}) => {
  const [v, g] = B(0), [x, a] = B();
  return C(() => {
    w && w(v);
  }, [v]), /* @__PURE__ */ N(
    "div",
    {
      className: "virtual-lg:rounded-3xl virtual-flex virtual-relative virtual-items-center virtual-justify-center virtual-h-full virtual-w-full",
      children: [
        /* @__PURE__ */ N(
          "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 === "" || v >= 100 ? "virtual-fadeOut" : ""}`,
            children: [
              /* @__PURE__ */ b(
                "p",
                {
                  className: "virtual-font-wenhei virtual-text-lg virtual-text-white virtual-animate-flicker virtual-text-center virtual-w-fit",
                  children: `${i ?? "Your Virtual is Dressing Up..."}`
                }
              ),
              /* @__PURE__ */ b("div", { className: "virtual-bg-white/20 virtual-h-[4px] virtual-w-[80%] virtual-rounded-3xl virtual-mb-2 virtual-mt-4", children: /* @__PURE__ */ b(
                "div",
                {
                  className: "virtual-bg-white virtual-h-[4px] virtual-rounded-3xl",
                  style: {
                    width: `${v}%`
                  }
                }
              ) })
            ]
          }
        ),
        /* @__PURE__ */ b(ge, { fallback: null, children: /* @__PURE__ */ N(
          Ve,
          {
            camera: {
              position: [0, 3, 15],
              zoom: c || 2
            },
            linear: !0,
            flat: !0,
            children: [
              !!e && /* @__PURE__ */ b(
                be,
                {
                  animation: t,
                  url: e,
                  speakCount: o,
                  onAudioEnd: n,
                  onLoad: (O) => {
                    g(O);
                  },
                  zoom: c ?? 2,
                  aside: r,
                  emotion: s,
                  position: u,
                  modelConfigs: d,
                  currentVrm: f || x,
                  setCurrentVrm: h || a,
                  onLoadErr: E,
                  scale: m
                }
              ),
              /* @__PURE__ */ b(
                "directionalLight",
                {
                  color: "#FFFFFF",
                  intensity: 1.5294117647058822,
                  position: new _e(2, 0, 3)
                }
              ),
              /* @__PURE__ */ b("ambientLight", { intensity: 1.6666666666666665, color: "#FFF" })
            ]
          }
        ) })
      ]
    }
  );
};
export {
  qe as CharacterScene
};
