import GoogleMaps from "../../bricks/GoogleMaps.js";
import Icon from "../../bricks/Icon.js";
import Iframe from "../../bricks/Iframe.js";
import Link from "../../bricks/Link.js";
import Picture from "../../bricks/Picture.js";
import Recursive from "../../bricks/Recursive.js";
import RichText from "../../bricks/RichText.js";
import Slideshow from "../../bricks/Slideshow.js";
import Spotify from "../../bricks/Spotify.js";
import Table from "../../bricks/Table.js";
import Text from "../../bricks/Text.js";
import Video from "../../bricks/Video.js";
import YoutubePlaylist from "../../bricks/YoutubePlaylist.js";
import typeOf from "../../services/type-of.js";
const Templates = {
    Text,
    RichText,
    Picture,
    Link,
    Icon,
    Slideshow,
    Table,
    Recursive,
    Video,
    Iframe,
    GoogleMaps,
    YoutubePlaylist,
    Spotify,
};
export async function initializeBlockRecursive(block, node, isCreation, parentSelector = "", parentBricks = []) {
    const selectorIndexes = {};
    await Promise.all(Object.entries(node).map(async ([templateName, rawTemplate]) => {
        const template = rawTemplate;
        let selector = parentSelector + templateName;
        const selectorIndex = selectorIndexes[selector] ?? 0;
        selectorIndexes[selector] = selectorIndex + 1;
        if (selectorIndex > 0) {
            selector += `:nth-child(${selectorIndex})`;
        }
        const element = block.element.querySelector(selector);
        let parents = parentBricks;
        const brickType = template.type;
        if (brickType !== undefined) {
            const BrickTemplate = Templates[brickType];
            if (BrickTemplate !== undefined) {
                const { options, markup } = template;
                const tableParams = brickType === "Table" ? { rows: markup } : {};
                const recursiveParams = brickType === "Recursive"
                    ? { templates: options?.templates ?? markup }
                    : {};
                const brick = new BrickTemplate({
                    block,
                    element,
                    parentBricks: parents,
                    options,
                    markup,
                    isCreation,
                    ...tableParams,
                    ...recursiveParams,
                });
                if (brickType === "Recursive" &&
                    typeof brick.init === "function") {
                    await brick.init();
                }
                block.instances = [
                    ...(block.instances || []),
                    brick,
                ];
                parents = [...parents, brick];
            }
        }
        const markupType = typeOf(template.markup);
        if ((markupType === "undefined" && brickType === undefined) ||
            markupType === "object") {
            const next = markupType === "object" ? template.markup : template;
            await initializeBlockRecursive(block, next, isCreation, parentSelector + templateName + " > ", parents);
        }
    }));
}
