import { jsx as A } from "react/jsx-runtime";
import { useState as C, useRef as y, useMemo as f, useEffect as b, useCallback as K, useContext as O, createContext as R } from "react";
import { useStreamContext as J } from "./Stream.es.js";
const v = R(void 0);
function z({ children: t }) {
  const [e, o] = C(null), s = f(() => ({ config: e, setConfig: o }), [e]);
  return /* @__PURE__ */ A(v.Provider, { value: s, children: t });
}
function w() {
  const t = O(v);
  if (!t)
    throw new Error("useSuggestionConfig must be used within a SuggestionProvider");
  return t;
}
function B(t = {}, e = []) {
  const { setConfig: o } = w(), s = f(() => JSON.stringify(t), [t.instructions, t.minSuggestions, t.maxSuggestions]), l = f(() => JSON.stringify(e), [e.length, ...e]);
  b(() => (o({
    options: t,
    deps: e,
    isEnabled: !0
  }), () => {
    o(null);
  }), [s, l]);
}
function F(t = {}) {
  const { suggestions: e } = t, { sendMessage: o } = J(), { config: s } = w(), [l, u] = C([]), p = s?.options?.instructions ?? t.instructions ?? "Suggest relevant next actions.", c = s?.options?.minSuggestions ?? t.minSuggestions ?? 2, m = s?.options?.maxSuggestions ?? t.maxSuggestions ?? 4, a = s?.options?.suggestions ?? t.suggestions, r = s?.deps ?? [], x = s?.isEnabled ?? !1, M = y(0), S = y(/* @__PURE__ */ new WeakMap()), $ = f(() => r.map((n) => {
    if (n === null) return "null";
    if (n === void 0) return "undefined";
    const g = typeof n;
    return g === "object" || g === "function" ? (S.current.has(n) || S.current.set(n, M.current++), `ref:${S.current.get(n)}`) : `${g}:${String(n)}`;
  }).join("|"), [r]);
  b(() => {
    if (!x) {
      u([]);
      return;
    }
    let n = !0;
    if (u([]), e && e.length > 0) {
      u(e);
      return;
    }
    if (a && a.length > 0) {
      u(a);
      return;
    }
    const g = [
      "Ask for clarification about the user's goals.",
      "Summarize the previous message in one sentence.",
      "Suggest next steps for implementing the feature.",
      "Ask whether they want code examples.",
      "Propose a refactor to improve readability.",
      "Offer to run tests and show results."
    ], h = [];
    if (r.length > 0) {
      const i = r[r.length - 1];
      typeof i == "string" && i.trim().length > 0 && (h.push(`Follow up on: "${i.slice(0, 120)}"`), h.push(`Ask a clarifying question about: "${i.slice(0, 80)}"`));
    }
    const E = Math.max(c, Math.min(m, Math.floor(Math.random() * (m - c + 1)) + c)), d = [...h, ...g].filter(Boolean).slice(0, E), P = setTimeout(() => {
      if (!n) return;
      const i = p ? d.map((j) => `${j}`) : d;
      u(i);
    }, 150);
    return () => {
      n = !1, clearTimeout(P);
    };
  }, [$, p, c, m, e, a, x]);
  const k = K((n) => {
    o(n, { type: "human" });
  }, [o]);
  return { suggestions: l, onSelect: k };
}
export {
  z as SuggestionProvider,
  B as default,
  B as useChatSuggestions,
  F as useGeneratedSuggestions,
  w as useSuggestionConfig
};
//# sourceMappingURL=useChatSuggestions.es.js.map
