import { Editor, Transforms } from "slate";
import { ContemberEditor } from "../../../ContemberEditor/index.js";
import { ParagraphRenderer } from "./ParagraphRenderer.js";
const paragraphElementType = "paragraph";
const isParagraphElement = (element, suchThat) => ContemberEditor.isElementType(element, paragraphElementType, suchThat);
const paragraphElementPlugin = {
  type: paragraphElementType,
  render: ParagraphRenderer,
  canContainAnyBlocks: false,
  acceptsAttributes: ({ editor, suchThat }) => {
    return "align" in suchThat;
  },
  toggleElement: ({ editor, suchThat }) => {
    Editor.withoutNormalizing(editor, () => {
      const topLevelNodes = Array.from(ContemberEditor.topLevelNodes(editor));
      if (topLevelNodes.every(([node]) => isParagraphElement(node, suchThat))) {
        for (const [, path] of topLevelNodes) {
          ContemberEditor.ejectElement(editor, path);
          Transforms.setNodes(editor, { type: editor.defaultElementType }, { at: path });
        }
      } else {
        for (const [node, path] of topLevelNodes) {
          if (isParagraphElement(node, suchThat)) {
            continue;
          }
          ContemberEditor.ejectElement(editor, path);
          const newProps = {
            ...suchThat,
            type: paragraphElementType
          };
          Transforms.setNodes(editor, newProps, { at: path });
        }
      }
    });
  },
  normalizeNode: ({ editor, element, path, preventDefault }) => {
    for (const [i, child] of element.children.entries()) {
      if (Editor.isBlock(editor, child)) {
        Transforms.unwrapNodes(editor, { at: [...path, i] });
        return preventDefault();
      }
    }
  }
};
export { isParagraphElement, paragraphElementPlugin, paragraphElementType };
//# sourceMappingURL=ParagraphElement.js.map
