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