import { registerEdenFormats } from "./eden/registration.js";
import { analyseEdenEditors, addEdenEditor } from "./eden/editors.js";
import { cleanEdenMarkup, postSaveEden, refreshEdenTimestamp, saveEden, } from "./eden/saving.js";
import { activateEden, changeEdenMode, desactivateEden, displayEdenInfo, hideEdenInfo, } from "./eden/mode.js";
import { disableBrowserTranslation } from "./services/util.js";
import { randomizeSvgSpriteCalls, cleanupOldSvgAttributes, } from "./services/cleanup.js";
import { startCollaboration, stopCollaboration, } from "./services/collaboration.js";
import { decodeToken } from "./services/token.js";
import { EdenMode, } from "./types.js";
import { setCSSTranslations } from "./services/css.js";
export default class Eden {
    constructor() {
        this.players = [];
        this.init = () => {
            this.editors = [];
            this.templateDescriptors = {};
            this.user = decodeToken();
            this.clientId = undefined;
            this.socket = undefined;
            cleanupOldSvgAttributes();
            randomizeSvgSpriteCalls();
            setCSSTranslations();
        };
        this.start = async () => {
            this.getTimestamp();
            this.init();
            this.changeMode(EdenMode.connected);
            await this.register();
            await this.analyse();
            startCollaboration(this);
            import("./user-interface/icons/eden-icon.js");
            import("./user-interface/common/eden-button.js");
            import("./user-interface/common/eden-checkbox.js");
            import("./user-interface/common/eden-input.js");
            import("./user-interface/common/eden-tag-manager.js");
            disableBrowserTranslation();
            const { default: EdenPageUi } = await import("./user-interface/page/eden-main-ui.js");
            this.pageUi = new EdenPageUi();
            document.body.append(this.pageUi);
        };
        this.getTimestamp = async () => {
            await refreshEdenTimestamp(this);
        };
        this.register = async () => {
            await registerEdenFormats(this);
        };
        this.analyse = async () => {
            await analyseEdenEditors(this);
        };
        this.addEditor = async (editorElement) => {
            return addEdenEditor(this, editorElement);
        };
        this.stop = () => {
            stopCollaboration(this);
            this.desactivate();
            this.pageUi?.remove();
            this.cleanRefs();
            this.init();
        };
        this.cleanRefs = () => {
            this.editors.forEach((editor) => editor.cleanRefs());
        };
        this.activate = () => {
            activateEden(this);
        };
        this.desactivate = () => {
            desactivateEden(this);
        };
        this.preSave = () => {
            this.editors.map((editor) => editor.preSave());
        };
        this.save = async () => {
            await saveEden(this);
        };
        this.postSave = (error) => {
            postSaveEden(this, error);
        };
        this.cleanMarkup = () => {
            cleanEdenMarkup();
        };
        this.getActiveBlock = () => {
            return this.activeBlock;
        };
        this.setActiveBlock = (block) => {
            this.activeBlock = block;
        };
        this.logout = async () => {
            if (window.updateEdenDraftMode) {
                const token = window.sessionStorage.getItem("eden_token");
                if (!token)
                    return;
                await window.updateEdenDraftMode(false, token);
            }
            stopCollaboration(this);
            this.desactivate();
            this.pageUi?.remove();
            window.sessionStorage.removeItem("eden_token");
            window.location.reload();
        };
        this.displayInfo = () => {
            displayEdenInfo(this);
        };
        this.hideInfo = () => {
            hideEdenInfo(this);
        };
        this.changeMode = (mode) => {
            changeEdenMode(mode);
        };
    }
}
