import { o as T } from "./rolldown-runtime.js";
import { i as E, n as j, t as I } from "./vendor/react.js";
import { t as y } from "./proxy.js";
import { t as w } from "./AnimatePresence.js";
import { t as S } from "./utils.js";
var s = /* @__PURE__ */ T(E(), 1), n = I();
function A({ textStream: e, speed: t = 50, onComplete: r }) {
  const [i, a] = s.useState(""), [h, o] = s.useState(!1), [p, m] = s.useState(!1), c = s.useRef(0), u = s.useRef(""), _ = s.useMemo(() => 100 - (t - 1) / 99 * 95, [t]), N = s.useCallback(() => {
    a(""), o(!1), m(!1), c.current = 0, u.current = "";
  }, []);
  return s.useEffect(() => {
    if (typeof e == "string") {
      u.current = e, o(!0), c.current = 0;
      const l = setInterval(() => {
        c.current < u.current.length ? (a(u.current.slice(0, c.current + 1)), c.current++) : (clearInterval(l), o(!1), m(!0), r?.());
      }, _);
      return () => clearInterval(l);
    }
    let f = !1;
    return o(!0), (async () => {
      try {
        for await (const l of e) {
          if (f) break;
          u.current += l, a(u.current);
        }
        f || (o(!1), m(!0), r?.());
      } catch (l) {
        console.error("Stream error:", l), o(!1);
      }
    })(), () => {
      f = !0;
    };
  }, [
    e,
    _,
    r
  ]), {
    displayedText: i,
    isStreaming: h,
    isComplete: p,
    reset: N
  };
}
function k({ text: e, className: t }) {
  return /* @__PURE__ */ (0, n.jsxs)("span", {
    className: t,
    children: [e, /* @__PURE__ */ (0, n.jsx)(y.span, {
      className: "inline-block w-0.5 h-[1em] bg-muted ms-0.5 align-text-bottom motion-reduce:animate-none motion-reduce:opacity-100",
      animate: { opacity: [1, 0] },
      transition: {
        duration: 0.5,
        repeat: Number.POSITIVE_INFINITY
      },
      "aria-hidden": "true"
    })]
  });
}
k.displayName = "TypewriterText";
function g({ text: e, className: t }) {
  return /* @__PURE__ */ (0, n.jsx)("span", {
    className: t,
    children: /* @__PURE__ */ (0, n.jsx)(w, {
      mode: "popLayout",
      children: e.split(" ").map((r, i) => /* @__PURE__ */ (0, n.jsx)(y.span, {
        initial: {
          opacity: 0,
          y: 5
        },
        animate: {
          opacity: 1,
          y: 0
        },
        transition: {
          duration: 0.2,
          delay: i * 0.05
        },
        className: "inline-block me-1 motion-reduce:!opacity-100 motion-reduce:!transform-none",
        children: r
      }, `${i}-${r}`))
    })
  });
}
g.displayName = "FadeText";
function x({ textStream: e, mode: t = "typewriter", speed: r = 50, onComplete: i, showCursor: a = !0, className: h }) {
  const { displayedText: o, isStreaming: p, isComplete: m } = A({
    textStream: e,
    speed: r,
    onComplete: i
  }), c = a && p && t === "typewriter";
  return /* @__PURE__ */ (0, n.jsxs)("div", {
    "data-slot": "ai-response-stream",
    role: "status",
    "aria-live": "polite",
    "aria-busy": p,
    className: S("text-base text-foreground", h),
    children: [t === "typewriter" ? /* @__PURE__ */ (0, n.jsxs)(n.Fragment, { children: [/* @__PURE__ */ (0, n.jsx)("span", { children: o }), c && /* @__PURE__ */ (0, n.jsx)(y.span, {
      className: "inline-block w-0.5 h-[1em] bg-muted-foreground ms-0.5 align-text-bottom motion-reduce:animate-none motion-reduce:opacity-100",
      animate: { opacity: [1, 0] },
      transition: {
        duration: 0.5,
        repeat: Number.POSITIVE_INFINITY
      },
      "aria-hidden": "true"
    })] }) : /* @__PURE__ */ (0, n.jsx)(g, { text: o }), m && !p && /* @__PURE__ */ (0, n.jsx)("span", {
      className: "sr-only",
      children: "Streaming complete"
    })]
  });
}
x.displayName = "AIResponseStream";
var O = j();
function P(e) {
  return { onComplete: (t) => {
    e.dispatchEvent(new CustomEvent("complete", {
      detail: t,
      bubbles: !0,
      composed: !0
    }));
  } };
}
function R(e) {
  const { _hostElement: t, ...r } = e, i = s.useMemo(() => t ? P(t) : {}, [t]);
  return s.createElement(x, {
    ...r,
    ...i
  });
}
var d = {
  textStream: "string",
  mode: "string",
  speed: "number",
  showCursor: "boolean",
  className: "string"
};
function b(e, t) {
  if (e !== null)
    switch (t) {
      case "number":
        return Number(e);
      case "boolean":
        return e !== "false" && e !== "0";
      case "json":
        try {
          return JSON.parse(e);
        } catch {
          return e;
        }
      default:
        return e;
    }
}
var C = class extends HTMLElement {
  _root = null;
  _childrenContainer = null;
  _props = {};
  _initialized = !1;
  connectedCallback() {
    setTimeout(() => this._initialize(), 0);
  }
  _initialize() {
    if (this._initialized) return;
    this._initialized = !0;
    const e = [];
    for (const r of Array.from(this.childNodes))
      r.nodeType === Node.TEXT_NODE && !r.textContent?.trim() || e.push(r);
    const t = document.createElement("div");
    for (t.style.display = "contents", this._childrenContainer = document.createElement("div"), this._childrenContainer.style.display = "contents", e.forEach((r) => this._childrenContainer.appendChild(r)); this.firstChild; ) this.removeChild(this.firstChild);
    this.appendChild(t), this._root = (0, O.createRoot)(t), this._render();
  }
  disconnectedCallback() {
    this._root?.unmount(), this._root = null;
  }
  attributeChangedCallback(e, t, r) {
    this._render();
  }
  static get observedAttributes() {
    return Object.keys(d).map((e) => e.replace(/([A-Z])/g, "-$1").toLowerCase());
  }
  _setProp(e, t) {
    const r = d[e] || "string";
    this._props[e] = this._parseValue(t, r), this._render();
  }
  _parseValue(e, t) {
    if (t === "boolean" && typeof e == "boolean" || t === "number" && typeof e == "number" || t === "string" && typeof e == "string") return e;
    if (typeof e == "string") switch (t) {
      case "boolean":
        return e !== "false" && e !== "0" && e !== "";
      case "number":
        return Number(e);
      case "json":
        try {
          return JSON.parse(e);
        } catch {
          return e;
        }
      default:
        return e;
    }
    return e;
  }
  _getProp(e) {
    if (e in this._props) return this._props[e];
    const t = e.replace(/([A-Z])/g, "-$1").toLowerCase(), r = d[e] || "string";
    return b(this.getAttribute(t), r);
  }
  _render() {
    if (!this._root) return;
    const e = {};
    for (const [r, i] of Object.entries(d)) if (r in this._props) e[r] = this._props[r];
    else {
      const a = r.replace(/([A-Z])/g, "-$1").toLowerCase();
      e[r] = b(this.getAttribute(a), i);
    }
    const t = s.createElement(R, {
      ...e,
      _hostElement: this
    });
    this._root.render(t);
  }
};
for (const e of Object.keys(d)) Object.defineProperty(C.prototype, e, {
  get() {
    return this._getProp(e);
  },
  set(t) {
    this._setProp(e, t);
  },
  enumerable: !0,
  configurable: !0
});
typeof customElements < "u" && !customElements.get("hf-ai-response-stream") && customElements.define("hf-ai-response-stream", C);
export {
  C as AIResponseStreamWC,
  C as default
};

//# sourceMappingURL=hf-ai-response-stream.js.map