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