import { jsx as n, Fragment as j } from "react/jsx-runtime";
import { L as R } from "./index-LexicalComposerContext.js";
import { AIAssistantPanel as w } from "./index-AIAssistantPanel.js";
import { useAiAssistantAdjustAndGenerate as I } from "./index-useAiAssistant-GenerateAndAdjust.js";
import { useRef as E, useCallback as L, useEffect as p, useState as b } from "react";
import { createPortal as G } from "react-dom";
import { getFloatingElemPosition as V } from "./index-getRteAiAssistantPosition.js";
import g from "./index-RteAiAssistant.module.scss.js";
import { useRteAiAssistantAi as H } from "./index-useRteAiAssistant-ai.js";
import { useAiAssistantSelection as M } from "./index-useRteAiAssistant-selection.js";
const J = (x) => {
  const {
    anchorElem: i,
    keywordsToInclude: m
  } = x, [r] = R.useLexicalComposerContext(), a = E(null), s = E(null), {
    isText: o,
    isLink: d,
    selection: t,
    updatePosition: f,
    setIsMouseOver: A,
    selectionRect: l
  } = M({
    editor: r,
    anchorElem: i
  }), v = L(() => {
    const e = a.current;
    if (!e)
      return;
    if (!l || d || !o || t.length < 40) {
      e.style.pointerEvents = "none", e.style.opacity = "0", e.style.transform = "translate(-10000px, -10000px)";
      return;
    }
    const T = V(l, e, i);
    e.style.pointerEvents = "auto", e.style.opacity = "1", e.style.transform = `translate(0px, ${T.y}px)`, s.current = null;
  }, [l, t, o, d]);
  p(() => (s.current && clearTimeout(s.current), s.current = setTimeout(() => {
    v();
  }, 100), () => {
    clearTimeout(s.current);
  }), [t]);
  const [u, y] = b(t), {
    panelStatus: k,
    setPanelStatus: c,
    generateError: C,
    onGenerateClick: h,
    onAdjustClick: P
  } = I({
    generateCallback: () => new Promise((e) => e(t)),
    setValue: y,
    useHtml: !0,
    keywordsToInclude: m
  }), {
    replaceSelection: S
  } = H({
    editor: r
  });
  return p(() => {
    u !== t && S(u);
  }, [u]), p(() => {
    if (o)
      c("adjust"), r.getEditorState().read(() => {
        f();
      });
    else {
      c("closed");
      const e = a.current;
      if (e) {
        e.style.pointerEvents = "none", e.style.opacity = "0", e.style.transform = "translate(-10000px, -10000px)";
        return;
      }
      r.getEditorState().read(() => {
        f();
      });
    }
  }, [o]), n(j, {
    children: G(n("div", {
      id: "AiAssistantPlugin",
      className: g.container,
      ref: a,
      children: n("div", {
        className: g.relative,
        onMouseEnter: () => A(!0),
        onMouseLeave: () => A(!1),
        children: n(w, {
          id: "AiAssistantPlugin_Assistant",
          status: k,
          setStatus: c,
          useHtml: !0,
          hasGenerateHashtagsAdjustOption: !0,
          hasIncludeKeywordsAdjustOption: !0,
          keywordsToInclude: m,
          //
          generateCallback: () => new Promise((e) => e(t)),
          onGenerateClick: h,
          generateError: C,
          onAdjustClick: P,
          //
          value: t,
          setValue: y
        })
      })
    }), i)
  });
};
export {
  J as RteAiAssistant
};
