import { K as b, T as F, j as r, s as $, c as M, p as G, C as I, a as q, I as z, b as X, m as Y, d as Z, R as E, e as N, S as y, f as C, g as P, h as J, N as K, U as Q, o as A, i as ee, k as se, P as L, L as w, l as R, w as te, n as ae, q as oe, r as ne } from "./styles.css-DWQk1uxg.mjs";
import { A as fe, ab as ve, a0 as ye, Y as Ce, V as Ee, Z as xe, t as Ae, B as we, u as Re, v as Te, a7 as je, a6 as be, a5 as Me, x as Ie, $ 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 Fe, G as $e, 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-DWQk1uxg.mjs";
import { useState as p, useMemo as k, 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 = k(
    () => s.map(
      (n, c) => b(n).with({ type: F.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: M(I.PROACTIVE, G({ side: e })), children: [
    /* @__PURE__ */ r.jsx("div", { className: q, onClick: () => a(!0), children: /* @__PURE__ */ r.jsx(z, { className: X, svg: "closeV2" }) }),
    /* @__PURE__ */ r.jsx("div", { className: Y, children: o })
  ] });
}, j = (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", D = (e, s, t) => S(() => {
  if (!ce(e))
    return;
  const a = j((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 = j(
      (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);
  };
}, []), U = (e) => {
  const [s, t] = p();
  return S(() => {
    e != null && e.color && t(Z(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;
}, O = (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;
}, B = ({ isMobile: e }) => {
  const s = m(E), t = m(N), { assistant: a, config: o } = s, n = U(a), c = T(() => {
    s.setStatus(y.ENDED), s.stopChat();
  }, []), l = T(
    (u) => {
      const i = t.session.turns[u - 1];
      return (i == null ? void 0 : i.type) === C.USER ? i : null;
    },
    [t.session.turns]
  );
  return n ? /* @__PURE__ */ r.jsx("div", { style: P(L, { colors: n }), className: J({ mobile: e }), children: /* @__PURE__ */ r.jsxs(
    K,
    {
      title: a.title,
      description: a.description,
      image: a.image,
      avatar: a.avatar,
      showPoweredBy: a.watermark,
      startTime: t.session.startTime,
      hasEnded: s.isStatus(y.ENDED),
      isLoading: s.isStatus(y.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
      },
      isMobile: e,
      children: [
        t.session.turns.map((u, i) => b(u).with({ type: C.USER }, ({ id: d, ...h }) => /* @__PURE__ */ re(Q, { ...A(h, ["type"]), key: d })).with({ type: C.SYSTEM }, ({ id: d, ...h }) => /* @__PURE__ */ r.jsx(
          ee,
          {
            ...A(h, ["type"]),
            avatar: a.avatar,
            isFirst: i === 0,
            feedback: {
              onClick: (g) => {
                s.feedback(g, h.messages, l(i));
              }
            },
            isLast: i === t.session.turns.length - 1
          },
          d
        )).exhaustive()),
        t.indicator && /* @__PURE__ */ r.jsx(se, { avatar: a.avatar })
      ]
    }
  ) }) : null;
}, pe = ({ shadowRoot: e, chatAPI: s, ready: t }) => {
  const { assistant: a, interact: o, open: n } = m(E), c = O(a, e);
  return D(
    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(B, {}) : null;
}, me = ({ shadowRoot: e, chatAPI: s, ready: t }) => {
  const { assistant: a, open: o, close: n, interact: c } = m(E), { isOpen: l } = m(N), [u, i] = p(!1), [d, h] = p([]), g = k(() => window.matchMedia("(max-width: 768px)").matches, []), x = U(a), H = () => {
    l ? n() : o();
  };
  D(
    s,
    () => ({
      open: o,
      close: n,
      hide: () => i(!0),
      show: () => i(!1),
      interact: c,
      proactive: {
        clear: () => h([]),
        push: (...V) => h((_) => [..._, ...V])
      }
    }),
    t
  );
  const f = a.position, v = { bottom: a.spacing.bottom, [f]: a.spacing.side }, W = `calc(100% - ${w + R + a.spacing.bottom + 20}px)`;
  return !O(a, e) || !x ? null : /* @__PURE__ */ r.jsxs(
    "div",
    {
      style: P(L, { colors: x }),
      className: M(I.WIDGET, te({ hidden: u, withChat: l })),
      children: [
        /* @__PURE__ */ r.jsxs("div", { className: ae, style: v, children: [
          /* @__PURE__ */ r.jsx(ie, { side: f, messages: d }),
          /* @__PURE__ */ r.jsx(oe, { onClick: H, isOpen: l, image: a.launcher })
        ] }),
        /* @__PURE__ */ r.jsx(
          "div",
          {
            className: ne,
            style: g ? {} : { [f]: v[f], bottom: v.bottom + w + R, height: W },
            children: /* @__PURE__ */ r.jsx(B, { isMobile: g })
          }
        )
      ]
    }
  );
};
export {
  fe as AgentMessage,
  ve as AnyExtension,
  ye as AssistantOptions,
  Ce as AutoScrollConsumer,
  Ee as AutoScrollContext,
  xe as AutoScrollProvider,
  Ae as Avatar,
  we as Button,
  Re as Card,
  Te as Carousel,
  je as ChatConfig,
  pe as ChatEmbed,
  be as ChatSpeechRecognitionConfig,
  Me as ChatSpeechRecognitionState,
  me as ChatWidget,
  B as ChatWindow,
  Ie 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,
  z as Icon,
  Be as Image,
  He as IsAutoScrollingContext,
  We as LaunchOptions,
  oe as Launcher,
  Ve as LinkPreview,
  _e as MessageInput,
  K as NewChat,
  Fe as NewFooter,
  ie as Proactive,
  $e as Prompt,
  Ge as RUNTIME_URL,
  qe as RenderMode,
  ze as RenderOptions,
  Xe as ResponseExtension,
  Ye as RuntimeProvider,
  E as RuntimeStateAPIContext,
  N as RuntimeStateContext,
  Ze as SendButton,
  ee as SystemResponse,
  Je as Tooltip,
  C as TurnType,
  Ke as TypingIndicator,
  Qe as UserOptions,
  Q as UserResponse,
  es as VerifyOptions,
  ss as WelcomeMessage,
  ts as chain,
  as as noop,
  os as useAutoScroll,
  D as useChatAPI,
  ns as useDidUpdateEffect,
  rs as useLocalStorage,
  is as useLocalStorageState,
  U as usePalette,
  cs as useSessionStorage,
  ls as useSessionStorageState,
  hs as useStateRef
};
