import { jsx as N } from "react/jsx-runtime";
import "@langchain/langgraph-sdk";
import { useContext as S, createContext as U, useState as n, useEffect as _, useCallback as l } from "react";
import { useChatRuntime as A } from "./ChatRuntime.es.js";
import { createClient as f } from "./client.es.js";
import { logger as T } from "../utils/logger.es.js";
import E from "../node_modules/.pnpm/uuid@13.0.0/node_modules/uuid/dist/validate.es.js";
const p = U(void 0);
function M(r) {
  return E(r) ? { assistant_id: r } : { graph_id: r };
}
function H({ children: r, initialMode: g = "single" }) {
  const { apiUrl: e, assistantId: i, identity: t } = A(), [v, s] = n([]), [k, w] = n(!1), [d, c] = n(null), [C, x] = n(), [y, P] = n(g);
  _(() => {
    const a = new URLSearchParams(window.location.search).get("thread");
    a && c(a);
  }, []);
  const h = l(async () => {
    if (!e || !i) return [];
    if (!t?.authToken)
      return T.error("No authToken available for getThreads"), [];
    const a = await f(e, t?.authToken ?? void 0).threads.search({
      metadata: {
        ...M(i)
      },
      limit: 100
    });
    return Array.isArray(a) ? a : [];
  }, [e, i, t]), b = l(async (o) => {
    if (!e) return;
    if (!t?.authToken) {
      T.error("No authToken available for deleteThread");
      return;
    }
    await f(e, t?.authToken ?? void 0).threads.delete(o), s((m) => m.filter((u) => u.thread_id !== o)), d === o && c(null);
  }, [e, d, t]), L = l(async (o, a) => {
    if (!e) return;
    if (!t?.authToken) {
      T.error("No authToken available for updateThread");
      return;
    }
    await f(e, t?.authToken ?? void 0).threads.update(o, { metadata: a });
    const u = await h();
    s(u);
  }, [e, h, t]);
  return /* @__PURE__ */ N(
    p.Provider,
    {
      value: { mode: y, setMode: P, threadId: d, setThreadId: c, getThreads: h, threads: v, setThreads: s, configuration: C, setConfiguration: x, threadsLoading: k, setThreadsLoading: w, deleteThread: b, updateThread: L },
      children: r
    }
  );
}
function J() {
  const r = S(p);
  if (!r)
    throw new Error("useThread must be used within a ThreadProvider");
  return r;
}
export {
  H as ThreadProvider,
  J as useThread
};
//# sourceMappingURL=Thread.es.js.map
