import { isLocalhost } from "../../services/util.js";
import Block from "../block.js";
export async function analyseEditor(editor) {
    const domBlocks = Array.from(editor.element.querySelectorAll("section"));
    const blocks = (await Promise.all(domBlocks.map(async (domBlock) => {
        const templateName = domBlock.getAttribute("data-template");
        const templateDescriptor = editor.templateDescriptors?.[templateName];
        if (!templateDescriptor || !templateName) {
            return;
        }
        const block = new Block(domBlock, templateDescriptor, editor, templateName, false);
        try {
            await block.init();
        }
        catch (e) {
            if (isLocalhost()) {
                console.info(e);
            }
        }
        return block;
    }))).filter((block) => Boolean(block));
    if (editor.defaultContent &&
        !editor.defaultContentSpawned &&
        !blocks.length) {
        await editor.create(editor.defaultContent, undefined, false);
        await editor.analyse();
        editor.defaultContentSpawned = true;
        return;
    }
    editor.blocks = blocks;
}
