import { EdenEvents, fireEvent } from "../../services/event.js";
import Block from "../block.js";
export async function createEditor(editor, code, variant, showBlockUi = true) {
    const block = await createEditorBlock(editor, code, variant, showBlockUi);
    if (!block) {
        return;
    }
    editor.element.append(block.element);
    if (showBlockUi) {
        block.showUi();
    }
    editor.showUi();
    if (block.editor) {
        fireEvent(document, EdenEvents.blockCreated, {
            block,
            editor,
        });
    }
}
export async function createEditorBlock(editor, templateCode, variant, activateBlock = true) {
    if (!canEditorAddBlock(editor)) {
        return;
    }
    const templateDescriptor = editor.templateDescriptors?.[templateCode];
    if (!templateDescriptor) {
        return;
    }
    const element = templateDescriptor.createElement(templateCode, editor, variant);
    const block = new Block(element, templateDescriptor, editor, templateCode, true);
    await block.init();
    editor.blocks?.push(block);
    if (activateBlock) {
        block.activate();
    }
    return block;
}
export function canEditorAddBlock(editor) {
    if (!editor.blocks) {
        return true;
    }
    return (editor.maxBlocks === undefined || editor.blocks.length < editor.maxBlocks);
}
