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