"use client";
import { jsx } from "react/jsx-runtime";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { $insertNodeToNearestRoot, mergeRegister } from "@lexical/utils";
import { $createParagraphNode, $getNodeByKey, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_NORMAL, createCommand } from "lexical";
import { $createLayoutContainerNode, $isLayoutContainerNode, LayoutContainerNode } from "../../nodes/layout-container-node/layout-container-node.js";
import { $createLayoutItemNode, $isLayoutItemNode, LayoutItemNode } from "../../nodes/layout-container-node/layout-item-node.js";
import { InsertLayoutModal } from "./insert-layout-modal.js";
import * as __rspack_external_react from "react";
const OPEN_INSERT_LAYOUT_MODAL_COMMAND = createCommand('OPEN_INSERT_LAYOUT_MODAL_COMMAND');
const INSERT_LAYOUT_COMMAND = createCommand();
const UPDATE_LAYOUT_COMMAND = createCommand();
function LayoutPlugin() {
    const [editor] = useLexicalComposerContext();
    const [open, setOpen] = __rspack_external_react.useState(false);
    const handleOnClose = ()=>{
        setOpen(false);
    };
    const handleOnSubmit = (layout)=>{
        if (null != layout) editor.dispatchCommand(INSERT_LAYOUT_COMMAND, layout);
        else console.error('Error: missing layout for insertion.');
        setOpen(false);
    };
    (0, __rspack_external_react.useEffect)(()=>{
        if (!editor.hasNodes([
            LayoutContainerNode,
            LayoutItemNode
        ])) throw new Error('LayoutPlugin: LayoutContainerNode, or LayoutItemNode not registered on editor');
        return mergeRegister(editor.registerCommand(OPEN_INSERT_LAYOUT_MODAL_COMMAND, ()=>{
            setOpen(true);
            return true;
        }, COMMAND_PRIORITY_NORMAL), editor.registerCommand(INSERT_LAYOUT_COMMAND, (template)=>{
            editor.update(()=>{
                const container = $createLayoutContainerNode(template);
                const itemsCount = getItemsCountFromTemplate(template);
                for(let i = 0; i < itemsCount; i++)container.append($createLayoutItemNode().append($createParagraphNode()));
                $insertNodeToNearestRoot(container);
                container.selectStart();
            });
            return true;
        }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(UPDATE_LAYOUT_COMMAND, ({ template, nodeKey })=>{
            editor.update(()=>{
                const container = $getNodeByKey(nodeKey);
                if (!$isLayoutContainerNode(container)) return;
                const itemsCount = getItemsCountFromTemplate(template);
                const prevItemsCount = getItemsCountFromTemplate(container.getTemplateColumns());
                if (itemsCount > prevItemsCount) for(let i = prevItemsCount; i < itemsCount; i++)container.append($createLayoutItemNode().append($createParagraphNode()));
                else if (itemsCount < prevItemsCount) for(let i = prevItemsCount - 1; i >= itemsCount; i--){
                    const layoutItem = container.getChildAtIndex(i);
                    if ($isLayoutItemNode(layoutItem)) layoutItem.remove();
                }
                container.setTemplateColumns(template);
            });
            return true;
        }, COMMAND_PRIORITY_EDITOR), editor.registerNodeTransform(LayoutItemNode, (node)=>{
            const parent = node.getParent();
            if (!$isLayoutContainerNode(parent)) {
                const children = node.getChildren();
                for (const child of children)node.insertBefore(child);
                node.remove();
            }
        }), editor.registerNodeTransform(LayoutContainerNode, (node)=>{
            const children = node.getChildren();
            if (!children.every($isLayoutItemNode)) {
                for (const child of children)node.insertBefore(child);
                node.remove();
            }
        }));
    }, [
        editor
    ]);
    return /*#__PURE__*/ jsx(InsertLayoutModal, {
        open: open,
        onClose: handleOnClose,
        onSubmit: handleOnSubmit
    });
}
function getItemsCountFromTemplate(template) {
    return template.trim().split(/\s+/).length;
}
export { INSERT_LAYOUT_COMMAND, LayoutPlugin, OPEN_INSERT_LAYOUT_MODAL_COMMAND, UPDATE_LAYOUT_COMMAND };
