import { analyseEditor } from "./editor/analysis.js";
import { addEditorUuids, copyEditor, pasteEditor } from "./editor/clipboard.js";
import { canEditorAddBlock, createEditor, createEditorBlock, } from "./editor/operations.js";
import { hideEditorUi, showEditorUi } from "./editor/ui-overlay.js";
export default class Editor {
    constructor(element, templateDescriptors) {
        this.analyse = async () => {
            await analyseEditor(this);
        };
        this.cleanRefs = () => {
            if (!this.blocks)
                return;
            this.blocks.forEach((block) => block.cleanRefs());
            delete this.blocks;
            delete this.templateDescriptors;
        };
        this.showUi = async () => {
            await showEditorUi(this);
        };
        this.hideUi = () => {
            hideEditorUi(this);
        };
        this.activate = () => {
            if (!this.blocks)
                return;
            this.showUi();
            this.blocks.forEach((block) => block.activate());
        };
        this.desactivate = () => {
            if (!this.blocks)
                return;
            this.hideUi();
            this.blocks.forEach((block) => block.desactivate());
        };
        this.preSave = () => {
            if (!this.blocks)
                return;
            this.blocks.forEach((block) => block.preSave());
        };
        this.postSave = () => {
            if (!this.blocks)
                return;
            this.blocks.forEach((block) => block.postSave());
        };
        this.displayInfo = () => {
            if (!this.blocks)
                return;
            this.blocks.forEach((block) => block.displayInfo());
        };
        this.hideInfo = () => {
            if (!this.blocks)
                return;
            this.blocks.forEach((block) => block.hideInfo());
        };
        this.addUuids = () => {
            addEditorUuids(this);
        };
        this.paste = async () => {
            await pasteEditor(this);
        };
        this.create = async (code, variant, showBlockUi = true) => {
            await createEditor(this, code, variant, showBlockUi);
        };
        this.createBlock = async (templateCode, variant, activateBlock = true) => {
            return createEditorBlock(this, templateCode, variant, activateBlock);
        };
        this.copy = async () => {
            await copyEditor(this);
        };
        this.canAddBlock = () => {
            return canEditorAddBlock(this);
        };
        this.element = element;
        const { dataset } = this.element;
        this.defaultContent = dataset.edenDefaultContent;
        this.maxBlocks = dataset.edenMaxBlocks
            ? parseInt(dataset.edenMaxBlocks, 10)
            : undefined;
        const templates = dataset.edenTemplates;
        this.templates = templates ? templates.split(",") : undefined;
        this.tagsLabel = dataset.edenTagsLabel;
        this.allowedTags = dataset.edenAllowedTags;
        this.allowMultipleTags = dataset.edenAllowMultipleTags === "true";
        this.isFixed = dataset.edenFixed === "true";
        this.isReadOnly = dataset.edenReadOnly === "true";
        this.withUuid = dataset.edenUuid === "true";
        this.hasChanged = false;
        this.defaultContentSpawned = false;
        this.templateDescriptors = templateDescriptors;
        this.blocks = [];
    }
}
