import { jsx as e, Fragment as D, jsxs as Ut } from "react/jsx-runtime";
import { useState as m, useMemo as et, useEffect as Vt } from "react";
import { G as Nt, I as lt, a as rt, C as At } from "../../CharacterInput-ACbpD9C7.js";
import { startLipSync as zt } from "../../utils/audio.js";
import { useVirtual as Rt } from "../../hooks/useVirtual.js";
import "../../utils/jwt.js";
import "../../three-vrm.module--Wx-EJJ8.js";
import "../../utils/vendors/models/loaders/vmdtovrmbinding.js";
import "../../utils/vendors/models/vrm-ik-handler.js";
import "../../utils/vendors/models/vrm-model-noise.js";
import { i as u } from "../../VrmService-XXqyFAJn.js";
import "../../utils/vendors/models/loaders/bvhtovrmbinding.js";
import "../../index-k8aZRcq6.js";
import { CharacterScene as Lt } from "../CharacterScene/CharacterScene.js";
import { ModelConfigs as Pt } from "../ModelConfigs/ModelConfigs.js";
function ot(T) {
  return Nt({ tag: "svg", attr: { viewBox: "0 0 24 24", fill: "currentColor", "aria-hidden": "true" }, child: [{ tag: "path", attr: { d: "M13.5 4.06c0-1.336-1.616-2.005-2.56-1.06l-4.5 4.5H4.508c-1.141 0-2.318.664-2.66 1.905A9.76 9.76 0 001.5 12c0 .898.121 1.768.35 2.595.341 1.24 1.518 1.905 2.659 1.905h1.93l4.5 4.5c.945.945 2.561.276 2.561-1.06V4.06zM18.584 5.106a.75.75 0 011.06 0c3.808 3.807 3.808 9.98 0 13.788a.75.75 0 11-1.06-1.06 8.25 8.25 0 000-11.668.75.75 0 010-1.06z" }, child: [] }, { tag: "path", attr: { d: "M15.932 7.757a.75.75 0 011.061 0 6 6 0 010 8.486.75.75 0 01-1.06-1.061 4.5 4.5 0 000-6.364.75.75 0 010-1.06z" }, child: [] }] })(T);
}
const _t = ({
  virtualName: T,
  userName: ut,
  hideVoice: G,
  inputClassName: H,
  inputStyle: O,
  hideInput: U,
  zoom: nt,
  position: dt,
  virtualId: V,
  onUserMessageCreated: p,
  onVirtualMessageCreated: W,
  onBeforeSendMessage: $,
  aside: ct,
  onInputFocused: b,
  onInputBlurred: h,
  initAccessToken: st,
  onAudioError: w,
  validateMessageCapability: q,
  overrideModelUrl: F,
  transformModelUrl: J,
  onPromptError: N,
  onInitCompleted: mt,
  metadata: vt,
  loadingText: ft,
  configs: l,
  onProgressChange: K,
  onLoadError: Q,
  scale: pt,
  showSettings: bt,
  modelConfigs: ht,
  lang: X,
  sceneConfigs: wt,
  renderLoadingComponent: xt,
  Input: Y,
  speakerStyle: Z,
  speakerContainerStyle: _,
  intensity: yt
}) => {
  const [A, E] = m(""), [z, n] = m(u), { modelUrl: M, virtualService: v, cores: x } = Rt({
    virtualId: V,
    userName: ut,
    virtualName: T,
    initAccessToken: st,
    onPromptError: N,
    metadata: vt,
    onInitCompleted: mt,
    onLoadError: Q
  }), [St, R] = m(0), [kt, d] = m("idle"), [a, y] = m(void 0), [L, Ct] = m(), P = et(() => x.includes("llm"), [x]), I = et(() => x.includes("tts"), [x]), [B, Tt] = m(), g = (t) => {
    E(t.target.value);
  }, tt = async (t) => {
    var s, f, k, at;
    if ((l != null && l.ttsMode || !P && I) && typeof t == "string") {
      try {
        if (!v)
          throw new Error("Virtual Service not available.");
        const i = await v.getTTSResponse(t), o = new Audio(i);
        o.load(), await o.play();
      } catch {
      }
      return;
    }
    if (!(q ? q() : !0))
      return;
    let r = t;
    try {
      if (typeof r == "string" && p && await p({
        text: r
      }), $ && (r = await $(t)), y(void 0), !v)
        throw new Error("Virtual Service not available.");
      const i = await v.createPrompt(r, l);
      if (typeof r != "string" && p && await p({
        text: i.prompt
      }), i.audioUid)
        l != null && l.speakOnResponse ? await S(i) : (R((o) => o + 1), n(((s = i.body) == null ? void 0 : s.url) ?? u), d(
          ((f = i.body) == null ? void 0 : f.sentiment) ?? "idle"
        ));
      else {
        W && await W({
          prompt: i.prompt,
          text: i.text,
          body: i.body,
          audioUid: i.audioUid
        }), y(i);
        let o = "";
        I && i.text && (o = await v.getTTSResponse(i.text)), y((C) => {
          if (C)
            return {
              ...C,
              audioUid: o
            };
        }), l != null && l.speakOnResponse ? await S({ ...i, audioUid: o }) : (R((C) => C + 1), n(((k = i.body) == null ? void 0 : k.url) ?? u), d(
          ((at = i.body) == null ? void 0 : at.sentiment) ?? "idle"
        ));
      }
    } catch (i) {
      N && N(i), n(u), d("idle");
    }
  }, S = async (t) => {
    try {
      if (!(t != null && t.audioUid))
        return;
      const c = new Audio(`${t.audioUid ?? ""}`), r = new AudioContext();
      await zt(
        L,
        c,
        r,
        () => {
          var s, f;
          R((k) => k + 1), n(((s = t.body) == null ? void 0 : s.url) ?? u), d(
            ((f = t.body) == null ? void 0 : f.sentiment) ?? "idle"
          );
        },
        () => {
          n(u), d("idle");
        },
        (s) => {
          w && w(s);
        }
      ), await r.resume(), c.preload = "metadata", await c.play();
    } catch (c) {
      console.log("play audio error", c), w && w(c);
    }
  }, j = async () => {
    const t = A.trim();
    t && (E(""), await tt(t));
  }, it = async (t) => {
    await tt(t);
  };
  return Vt(() => () => {
    y(void 0);
  }, [V]), V ? /* @__PURE__ */ Ut(
    "form",
    {
      className: "virtual-justify-end virtual-h-full virtual-w-full virtual-flex virtual-flex-col virtual-overflow-y-hidden virtual-relative",
      onSubmit: (t) => {
        t.preventDefault(), j();
      },
      children: [
        bt && /* @__PURE__ */ e(
          Pt,
          {
            vrm: L,
            configs: B,
            setConfigs: Tt
          }
        ),
        /* @__PURE__ */ e("div", { className: "virtual-w-full virtual-h-full virtual-relative", children: /* @__PURE__ */ e(
          Lt,
          {
            intensity: yt,
            sceneConfigs: wt,
            scale: pt,
            renderLoadingComponent: xt,
            onProgressChange: (t) => {
              t < 100 && (n(u), d("idle")), K && K(t);
            },
            onLoadError: Q,
            zoom: nt,
            animation: z,
            loadingText: ft,
            currentVrm: L,
            setCurrentVrm: Ct,
            modelUrl: F || (J ? J(M) : M),
            speakCount: St,
            onAudioEnd: () => {
              n(u), d("idle");
            },
            aside: ct,
            emotion: kt,
            position: dt,
            modelConfigs: B ?? ht
          }
        ) }),
        !U && (Y ? /* @__PURE__ */ e(
          Y,
          {
            lang: X,
            value: A,
            onChange: g,
            onSubmit: j,
            disabled: z === "think",
            onSubmitVoice: it,
            hideVoice: G || !P,
            onFocus: () => {
              b && b();
            },
            onBlur: () => {
              h && h();
            },
            className: H,
            style: O,
            Toolbar: a != null && a.audioUid && !U ? /* @__PURE__ */ e(
              lt,
              {
                "aria-label": "Play / Pause",
                icon: /* @__PURE__ */ e(
                  rt,
                  {
                    as: ot,
                    className: "!virtual-text-white !virtual-text-xl",
                    sx: Z
                  }
                ),
                className: "!virtual-rounded-full !virtual-w-10 !virtual-h-10 !virtual-bg-black/30 hover:!virtual-bg-black/30 !virtual-backdrop-blur-xl !virtual-z-40 !virtual-self-end",
                sx: _,
                isDisabled: !a.audioUid,
                onClick: async () => {
                  a != null && a.audioUid && await S(a);
                }
              }
            ) : /* @__PURE__ */ e(D, {})
          }
        ) : /* @__PURE__ */ e(
          At,
          {
            lang: X,
            value: A,
            onChange: g,
            onSubmit: j,
            disabled: z === "think",
            onSubmitVoice: it,
            hideVoice: G || !P,
            onFocus: () => {
              b && b();
            },
            onBlur: () => {
              h && h();
            },
            className: H,
            style: O,
            Toolbar: a != null && a.audioUid && !U ? /* @__PURE__ */ e(
              lt,
              {
                "aria-label": "Play / Pause",
                icon: /* @__PURE__ */ e(
                  rt,
                  {
                    as: ot,
                    className: "!virtual-text-white !virtual-text-xl",
                    sx: Z
                  }
                ),
                className: "!virtual-rounded-full !virtual-w-10 !virtual-h-10 !virtual-bg-black/30 hover:!virtual-bg-black/30 !virtual-backdrop-blur-xl !virtual-z-40 !virtual-self-end",
                sx: _,
                isDisabled: !a.audioUid,
                onClick: async () => {
                  a != null && a.audioUid && await S(a);
                }
              }
            ) : /* @__PURE__ */ e(D, {})
          }
        ))
      ]
    }
  ) : /* @__PURE__ */ e(D, {});
};
export {
  _t as CharacterRoom
};
