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