"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { ReactExtension } from "@lexical/react/ReactExtension";
import { $insertNodeToNearestRoot, mergeRegister } from "@lexical/utils";
import { $getSelection, $isRangeSelection, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_NORMAL, configExtension, createCommand, declarePeerDependency, defineExtension } from "lexical";
import { useToolbarActiveEditor } from "../../plugins/toolbar-plugin/toolbar-active-editor.js";
import { DropDownItem } from "../../ui/dropdown.js";
import { BylineToolbarExtension } from "../byline-toolbar/byline-toolbar-extension.js";
import { AdmonitionModal } from "./admonition-modal.js";
import { $createAdmonitionNode, AdmonitionNode } from "./admonition-node.js";
import * as __rspack_external_react from "react";
const OPEN_ADMONITION_MODAL_COMMAND = createCommand('OPEN_ADMONITION_MODAL_COMMAND');
const INSERT_ADMONITION_COMMAND = createCommand('INSERT_ADMONITION_COMMAND');
function AdmonitionPlugin() {
    const [editor] = useLexicalComposerContext();
    const [open, setOpen] = __rspack_external_react.useState(false);
    (0, __rspack_external_react.useEffect)(()=>{
        if (!editor.hasNodes([
            AdmonitionNode
        ])) throw new Error('AdmonitionPlugin: AdmonitionNode not registered on editor');
        return mergeRegister(editor.registerCommand(OPEN_ADMONITION_MODAL_COMMAND, ()=>{
            setOpen(true);
            return true;
        }, COMMAND_PRIORITY_NORMAL), editor.registerCommand(INSERT_ADMONITION_COMMAND, (payload)=>{
            const selection = $getSelection();
            if (!$isRangeSelection(selection)) return false;
            const focusNode = selection.focus.getNode();
            if (null !== focusNode) {
                const admonitionNode = $createAdmonitionNode(payload);
                $insertNodeToNearestRoot(admonitionNode);
            }
            return true;
        }, COMMAND_PRIORITY_EDITOR));
    }, [
        editor
    ]);
    const handleInsertAdmonition = ({ admonitionType, title })=>{
        if (null != title && null != admonitionType) {
            const admonitionPayload = {
                admonitionType,
                title
            };
            editor.dispatchCommand(INSERT_ADMONITION_COMMAND, admonitionPayload);
        } else console.error('Error: missing title or type for admonition.');
        setOpen(false);
    };
    return /*#__PURE__*/ jsx(AdmonitionModal, {
        open: open,
        data: {
            title: '',
            admonitionType: void 0
        },
        onClose: ()=>setOpen(false),
        onSubmit: handleInsertAdmonition
    });
}
function AdmonitionInsertItem() {
    const editor = useToolbarActiveEditor();
    return /*#__PURE__*/ jsxs(DropDownItem, {
        onClick: ()=>{
            editor.dispatchCommand(OPEN_ADMONITION_MODAL_COMMAND, null);
        },
        className: "item",
        children: [
            /*#__PURE__*/ jsx("i", {
                className: "icon admonition"
            }),
            /*#__PURE__*/ jsx("span", {
                className: "text",
                children: "Admonition"
            })
        ]
    });
}
const AdmonitionExtension = defineExtension({
    name: '@byline/richtext-lexical/Admonition',
    nodes: ()=>[
            AdmonitionNode
        ],
    dependencies: [
        configExtension(ReactExtension, {
            decorators: [
                /*#__PURE__*/ jsx(AdmonitionPlugin, {}, "d")
            ]
        })
    ],
    peerDependencies: [
        declarePeerDependency(BylineToolbarExtension.name, {
            items: [
                {
                    id: '@byline/richtext-lexical/Admonition/insert',
                    placement: 'insert-menu',
                    order: 30,
                    node: /*#__PURE__*/ jsx(AdmonitionInsertItem, {})
                }
            ]
        })
    ]
});
export { AdmonitionExtension, AdmonitionPlugin, INSERT_ADMONITION_COMMAND, OPEN_ADMONITION_MODAL_COMMAND };
