import { __exports as C } from "./index-_virtual23.js";
import { __require as ke } from "./index-LexicalCode2.js";
import { __require as Ie } from "./index-LexicalLink2.js";
import { __require as De } from "./index-LexicalList2.js";
import { __require as $e } from "./index-LexicalDecoratorBlockNode.js";
import { __require as qe } from "./index-LexicalHorizontalRuleNode.js";
import { __require as ve } from "./index-LexicalRichText2.js";
import { __require as xe } from "./index-LexicalSelection.js";
import { __require as Be } from "./index-LexicalTable2.js";
import { __require as Ue } from "./index-LexicalUtils.js";
import { __require as we } from "./index-Lexical.js";
import Pe from "react";
import { __require as Ge } from "./index-useFontFormatOptions.js";
import { __require as Ke } from "./index-useElementFormat.js";
import { __require as He } from "./index-useBlockFormat.js";
import { __require as ze } from "./index-getSelectedNode.js";
var B;
function nt() {
  if (B) return C;
  B = 1, Object.defineProperty(C, "__esModule", { value: !0 }), C.useLexicalToolbar = void 0;
  const f = ke(), g = Ie(), E = De(), U = $e(), w = qe(), M = ve(), _ = xe(), A = Be(), h = Ue(), s = we(), t = Pe, R = Ge(), P = Ke(), G = He(), K = ze(), k = {
    bullet: "Bulleted List",
    check: "Check List",
    code: "Code Block",
    h1: "Heading 1",
    h2: "Heading 2",
    h3: "Heading 3",
    h4: "Heading 4",
    h5: "Heading 5",
    h6: "Heading 6",
    number: "Numbered List",
    paragraph: "Normal",
    quote: "Quote"
  };
  function H() {
    const N = [];
    for (const [c, T] of Object.entries(f.CODE_LANGUAGE_FRIENDLY_NAME_MAP))
      N.push([c, T]);
    return N;
  }
  const z = H();
  function Y(N) {
    const { editor: c, urlInitialValue: T = "" } = N, [o, X] = (0, t.useState)(c), [r, I] = (0, t.useState)("paragraph"), [V, D] = (0, t.useState)("root"), [F, Z] = (0, t.useState)(null), [l, j] = (0, t.useState)(() => c.isEditable()), [Q, J] = (0, t.useState)(!1), [W, ee] = (0, t.useState)(!1), [te, oe] = (0, t.useState)(!1), [$, se] = (0, t.useState)("Arial"), [ie, ae] = (0, t.useState)("15px"), [q, re] = (0, t.useState)("var(--n700)"), [le, ne] = (0, t.useState)("var(--n000)"), [v, ce] = (0, t.useState)("left"), [de, ue] = (0, t.useState)(!1), [me, pe] = (0, t.useState)(!1), [_e, fe] = (0, t.useState)(!1), [ge, he] = (0, t.useState)(!1), [Ce, Ne] = (0, t.useState)(!1), [Te, be] = (0, t.useState)(!1), [b, x] = (0, t.useState)(!1), [Se, Le] = (0, t.useState)(!1), [y, Oe] = (0, t.useState)(""), S = (0, t.useCallback)(() => {
      const e = (0, s.$getSelection)();
      if ((0, s.$isRangeSelection)(e)) {
        const i = e.anchor.getNode();
        let n = i.getKey() === "root" ? i : (0, h.$findMatchingParent)(i, (p) => {
          const u = p.getParent();
          return u !== null && (0, s.$isRootOrShadowRoot)(u);
        });
        n === null && (n = i.getTopLevelElementOrThrow());
        const m = n.getKey(), a = o.getElementByKey(m);
        ue(e.hasFormat("bold")), pe(e.hasFormat("italic")), fe(e.hasFormat("underline")), he(e.hasFormat("strikethrough")), Ne(e.hasFormat("subscript")), be(e.hasFormat("superscript")), Le(e.hasFormat("code")), oe((0, _.$isParentElementRTL)(e));
        const d = (0, K.getSelectedNode)(e), O = d.getParent();
        (0, g.$isLinkNode)(O) || (0, g.$isLinkNode)(d) ? x(!0) : x(!1);
        const Re = (0, h.$findMatchingParent)(d, A.$isTableNode);
        if ((0, A.$isTableNode)(Re) ? D("table") : D("root"), a !== null)
          if (Z(m), (0, E.$isListNode)(n)) {
            const p = (0, h.$getNearestNodeOfType)(i, E.ListNode), u = p ? p.getListType() : n.getListType();
            I(u);
          } else {
            const p = (0, M.$isHeadingNode)(n) ? n.getTag() : n.getType();
            if (p in k && I(p), (0, f.$isCodeNode)(n)) {
              const u = n.getLanguage();
              Oe(u ? f.CODE_LANGUAGE_MAP[u] || u : "");
              return;
            }
          }
        ae((0, _.$getSelectionStyleValueForProperty)(e, "font-size", "15px")), re((0, _.$getSelectionStyleValueForProperty)(e, "color", "var(--n700)")), ne((0, _.$getSelectionStyleValueForProperty)(e, "background-color", "var(--n000)")), se((0, _.$getSelectionStyleValueForProperty)(e, "font-family", "Arial")), ce(((0, s.$isElementNode)(d) ? d.getFormatType() : O == null ? void 0 : O.getFormatType()) || "left");
      }
    }, [o]);
    (0, t.useEffect)(() => c.registerCommand(s.SELECTION_CHANGE_COMMAND, (e, i) => (S(), X(i), !1), s.COMMAND_PRIORITY_CRITICAL), [c, S]), (0, t.useEffect)(() => (0, h.mergeRegister)(c.registerEditableListener((e) => {
      j(e);
    }), o.registerUpdateListener(({ editorState: e }) => {
      e.read(() => {
        S();
      });
    }), o.registerCommand(s.CAN_UNDO_COMMAND, (e) => (J(e), !1), s.COMMAND_PRIORITY_CRITICAL), o.registerCommand(s.CAN_REDO_COMMAND, (e) => (ee(e), !1), s.COMMAND_PRIORITY_CRITICAL)), [S, o, c]), (0, t.useEffect)(() => o.registerCommand(s.KEY_MODIFIER_COMMAND, (e) => {
      const i = e, { code: n, ctrlKey: m, metaKey: a } = i;
      return n === "KeyK" && (m || a) ? (i.preventDefault(), o.dispatchCommand(g.TOGGLE_LINK_COMMAND, T)) : !1;
    }, s.COMMAND_PRIORITY_NORMAL), [o, b]);
    const L = (0, t.useCallback)((e) => {
      o.update(() => {
        const i = (0, s.$getSelection)();
        ((0, s.$isRangeSelection)(i) || (0, s.DEPRECATED_$isGridSelection)(i)) && (0, _.$patchStyleText)(i, e);
      });
    }, [o]), Me = (0, t.useCallback)(() => {
      o.update(() => {
        const e = (0, s.$getSelection)();
        if ((0, s.$isRangeSelection)(e)) {
          const i = e.anchor, n = e.focus, m = e.getNodes();
          if (i.key === n.key && i.offset === n.offset)
            return;
          m.forEach((a, d) => {
            (0, s.$isTextNode)(a) ? (d === 0 && i.offset !== 0 && (a = a.splitText(i.offset)[1] || a), d === m.length - 1 && (a = a.splitText(n.offset)[0] || a), a.__style !== "" && a.setStyle(""), a.__format !== 0 && (a.setFormat(0), (0, h.$getNearestBlockElementAncestorOrThrow)(a).setFormat(""))) : (0, M.$isHeadingNode)(a) || (0, M.$isQuoteNode)(a) ? a.replace((0, s.$createParagraphNode)(), !0) : (0, U.$isDecoratorBlockNode)(a) && a.setFormat("");
          });
        }
      });
    }, [o]), Fe = (0, t.useCallback)((e) => {
      L({ color: e });
    }, [L]), ye = (0, t.useCallback)((e) => {
      L({ "background-color": e });
    }, [L]), Ee = (0, t.useCallback)(() => {
      b ? c.dispatchCommand(g.TOGGLE_LINK_COMMAND, null) : c.dispatchCommand(g.TOGGLE_LINK_COMMAND, T);
    }, [c, b]), Ae = (0, t.useCallback)((e) => {
      o.update(() => {
        if (F !== null) {
          const i = (0, s.$getNodeByKey)(F);
          (0, f.$isCodeNode)(i) && i.setLanguage(e);
        }
      });
    }, [o, F]);
    return {
      isRTL: te,
      blockType: r,
      isEditable: l,
      undo: {
        dispatch: () => o.dispatchCommand(s.UNDO_COMMAND, void 0),
        disabled: !Q || !l,
        title: "Undo (Ctrl+Z or ⌘Z)"
      },
      redo: {
        dispatch: () => o.dispatchCommand(s.REDO_COMMAND, void 0),
        disabled: !W || !l,
        title: "Redo (Ctrl+Y or ⌘Y)"
      },
      bold: {
        show: r !== "code",
        disabled: !l,
        active: de,
        title: "Bold (Ctrl+B or ⌘B)",
        dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "bold")
      },
      italic: {
        show: r !== "code",
        disabled: !l,
        active: me,
        title: "Italic (Ctrl+I or ⌘I)",
        dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "italic")
      },
      underline: {
        show: r !== "code",
        disabled: !l,
        active: _e,
        title: "Underline (Ctrl+U or ⌘U)",
        dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "underline")
      },
      strikethrough: {
        show: r !== "code",
        disabled: !l,
        active: ge,
        title: "Strikethrough",
        dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "strikethrough")
      },
      subscript: {
        show: r !== "code",
        disabled: !l,
        active: Ce,
        title: "Subscript",
        dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "subscript")
      },
      superscript: {
        show: r !== "code",
        disabled: !l,
        active: Te,
        title: "Superscript",
        dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "superscript")
      },
      clearFormatting: {
        show: r !== "code",
        disabled: !l,
        title: "Clear formatting",
        dispatch: () => Me()
      },
      link: {
        show: r !== "code",
        disabled: !l,
        active: b,
        title: "Insert link",
        dispatch: () => Ee()
      },
      fontFamily: {
        show: r !== "code",
        disabled: !l,
        title: "Font family",
        value: $,
        options: (0, R.useFontFormatOptions)({
          editor: o,
          value: $,
          style: "font-family"
        })
      },
      fontSize: {
        show: r !== "code",
        disabled: !l,
        title: "Font size",
        value: ie,
        options: (0, R.useFontFormatOptions)({
          editor: o,
          value: q,
          style: "size"
        })
      },
      fontColor: {
        show: r !== "code",
        disabled: !l,
        title: "Text color",
        value: q,
        dispatch: Fe
      },
      backgroundColor: {
        show: r !== "code",
        disabled: !l,
        title: "Background color",
        value: le,
        dispatch: ye
      },
      hr: {
        show: r !== "code",
        disabled: !l,
        title: "Insert horizontal rule",
        dispatch: () => o.dispatchCommand(w.INSERT_HORIZONTAL_RULE_COMMAND, void 0)
      },
      blockFormatting: {
        show: r in k && o === c,
        disabled: !l,
        value: r,
        ...(0, G.useBlockFormat)({
          editor: o,
          blockType: r,
          rootType: V
        })
      },
      elementFormatting: {
        disabled: !l,
        value: v,
        ...(0, P.useElementFormat)({
          editor: o,
          value: v
        })
      },
      code: {
        disabled: !l,
        active: Se,
        insert: {
          show: r !== "code",
          title: "Insert code block",
          dispatch: () => o.dispatchCommand(s.FORMAT_TEXT_COMMAND, "code")
        },
        edit: {
          show: r === "code",
          title: "Select language",
          value: y,
          label: (0, f.getLanguageFriendlyName)(y),
          options: z.map(([e, i]) => ({
            title: i,
            value: e,
            active: e === y,
            dispatch: () => Ae(e)
          }))
        }
      }
    };
  }
  return C.useLexicalToolbar = Y, C;
}
export {
  nt as __require
};
