import { K as M, T as _, j as r, s as $, c as b, p as F, C as N, a as G, I as q, b as z, m as X, d as Y, R as E, e as P, S as C, f as y, N as A, U as Z, o as R, g as J, h as K, i as Q, L as w, k as j, l as ee, w as se, n as te, q as ae, r as oe, P as ne } from "./styles.css-RZgqP4wo.mjs";
import { A as fe, ab as ve, a0 as Ce, Y as ye, V as Ee, Z as xe, t as Ae, B as Re, u as we, v as je, a7 as Te, a6 as Ie, a5 as Me, x as be, $ as Ne, D as Pe, ac as Le, a9 as ke, a8 as De, F as Ue, H as Oe, y as Be, X as He, a3 as We, z as Ve, M as _e, E as $e, G as Fe, a1 as Ge, ad as qe, ae as ze, aa as Xe, _ as Ye, J as Ze, O as Je, Q as Ke, a4 as Qe, a2 as es, W as ss, am as ts, an as as, af as os, ag as ns, ai as rs, ak as is, aj as cs, al as ls, ah as hs } from "./styles.css-RZgqP4wo.mjs";
import { useState as p, useMemo as L, useEffect as S, useContext as m, useCallback as T, createElement as re } from "react";
const ie = ({ side: e, messages: s }) => {
  const [t, a] = p(!1), o = L(
    () => s.map(
      (n, c) => M(n).with({ type: _.TEXT }, ({ payload: l }) => /* @__PURE__ */ r.jsx("div", { className: $, children: String(l.message) }, c)).otherwise(() => null)
    ),
    [s]
  );
  return S(() => {
    o.length && a(!1);
  }, [o]), t || !o.length ? null : /* @__PURE__ */ r.jsxs("div", { className: b(N.PROACTIVE, F({ side: e })), children: [
    /* @__PURE__ */ r.jsx("div", { className: G, onClick: () => a(!0), children: /* @__PURE__ */ r.jsx(q, { className: z, svg: "closeV2" }) }),
    /* @__PURE__ */ r.jsx("div", { className: X, children: o })
  ] });
}, I = (e) => {
  const s = (t) => () => console.warn(e(t));
  return {
    open: s("open"),
    hide: s("hide"),
    show: s("show"),
    close: s("close"),
    interact: s("interact"),
    proactive: {
      clear: s("proactive.clear"),
      push: s("proactive.push")
    }
  };
}, ce = (e) => !!e && !Array.isArray(e) && typeof e == "object", k = (e, s, t) => S(() => {
  if (!ce(e))
    return;
  const a = I((n) => `Method '${n}' has no effect in this mode.`), o = s();
  return Object.assign(e, {
    ...a,
    ...o,
    proactive: {
      ...a.proactive,
      ...o.proactive
    }
  }), t == null || t(), () => {
    const n = I(
      (c) => `Method '${c}' has no effect after 'destroy' has been called. Call 'load' to render the chat and restore this method.`
    );
    Object.assign(e, n);
  };
}, []), D = (e) => {
  const [s, t] = p();
  return S(() => {
    e != null && e.color && t(Y(e.color));
  }, [e == null ? void 0 : e.color]), s;
}, le = async (e, s) => {
  const t = document.createElement("link"), a = new Promise((o, n) => {
    t.onload = o, t.onerror = n;
  });
  t.rel = "stylesheet", t.href = e, s.appendChild(t), await a;
}, U = (e, s) => {
  const [t, a] = p(!1);
  return S(() => {
    if (!e || t)
      return;
    if (!e.stylesheet) {
      a(!0);
      return;
    }
    const o = Array.isArray(e.stylesheet) ? e.stylesheet[0] : e.stylesheet;
    (async () => (await le(o, s ?? document.head).catch((n) => {
      console.error(`failed to load stylesheet: ${e.stylesheet}`), console.error(n);
    }), a(!0)))();
  }, [e]), t;
}, O = ({ isMobile: e }) => {
  const s = m(E), t = m(P), { assistant: a, config: o } = s, n = D(a), c = T(() => {
    s.setStatus(C.ENDED), s.stopChat();
  }, []), l = T(
    (u) => {
      const i = t.session.turns[u - 1];
      return (i == null ? void 0 : i.type) === y.USER ? i : null;
    },
    [t.session.turns]
  );
  return n ? /* @__PURE__ */ r.jsx(A.Container, { isMobile: e, palette: n, children: /* @__PURE__ */ r.jsxs(
    A,
    {
      title: a.title,
      description: a.description,
      image: a.image,
      avatar: a.avatar,
      showPoweredBy: a.watermark,
      startTime: t.session.startTime,
      hasEnded: s.isStatus(C.ENDED),
      isLoading: s.isStatus(C.IDLE) && t.session.turns.length === 0 && o.autostart,
      onStart: s.launch,
      onEnd: c,
      onSend: s.reply,
      onMinimize: s.close,
      audioInterface: a.audioInterface,
      messageInputProps: {
        onSubmit: s.reply,
        audioInterface: a.audioInterface
      },
      isMobile: e,
      children: [
        t.session.turns.map((u, i) => M(u).with({ type: y.USER }, ({ id: d, ...h }) => /* @__PURE__ */ re(
          Z,
          {
            ...R(h, ["type"]),
            key: d,
            isLast: i === t.session.turns.length - 1
          }
        )).with({ type: y.SYSTEM }, ({ id: d, ...h }) => /* @__PURE__ */ r.jsx(
          J,
          {
            ...R(h, ["type"]),
            avatar: a.avatar,
            feedback: {
              onClick: (g) => {
                s.feedback(g, h.messages, l(i));
              }
            },
            isLast: i === t.session.turns.length - 1
          },
          d
        )).exhaustive()),
        t.indicator && /* @__PURE__ */ r.jsx("div", { className: K, children: /* @__PURE__ */ r.jsx(Q, { avatar: a.avatar, isLast: !0 }) })
      ]
    }
  ) }) : null;
}, pe = ({ shadowRoot: e, chatAPI: s, ready: t }) => {
  const { assistant: a, interact: o, open: n } = m(E), c = U(a, e);
  return k(
    s,
    () => (console.info("Methods open, close, hide, show, proactive.clear, proactive.push have no effect in this mode."), { interact: o }),
    t
  ), S(() => {
    n();
  }, []), c ? /* @__PURE__ */ r.jsx(O, {}) : null;
}, me = ({ shadowRoot: e, chatAPI: s, ready: t }) => {
  const { assistant: a, open: o, close: n, interact: c } = m(E), { isOpen: l } = m(P), [u, i] = p(!1), [d, h] = p([]), g = L(() => window.matchMedia("(max-width: 768px)").matches, []), x = D(a), B = () => {
    l ? n() : o();
  };
  k(
    s,
    () => ({
      open: o,
      close: n,
      hide: () => i(!0),
      show: () => i(!1),
      interact: c,
      proactive: {
        clear: () => h([]),
        push: (...W) => h((V) => [...V, ...W])
      }
    }),
    t
  );
  const f = a.position, v = { bottom: a.spacing.bottom, [f]: a.spacing.side }, H = `calc(100% - ${w + j + a.spacing.bottom + 20}px)`;
  return !U(a, e) || !x ? null : /* @__PURE__ */ r.jsxs(
    "div",
    {
      style: ee(ne, { colors: x }),
      className: b(N.WIDGET, se({ hidden: u, withChat: l })),
      children: [
        /* @__PURE__ */ r.jsxs("div", { className: te, style: v, children: [
          /* @__PURE__ */ r.jsx(ie, { side: f, messages: d }),
          /* @__PURE__ */ r.jsx(ae, { onClick: B, isOpen: l, image: a.launcher })
        ] }),
        /* @__PURE__ */ r.jsx(
          "div",
          {
            className: oe,
            style: g ? {} : { [f]: v[f], bottom: v.bottom + w + j, height: H },
            children: /* @__PURE__ */ r.jsx(O, { isMobile: g })
          }
        )
      ]
    }
  );
};
export {
  fe as AgentMessage,
  ve as AnyExtension,
  Ce as AssistantOptions,
  ye as AutoScrollConsumer,
  Ee as AutoScrollContext,
  xe as AutoScrollProvider,
  Ae as Avatar,
  Re as Button,
  we as Card,
  je as Carousel,
  Te as ChatConfig,
  pe as ChatEmbed,
  Ie as ChatSpeechRecognitionConfig,
  Me as ChatSpeechRecognitionState,
  me as ChatWidget,
  O as ChatWindow,
  be as ColorTheme,
  Ne as DEFAULT_AVATAR,
  Pe as DebugMessage,
  Le as EMBEDDED_TARGET,
  ke as EffectExtension,
  De as ExtensionType,
  Ue as FeedbackButton,
  Oe as Header,
  q as Icon,
  Be as Image,
  He as IsAutoScrollingContext,
  We as LaunchOptions,
  ae as Launcher,
  Ve as LinkPreview,
  _e as MessageInput,
  A as NewChat,
  $e as NewFooter,
  ie as Proactive,
  Fe as Prompt,
  Ge as RUNTIME_URL,
  qe as RenderMode,
  ze as RenderOptions,
  Xe as ResponseExtension,
  Ye as RuntimeProvider,
  E as RuntimeStateAPIContext,
  P as RuntimeStateContext,
  Ze as SendButton,
  J as SystemResponse,
  Je as Tooltip,
  y as TurnType,
  Ke as TypingIndicator,
  Qe as UserOptions,
  Z as UserResponse,
  es as VerifyOptions,
  ss as WelcomeMessage,
  ts as chain,
  as as noop,
  os as useAutoScroll,
  k as useChatAPI,
  ns as useDidUpdateEffect,
  rs as useLocalStorage,
  is as useLocalStorageState,
  D as usePalette,
  cs as useSessionStorage,
  ls as useSessionStorageState,
  hs as useStateRef
};
