"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    default: ()=>ToolbarPlugin
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const link_namespaceObject = require("@lexical/link");
const list_namespaceObject = require("@lexical/list");
const LexicalComposerContext_namespaceObject = require("@lexical/react/LexicalComposerContext");
const rich_text_namespaceObject = require("@lexical/rich-text");
const selection_namespaceObject = require("@lexical/selection");
const utils_namespaceObject = require("@lexical/utils");
const icons_material_namespaceObject = require("@mui/icons-material");
const material_namespaceObject = require("@mui/material");
const external_lexical_namespaceObject = require("lexical");
const external_react_namespaceObject = require("react");
const useSafeLingui_cjs_namespaceObject = require("../../../../i18n/useSafeLingui.cjs");
const LowPriority = 1;
function Divider() {
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "divider"
    });
}
function getSelectedNode(selection) {
    const anchor = selection.anchor;
    const focus = selection.focus;
    const anchorNode = selection.anchor.getNode();
    const focusNode = selection.focus.getNode();
    if (anchorNode === focusNode) return anchorNode;
    const isBackward = selection.isBackward();
    if (isBackward) return (0, selection_namespaceObject.$isAtNodeEnd)(focus) ? anchorNode : focusNode;
    return (0, selection_namespaceObject.$isAtNodeEnd)(anchor) ? focusNode : anchorNode;
}
function ToolbarPlugin({ disabled = false }) {
    const [editor] = (0, LexicalComposerContext_namespaceObject.useLexicalComposerContext)();
    const toolbarRef = (0, external_react_namespaceObject.useRef)(null);
    const { _ } = (0, useSafeLingui_cjs_namespaceObject.useSafeLingui)();
    const [isBold, setIsBold] = (0, external_react_namespaceObject.useState)(false);
    const [isItalic, setIsItalic] = (0, external_react_namespaceObject.useState)(false);
    const [isUnderline, setIsUnderline] = (0, external_react_namespaceObject.useState)(false);
    const [isStrikethrough, setIsStrikethrough] = (0, external_react_namespaceObject.useState)(false);
    const [blockType, setBlockType] = (0, external_react_namespaceObject.useState)('paragraph');
    const [isLink, setIsLink] = (0, external_react_namespaceObject.useState)(false);
    const [isCode, setIsCode] = (0, external_react_namespaceObject.useState)(false);
    const $updateToolbar = (0, external_react_namespaceObject.useCallback)(()=>{
        const selection = (0, external_lexical_namespaceObject.$getSelection)();
        if ((0, external_lexical_namespaceObject.$isRangeSelection)(selection)) {
            const anchorNode = selection.anchor.getNode();
            const element = 'root' === anchorNode.getKey() ? anchorNode : anchorNode.getTopLevelElementOrThrow();
            const elementKey = element.getKey();
            const elementDOM = editor.getElementByKey(elementKey);
            if (null !== elementDOM) if ((0, list_namespaceObject.$isListNode)(element)) {
                const parentList = (0, utils_namespaceObject.$getNearestNodeOfType)(anchorNode, list_namespaceObject.ListNode);
                const type = parentList ? parentList.getTag() : element.getTag();
                setBlockType(type);
            } else {
                const type = (0, rich_text_namespaceObject.$isHeadingNode)(element) ? element.getTag() : element.getType();
                setBlockType(type);
            }
            setIsBold(selection.hasFormat('bold'));
            setIsItalic(selection.hasFormat('italic'));
            setIsUnderline(selection.hasFormat('underline'));
            setIsStrikethrough(selection.hasFormat('strikethrough'));
            setIsCode(selection.hasFormat('code'));
            const node = getSelectedNode(selection);
            const parent = node.getParent();
            (0, link_namespaceObject.$isLinkNode)(parent) || (0, link_namespaceObject.$isLinkNode)(node) ? setIsLink(true) : setIsLink(false);
        }
    }, [
        editor
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>(0, utils_namespaceObject.mergeRegister)(editor.registerUpdateListener(({ editorState })=>{
            editorState.read(()=>{
                $updateToolbar();
            });
        }), editor.registerCommand(external_lexical_namespaceObject.SELECTION_CHANGE_COMMAND, ()=>{
            $updateToolbar();
            return false;
        }, LowPriority)), [
        editor,
        $updateToolbar
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        className: `toolbar${disabled ? ' disabled' : ''}`,
        ref: toolbarRef,
        role: "toolbar",
        "aria-orientation": "horizontal",
        "aria-disabled": disabled,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.bold'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        editor.dispatchCommand(external_lexical_namespaceObject.FORMAT_TEXT_COMMAND, 'bold');
                    },
                    disabled: disabled,
                    className: 'toolbar-item spaced ' + (isBold ? 'active' : ''),
                    "aria-pressed": isBold,
                    "aria-label": _({
                        id: 'rich-text-editor.format-bold'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.FormatBold, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.italic'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        editor.dispatchCommand(external_lexical_namespaceObject.FORMAT_TEXT_COMMAND, 'italic');
                    },
                    className: 'toolbar-item spaced ' + (isItalic ? 'active' : ''),
                    "aria-pressed": isItalic,
                    disabled: disabled,
                    "aria-label": _({
                        id: 'rich-text-editor.format-italic'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.FormatItalic, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.underline'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        editor.dispatchCommand(external_lexical_namespaceObject.FORMAT_TEXT_COMMAND, 'underline');
                    },
                    className: 'toolbar-item spaced ' + (isUnderline ? 'active' : ''),
                    "aria-pressed": isUnderline,
                    disabled: disabled,
                    "aria-label": _({
                        id: 'rich-text-editor.format-underline'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.FormatUnderlined, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.strikethrough'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        editor.dispatchCommand(external_lexical_namespaceObject.FORMAT_TEXT_COMMAND, 'strikethrough');
                    },
                    className: 'toolbar-item spaced ' + (isStrikethrough ? 'active' : ''),
                    "aria-pressed": isStrikethrough,
                    disabled: disabled,
                    "aria-label": _({
                        id: 'rich-text-editor.format-strikethrough'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.FormatStrikethrough, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Divider, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.unordered-list'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        if ('ul' !== blockType) editor.dispatchCommand(list_namespaceObject.INSERT_UNORDERED_LIST_COMMAND, void 0);
                        else editor.dispatchCommand(list_namespaceObject.REMOVE_LIST_COMMAND, void 0);
                    },
                    className: 'toolbar-item spaced ' + ('ul' === blockType ? 'active' : ''),
                    disabled: disabled,
                    "aria-pressed": 'ul' === blockType,
                    "aria-label": _({
                        id: 'rich-text-editor.format-unordered-list'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.FormatListBulleted, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.ordered-list'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        if ('ol' !== blockType) editor.dispatchCommand(list_namespaceObject.INSERT_ORDERED_LIST_COMMAND, void 0);
                        else editor.dispatchCommand(list_namespaceObject.REMOVE_LIST_COMMAND, void 0);
                    },
                    className: 'toolbar-item spaced ' + ('ol' === blockType ? 'active' : ''),
                    disabled: disabled,
                    "aria-pressed": 'ol' === blockType,
                    "aria-label": _({
                        id: 'rich-text-editor.format-ordered-list'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.FormatListNumbered, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Divider, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.link'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        if (isLink) editor.dispatchCommand(link_namespaceObject.TOGGLE_LINK_COMMAND, null);
                        else editor.update(()=>{
                            const selection = (0, external_lexical_namespaceObject.$getSelection)();
                            if ((0, external_lexical_namespaceObject.$isRangeSelection)(selection)) selection.insertText(`[${selection.getTextContent() || 'text'}](link)`);
                        });
                    },
                    className: 'toolbar-item spaced ' + (isLink ? 'active' : ''),
                    disabled: disabled,
                    "aria-pressed": isLink,
                    "aria-label": _({
                        id: 'rich-text-editor.insert-link'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.Link, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Tooltip, {
                title: _({
                    id: 'rich-text-editor.code'
                }),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                    type: "button",
                    onClick: ()=>{
                        editor.dispatchCommand(external_lexical_namespaceObject.FORMAT_TEXT_COMMAND, 'code');
                    },
                    className: 'toolbar-item spaced ' + (isCode ? 'active' : ''),
                    "aria-pressed": isCode,
                    disabled: disabled,
                    "aria-label": _({
                        id: 'rich-text-editor.format-code'
                    }),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_material_namespaceObject.Code, {
                        sx: {
                            fontSize: '24px'
                        }
                    })
                })
            })
        ]
    });
}
exports["default"] = __webpack_exports__["default"];
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "default"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
