import ArenaSelection from './helpers/ArenaSelection';
import ElementHelper from './helpers/ElementHelper';
import blockquotePlugin from './plugins/blockquotePlugin';
import calloutPlugin from './plugins/calloutPlugin';
import commonPlugin from './plugins/commonPlugin';
import embedPlugin from './plugins/embedPlugin';
import formatingsPlugin from './plugins/formatingsPlugin';
import headersPlugin from './plugins/headersPlugin';
import hrPlugin from './plugins/hrPlugin';
import imagePlugin from './plugins/imagePlugin';
import linkPlugin from './plugins/linkPlugin';
import listsPlugin from './plugins/listsPlugin';
import paragraphPlugin from './plugins/paragraphPlugin';
import ArenaServiceManager from './services/ArenaServiceManager';
import asidePlugin from './plugins/asidePlugin';
import quotePlugin from './plugins/quotePlugin';
import imageWithCaptionPlugin from './plugins/imageWithCaptionPlugin';
import typoSugarPlugin from './plugins/typoSugarPlugin';
const imgOpts = {
    srcset: [
        {
            media: '(max-width: 320px)',
            rations: [
                {
                    ratio: 1,
                    width: 100,
                    height: 100,
                },
                {
                    ratio: 2,
                    width: 200,
                    height: 200,
                },
            ],
        },
        {
            media: '',
            rations: [
                {
                    ratio: 1,
                    width: 200,
                    height: 200,
                },
                {
                    ratio: 2,
                    width: 400,
                    height: 400,
                },
            ],
        },
    ],
};
const defaultOptions = {
    editable: true,
    debug: false,
    toolbar: {
        enabled: true,
        tools: [
            'strong',
            'emphasized',
            'underline',
            'strikethrough',
            'link',
            'paragraph',
            'unordered-list',
            'ordered-list',
            'header2',
            'header3',
            'header4',
            'blockquote',
            'clearFormatings',
        ],
    },
    creatorBar: {
        enabled: true,
        creators: [
            'unordered-list',
            'ordered-list',
            'header2',
            'header3',
            'header4',
            'image-with-caption',
            'blockquote',
            'embed',
            'aside',
            'quote',
        ],
    },
    plugins: [
        commonPlugin(),
        paragraphPlugin(),
        formatingsPlugin(),
        headersPlugin(),
        hrPlugin(),
        listsPlugin(),
        blockquotePlugin(),
        calloutPlugin(),
        imagePlugin(),
        imageWithCaptionPlugin(imgOpts),
        embedPlugin(),
        linkPlugin(),
        asidePlugin(),
        quotePlugin(),
        typoSugarPlugin(),
    ],
};
class Textarena {
    constructor(container, options) {
        this.simpleArenas = [];
        this.debug = false;
        this.options = {};
        this.meta = {};
        // DOM Elements
        this.container = new ElementHelper(container, 'textarena-container', '');
        this.editor = new ElementHelper('DIV', 'textarena-editor');
        this.container.appendChild(this.editor.getElem());
        // Services
        this.asm = new ArenaServiceManager(this);
        this.setOptions(options ? Object.assign(Object.assign({}, defaultOptions), options) : defaultOptions);
        this.start();
    }
    getContainerElement() {
        return this.container;
    }
    getEditorElement() {
        return this.editor;
    }
    destructor() {
        this.asm.eventManager.fire('turnOff');
    }
    setOptions(options) {
        if (options.onChange !== undefined) {
            this.setOnChange(options.onChange);
        }
        if (options.onReady !== undefined) {
            this.setOnReady(options.onReady);
        }
        if (options.onEvent !== undefined) {
            this.setOnEvent(options.onEvent);
        }
        if (options.plugins) {
            this.setPlugins(options.plugins);
        }
        if (options.toolbar !== undefined) {
            this.setToolbarOptions(options.toolbar);
        }
        if (options.creatorBar !== undefined) {
            this.setCreatorBarOptions(options.creatorBar);
        }
        // if (options.initData !== undefined) {
        this.setData(options.initData);
        // }
        if (options.editable !== undefined) {
            this.setEditable(options.editable);
        }
        if (options.debug !== undefined) {
            this.debug = options.debug;
            this.asm.logger.setDebug(options.debug);
        }
    }
    getData() {
        return {
            html: this.getPublicHtml(),
            dataHtml: this.getDataHtml(),
            json: this.getJson(),
        };
    }
    getDataHtml() {
        return this.asm.model.getDataHtml();
    }
    getPublicHtml() {
        return this.asm.model.getPublicHtml();
    }
    getJson() {
        return this.asm.model.getJson();
    }
    setData(data) {
        const html = data && typeof data.dataHtml === 'string' ? data.dataHtml : '';
        const sel = this.asm.parser.insertHtmlToRoot(html);
        this.asm.history.reset(sel);
        this.asm.view.render();
    }
    setEditable(editable) {
        if (this.options.editable !== editable) {
            if (editable) {
                this.asm.eventManager.fire('turnOn');
            }
            else {
                this.asm.eventManager.fire('turnOff');
            }
            this.options.editable = editable;
            this.editor.setContentEditable(editable);
        }
    }
    setOnChange(onChange) {
        this.options.onChange = onChange;
    }
    setOnReady(onReady) {
        this.options.onReady = onReady;
    }
    setOnEvent(onEvent) {
        this.options.onEvent = onEvent;
    }
    setPlugins(plugins) {
        plugins.forEach((plugin) => {
            plugin.register(this);
        });
    }
    setToolbarOptions(toolbarOptions) {
        this.asm.toolbar.setOptions(toolbarOptions);
    }
    setCreatorBarOptions(creatorBarOptions) {
        this.asm.creatorBar.setOptions(creatorBarOptions);
    }
    getRootArenaName() {
        return this.asm.model.rootArenaName;
    }
    getSimpleArenas() {
        return this.simpleArenas;
    }
    addSimpleArenas(arena) {
        this.simpleArenas.push(arena);
    }
    setDefaultTextArena(arena) {
        this.asm.model.setDefaultTextArena(arena);
    }
    getDefaultTextArena() {
        return this.asm.model.model.arena.getArenaForText();
    }
    getArena(name) {
        return this.asm.model.getArena(name);
    }
    getArenas() {
        return this.asm.model.getArenas();
    }
    registerArena(arenaOptions, markers, parentArenas) {
        return this.asm.model.registerArena(arenaOptions, markers, parentArenas);
    }
    registerFormating(formating, markers) {
        return this.asm.model.registerFormating(formating, markers);
    }
    registerCommand(command, action) {
        return this.asm.commandManager.registerCommand(command, action);
    }
    registerShortcut(shortcut, command) {
        return this.asm.commandManager.registerShortcut(shortcut, command);
    }
    registerTool(opts) {
        this.asm.toolbar.registerTool(opts);
    }
    registerCreator(opts) {
        this.asm.creatorBar.registerCreator(opts);
    }
    applyArenaToSelection(selection, arena) {
        return this.asm.model.applyArenaToSelection(selection, arena);
    }
    applyFormationToSelection(selection, formating) {
        return this.asm.model.applyFormationToSelection(selection, formating);
    }
    clearFormationInSelection(selection) {
        return this.asm.model.clearFormationInSelection(selection);
    }
    insertBeforeSelected(selection, arena) {
        return this.asm.model.insertBeforeSelected(selection, arena);
    }
    breakSelection(selection) {
        return this.asm.model.breakSelection(selection);
    }
    removeSelection(selection, direction) {
        return this.asm.model.removeSelection(selection, direction);
    }
    createAndInsertNode(arena, parent, offset, before = false, onlyChild = false) {
        return this.asm.model.createAndInsertNode(arena, parent, offset, before, onlyChild);
    }
    moveChild(selection, direction) {
        return this.asm.model.moveChild(selection, direction);
    }
    addInlineNode(selection, arena) {
        return this.asm.model.addInlineNode(selection, arena);
    }
    getInlineNode(selection, arena) {
        return this.asm.model.getInlineNode(selection, arena);
    }
    removeInlineNode(selection, node) {
        return this.asm.model.removeInlineNode(selection, node);
    }
    updateInlineNode(selection, node) {
        return this.asm.model.updateInlineNode(selection, node);
    }
    subscribe(event, handler) {
        this.asm.eventManager.subscribe(event, handler);
    }
    start() {
        this.asm.eventManager.subscribe('modelChanged', (e) => {
            if (typeof e === 'object') {
                this.asm.view.render(e.detail instanceof ArenaSelection ? e.detail : undefined);
            }
            if (this.options.onChange) {
                this.options.onChange(this.getData());
            }
        });
        this.asm.eventManager.subscribe('*', (e) => {
            if (this.options.onEvent) {
                this.options.onEvent(e);
            }
        });
        this.asm.eventManager.fire('ready');
        if (this.options.onReady) {
            this.options.onReady(this.getData());
        }
        if (this.debug) {
            window.asm = this.asm;
        }
    }
}
export default Textarena;
//# sourceMappingURL=Textarena.js.map