import { jsx as r, Fragment as D, jsxs as Gt } from "react/jsx-runtime";
import { useState as m, useMemo as rt, useEffect as jt } from "react";
import { G as zt, I as nt, a as ut, C as Lt } from "../../CharacterInput-ACbpD9C7.js";
import { startLipSync as R } from "../../utils/audio.js";
import { useVirtual as Vt } from "../../hooks/useVirtual.js";
import { UNSAFE_initAccessToken as Pt } from "../../utils/initAccessToken.js";
import "../../utils/jwt.js";
import { getQuotedTexts as st } from "../../utils/string.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 s } from "../../VrmService-Dx-J8CUx.js";
import "../../index-k8aZRcq6.js";
import { CharacterScene as Dt } from "../CharacterScene/CharacterScene.js";
import { ModelConfigs as Rt } from "../ModelConfigs/ModelConfigs.js";
function dt(g) {
  return zt({ 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: [] }] })(g);
}
const Et = ({
  virtualName: g,
  userName: ct,
  hideVoice: H,
  inputClassName: $,
  inputStyle: F,
  hideInput: A,
  zoom: mt,
  position: ft,
  virtualId: N,
  onUserMessageCreated: y,
  onVirtualMessageCreated: w,
  onBeforeSendMessage: Q,
  onErrorSendingMessage: W,
  aside: vt,
  onInputFocused: h,
  onInputBlurred: S,
  initAccessToken: _,
  onAudioErr: f,
  validateMessageCapability: q,
  overrideModelUrl: J,
  transformModelUrl: K,
  onPromptError: G,
  onInitCompleted: pt,
  metadata: bt,
  loadingText: xt,
  configs: k,
  onProgressChange: O,
  onLoadErr: yt,
  scale: wt,
  showSettings: ht,
  modelConfigs: St,
  lang: X,
  sceneConfigs: kt,
  LoadingComponent: Ct,
  Input: Y,
  speakerStyle: Z,
  speakerContainerStyle: B
}) => {
  const [j, M] = m(""), [z, n] = m(s), { modelUrl: C, virtualService: L, cores: U } = Vt({
    virtualId: N,
    userName: ct,
    virtualName: g,
    initAccessToken: _ || Pt,
    onPromptError: G,
    metadata: bt,
    onInitCompleted: pt
  }), [Ut, V] = m(0), [Tt, u] = m("idle"), [a, p] = m(void 0), [I, l] = m(!1), [b, gt] = m(), T = rt(() => U.includes("llm"), [U]), E = rt(() => U.includes("tts"), [U]), [tt, At] = m(), it = (i) => {
    M(i.target.value);
  }, at = async (i, e, o) => {
    var ot, lt;
    if ((k != null && k.ttsMode || !T) && typeof i == "string") {
      try {
        const t = await L.getTTSResponse(i), c = new Audio(t);
        c.load(), await c.play();
      } catch (t) {
        G && G(t);
      }
      return;
    }
    if (l(!1), !(q ? q() : !0))
      return;
    let d = i;
    try {
      typeof d == "string" && y && await y({
        text: d
      }), Q && (d = await Q(i)), p(void 0);
      const t = await L.createPrompt(d, k);
      if (typeof d != "string" && y && await y({
        text: t.prompt
      }), t.audioUid)
        e.src = t.audioUid, await R(
          b,
          e,
          o,
          async () => {
            var c, x;
            l(!0), (c = t.body) != null && c.url && n(t.body.url), (x = t.body) != null && x.sentiment && u(
              t.body.sentiment
            ), w && await w({
              prompt: t.prompt,
              text: t.text,
              body: t.body,
              audioUid: t.audioUid
            }), p(t), V((Nt) => Nt + 1);
          },
          () => {
            l(!1), n(s), u("idle");
          },
          () => {
            f && f(), l(!1), n(s), u("idle");
          }
        );
      else {
        (ot = t.body) != null && ot.url && n(t.body.url), (lt = t.body) != null && lt.sentiment && u(
          t.body.sentiment
        ), w && await w({
          prompt: t.prompt,
          text: t.text,
          body: t.body,
          audioUid: t.audioUid
        }), p(t);
        let c = "";
        E && st(t.text ?? "").join(" ") && (c = await L.getTTSResponse(
          st(t.text ?? "").join(" ")
        )), p((x) => {
          if (x)
            return {
              ...x,
              audioUid: c
            };
        });
      }
    } catch (t) {
      n(s), u("idle"), W && W(t);
    }
  }, P = async () => {
    const i = j.trim();
    if (!i)
      return;
    M("");
    const e = new Audio(), o = new AudioContext();
    await at(i, e, o), o.createGain(), await o.resume(), await e.play();
  }, et = async (i) => {
    const e = new Audio(), o = new AudioContext();
    await at(i, e, o), o.createGain(), await o.resume(), await e.play();
  };
  return jt(() => () => {
    p(void 0);
  }, [N]), N ? /* @__PURE__ */ Gt(
    "form",
    {
      className: "virtual-justify-end virtual-h-full virtual-w-full virtual-flex virtual-flex-col virtual-overflow-y-hidden virtual-relative",
      onSubmit: (i) => {
        i.preventDefault(), P();
      },
      children: [
        ht && /* @__PURE__ */ r(
          Rt,
          {
            vrm: b,
            configs: tt,
            setConfigs: At
          }
        ),
        /* @__PURE__ */ r("div", { className: "virtual-w-full virtual-h-full virtual-relative", children: /* @__PURE__ */ r(
          Dt,
          {
            sceneConfigs: kt,
            scale: wt,
            LoadingComponent: Ct,
            onProgressChange: (i) => {
              i < 100 && (n(s), u("idle")), O && O(i);
            },
            onLoadErr: yt,
            zoom: mt,
            animation: z,
            loadingText: xt,
            currentVrm: b,
            setCurrentVrm: gt,
            modelUrl: J || (K ? K(C) : C),
            speakCount: Ut,
            onAudioEnd: () => {
              n(s), u("idle");
            },
            aside: vt,
            emotion: Tt,
            position: ft,
            modelConfigs: tt ?? St
          }
        ) }),
        !A && (T || E) && (Y ? /* @__PURE__ */ r(
          Y,
          {
            lang: X,
            value: j,
            onChange: it,
            onSubmit: P,
            disabled: z === "think",
            onSubmitVoice: et,
            hideVoice: H || !T,
            onFocus: () => {
              h && h();
            },
            onBlur: () => {
              S && S();
            },
            className: $,
            style: F,
            Toolbar: a != null && a.audioUid && !A ? /* @__PURE__ */ r(
              nt,
              {
                "aria-label": "Play / Pause",
                icon: /* @__PURE__ */ r(
                  ut,
                  {
                    as: dt,
                    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: B,
                isDisabled: !!C && I || !a.audioUid,
                onClick: async () => {
                  if (l(!0), !a.audioUid) {
                    l(!1);
                    return;
                  }
                  const i = new Audio(
                    `${a.audioUid ?? ""}`
                  ), e = new AudioContext();
                  await R(
                    b,
                    i,
                    e,
                    () => {
                      var o, v;
                      V((d) => d + 1), n(((o = a.body) == null ? void 0 : o.url) ?? s), u(
                        ((v = a.body) == null ? void 0 : v.sentiment) ?? "idle"
                      );
                    },
                    () => {
                      l(!1), n(s), u("idle");
                    },
                    () => {
                      f && f(), l(!1);
                    }
                  ), e.createGain(), await e.resume(), i.preload = "metadata", i.play();
                }
              }
            ) : /* @__PURE__ */ r(D, {})
          }
        ) : /* @__PURE__ */ r(
          Lt,
          {
            lang: X,
            value: j,
            onChange: it,
            onSubmit: P,
            disabled: z === "think",
            onSubmitVoice: et,
            hideVoice: H || !T,
            onFocus: () => {
              h && h();
            },
            onBlur: () => {
              S && S();
            },
            className: $,
            style: F,
            Toolbar: a != null && a.audioUid && !A ? /* @__PURE__ */ r(
              nt,
              {
                "aria-label": "Play / Pause",
                icon: /* @__PURE__ */ r(
                  ut,
                  {
                    as: dt,
                    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: B,
                isDisabled: !!C && I || !a.audioUid,
                onClick: async () => {
                  if (l(!0), !a.audioUid) {
                    l(!1);
                    return;
                  }
                  const i = new Audio(
                    `${a.audioUid ?? ""}`
                  ), e = new AudioContext();
                  await R(
                    b,
                    i,
                    e,
                    () => {
                      var o, v;
                      V((d) => d + 1), n(((o = a.body) == null ? void 0 : o.url) ?? s), u(
                        ((v = a.body) == null ? void 0 : v.sentiment) ?? "idle"
                      );
                    },
                    () => {
                      l(!1), n(s), u("idle");
                    },
                    () => {
                      f && f(), l(!1);
                    }
                  ), e.createGain(), await e.resume(), i.preload = "metadata", i.play();
                }
              }
            ) : /* @__PURE__ */ r(D, {})
          }
        ))
      ]
    }
  ) : /* @__PURE__ */ r(D, {});
};
export {
  Et as CharacterRoom
};
