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