import { Editor, Element as SlateElement, Transforms } from "slate";
import { listTypes, textAlignTypes } from "../templates/richTextTemplates";
import { isBlockActive } from "./isBlockActive";
function toggleBlock(editor, format) {
    const blockType = textAlignTypes.includes(format) ? "align" : "type";
    const isActive = isBlockActive(editor, format, blockType);
    const isList = listTypes.includes(format);
    Transforms.unwrapNodes(editor, {
        match: (n) => !Editor.isEditor(n) &&
            SlateElement.isElement(n) &&
            listTypes.includes(n.type) &&
            !textAlignTypes.includes(format),
        split: true,
    });
    let newProperties;
    if (textAlignTypes.includes(format)) {
        const formatType = format;
        newProperties = { align: isActive ? undefined : formatType };
    }
    else {
        const formatType = format;
        newProperties = {
            type: isActive ? "paragraph" : isList ? "listItem" : formatType,
        };
    }
    Transforms.setNodes(editor, newProperties);
    if (!isActive && isList) {
        const block = { type: format, children: [] };
        Transforms.wrapNodes(editor, block);
    }
}
export { toggleBlock };
