import { useRef as T, useCallback as w, useSyncExternalStore as f, useEffect as x } from "react";
import { AbstractChat as A } from "../../../../../ai@5.0.39_zod@4.1.5/node_modules/ai/dist/index.js";
import F from "../../../../../../../_virtual/index5.js";
var S = (r, t, s) => {
  if (!t.has(r))
    throw TypeError("Cannot " + s);
}, a = (r, t, s) => (S(r, t, "read from private field"), s ? s.call(r) : t.get(r)), i = (r, t, s) => {
  if (t.has(r))
    throw TypeError("Cannot add the same private member more than once");
  t instanceof WeakSet ? t.add(r) : t.set(r, s);
}, l = (r, t, s, e) => (S(r, t, "write to private field"), t.set(r, s), s);
function G(r, t) {
  return t != null ? F(r, t) : r;
}
var c, k, C, o, d, g, n, p, M, I = class {
  constructor(r = []) {
    i(this, c, void 0), i(this, k, "ready"), i(this, C, void 0), i(this, o, /* @__PURE__ */ new Set()), i(this, d, /* @__PURE__ */ new Set()), i(this, g, /* @__PURE__ */ new Set()), this.pushMessage = (t) => {
      l(this, c, a(this, c).concat(t)), a(this, n).call(this);
    }, this.popMessage = () => {
      l(this, c, a(this, c).slice(0, -1)), a(this, n).call(this);
    }, this.replaceMessage = (t, s) => {
      l(this, c, [
        ...a(this, c).slice(0, t),
        // We deep clone the message here to ensure the new React Compiler (currently in RC) detects deeply nested parts/metadata changes:
        this.snapshot(s),
        ...a(this, c).slice(t + 1)
      ]), a(this, n).call(this);
    }, this.snapshot = (t) => structuredClone(t), this["~registerMessagesCallback"] = (t, s) => {
      const e = s ? G(t, s) : t;
      return a(this, o).add(e), () => {
        a(this, o).delete(e);
      };
    }, this["~registerStatusCallback"] = (t) => (a(this, d).add(t), () => {
      a(this, d).delete(t);
    }), this["~registerErrorCallback"] = (t) => (a(this, g).add(t), () => {
      a(this, g).delete(t);
    }), i(this, n, () => {
      a(this, o).forEach((t) => t());
    }), i(this, p, () => {
      a(this, d).forEach((t) => t());
    }), i(this, M, () => {
      a(this, g).forEach((t) => t());
    }), l(this, c, r);
  }
  get status() {
    return a(this, k);
  }
  set status(r) {
    l(this, k, r), a(this, p).call(this);
  }
  get error() {
    return a(this, C);
  }
  set error(r) {
    l(this, C, r), a(this, M).call(this);
  }
  get messages() {
    return a(this, c);
  }
  set messages(r) {
    l(this, c, [...r]), a(this, n).call(this);
  }
};
c = /* @__PURE__ */ new WeakMap();
k = /* @__PURE__ */ new WeakMap();
C = /* @__PURE__ */ new WeakMap();
o = /* @__PURE__ */ new WeakMap();
d = /* @__PURE__ */ new WeakMap();
g = /* @__PURE__ */ new WeakMap();
n = /* @__PURE__ */ new WeakMap();
p = /* @__PURE__ */ new WeakMap();
M = /* @__PURE__ */ new WeakMap();
var u, v = class extends A {
  constructor({ messages: r, ...t }) {
    const s = new I(r);
    super({ ...t, state: s }), i(this, u, void 0), this["~registerMessagesCallback"] = (e, _) => a(this, u)["~registerMessagesCallback"](e, _), this["~registerStatusCallback"] = (e) => a(this, u)["~registerStatusCallback"](e), this["~registerErrorCallback"] = (e) => a(this, u)["~registerErrorCallback"](e), l(this, u, s);
  }
};
u = /* @__PURE__ */ new WeakMap();
function D({
  experimental_throttle: r,
  resume: t = !1,
  ...s
} = {}) {
  const e = T(
    "chat" in s ? s.chat : new v(s)
  );
  ("chat" in s && s.chat !== e.current || "id" in s && e.current.id !== s.id) && (e.current = "chat" in s ? s.chat : new v(s));
  const b = "id" in s ? s.id : null, E = w(
    (h) => e.current["~registerMessagesCallback"](h, r),
    // optionsId is required to trigger re-subscription when the chat ID changes
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [r, b]
  ), m = f(
    E,
    () => e.current.messages,
    () => e.current.messages
  ), W = f(
    e.current["~registerStatusCallback"],
    () => e.current.status,
    () => e.current.status
  ), R = f(
    e.current["~registerErrorCallback"],
    () => e.current.error,
    () => e.current.error
  ), y = w(
    (h) => {
      typeof h == "function" && (h = h(e.current.messages)), e.current.messages = h;
    },
    [e]
  );
  return x(() => {
    t && e.current.resumeStream();
  }, [t, e]), {
    id: e.current.id,
    messages: m,
    setMessages: y,
    sendMessage: e.current.sendMessage,
    regenerate: e.current.regenerate,
    clearError: e.current.clearError,
    stop: e.current.stop,
    error: R,
    resumeStream: e.current.resumeStream,
    status: W,
    addToolResult: e.current.addToolResult
  };
}
export {
  v as Chat,
  D as useChat
};
//# sourceMappingURL=index.js.map
