System.register(["quill"], function (exports_1, context_1) {
    "use strict";
    var quill_1, debug, Logger, QuillHtmlEditPlugin;
    var __moduleName = context_1 && context_1.id;
    function $create(elName) {
        return document.createElement(elName);
    }
    function $setAttr(el, key, value) {
        return el.setAttribute(key, value);
    }
    function launchPopupEditor(quill, options) {
        const htmlFromEditor = quill.container.querySelector('.ql-editor').innerHTML;
        const popupContainer = $create('div');
        const overlayContainer = $create('div');
        const msg = options.msg || 'Edit HTML here, when you click "OK" the quill editor\'s contents will be replaced';
        const cancelText = options.cancelText || 'Cancel';
        const okText = options.okText || 'Ok';
        $setAttr(overlayContainer, 'class', 'ql-html-overlayContainer');
        $setAttr(popupContainer, 'class', 'ql-html-popupContainer');
        const popupTitle = $create('i');
        $setAttr(popupTitle, 'class', 'ql-html-popupTitle');
        popupTitle.innerText = msg;
        const textContainer = $create('div');
        textContainer.appendChild(popupTitle);
        $setAttr(textContainer, 'class', 'ql-html-textContainer');
        const textArea = $create('textarea');
        $setAttr(textArea, 'class', 'ql-html-textArea');
        textArea.value = formatHTML(htmlFromEditor);
        const buttonCancel = $create('button');
        buttonCancel.innerHTML = cancelText;
        $setAttr(buttonCancel, 'class', 'ql-html-buttonCancel');
        const buttonOk = $create('button');
        buttonOk.innerHTML = okText;
        const buttonGroup = $create('div');
        $setAttr(buttonGroup, 'class', 'ql-html-buttonGroup');
        buttonGroup.appendChild(buttonCancel);
        buttonGroup.appendChild(buttonOk);
        textContainer.appendChild(textArea);
        textContainer.appendChild(buttonGroup);
        popupContainer.appendChild(textContainer);
        overlayContainer.appendChild(popupContainer);
        document.body.appendChild(overlayContainer);
        buttonCancel.onclick = () => {
            document.body.removeChild(overlayContainer);
        };
        overlayContainer.onclick = buttonCancel.onclick;
        popupContainer.onclick = (e) => {
            e.preventDefault();
            e.stopPropagation();
        };
        buttonOk.onclick = () => {
            const output = textArea.value.split(/\r?\n/g).map((el) => el.trim());
            const noNewlines = output.join('');
            quill.container.querySelector('.ql-editor').innerHTML = noNewlines;
            document.body.removeChild(overlayContainer);
        };
    }
    function formatHTML(code) {
        'use strict';
        const stripWhiteSpaces = true;
        const stripEmptyLines = true;
        const whitespace = ' '.repeat(2);
        let currentIndent = 0;
        const newlineChar = '\n';
        let char = null;
        let nextChar = null;
        let result = '';
        for (let pos = 0; pos <= code.length; pos++) {
            char = code.substr(pos, 1);
            nextChar = code.substr(pos + 1, 1);
            const isBrTag = code.substr(pos, 4) === '<br>';
            const isOpeningTag = char === '<' && nextChar !== '/' && !isBrTag;
            const isClosingTag = char === '<' && nextChar === '/' && !isBrTag;
            if (isBrTag) {
                result += newlineChar;
                currentIndent--;
                pos += 4;
            }
            if (isOpeningTag) {
                result += newlineChar + whitespace.repeat(currentIndent);
                currentIndent++;
            }
            else if (isClosingTag) {
                if (--currentIndent < 0) {
                    currentIndent = 0;
                }
                result += newlineChar + whitespace.repeat(currentIndent);
            }
            else if (stripWhiteSpaces === true && char === ' ' && nextChar === ' ') {
                char = '';
            }
            else if (stripEmptyLines === true && char === newlineChar) {
                if (code.substr(pos, code.substr(pos).indexOf('<')).trim() === '') {
                    char = '';
                }
            }
            result += char;
        }
        Logger.log('formatHTML', {
            before: code,
            after: result
        });
        return result;
    }
    return {
        setters: [
            function (quill_1_1) {
                quill_1 = quill_1_1;
            }
        ],
        execute: function () {
            debug = false;
            Logger = {
                prefixString() {
                    return `</> quill-html-edit-button: `;
                },
                get log() {
                    if (!debug) {
                        return () => { };
                    }
                    const boundLogFn = console.log.bind(console, this.prefixString());
                    return boundLogFn;
                }
            };
            QuillHtmlEditPlugin = class QuillHtmlEditPlugin {
                constructor(quill, options) {
                    debug = options && options.debug;
                    Logger.log('logging enabled');
                    const toolbarModule = quill.getModule('toolbar');
                    if (!toolbarModule) {
                        throw new Error('quill.htmlEditButton requires the "toolbar" module to be included too');
                    }
                    this.registerDivModule();
                    const toolbarEl = toolbarModule.container;
                    const buttonContainer = $create('span');
                    $setAttr(buttonContainer, 'class', 'ql-formats');
                    const button = $create('button');
                    button.innerHTML = options.buttonHTML || '&lt;&gt;';
                    button.title = options.buttonTitle || 'Show HTML source';
                    button.onclick = (e) => {
                        e.preventDefault();
                        launchPopupEditor(quill, options);
                    };
                    buttonContainer.appendChild(button);
                    toolbarEl.appendChild(buttonContainer);
                }
                registerDivModule() {
                    const Block = quill_1.default.import('blots/block');
                    class Div extends Block {
                    }
                    Div.tagName = 'div';
                    Div.blotName = 'div';
                    Div.allowedChildren = Block.allowedChildren;
                    Div.allowedChildren.push(Block);
                    quill_1.default.register(Div);
                }
            };
            exports_1("QuillHtmlEditPlugin", QuillHtmlEditPlugin);
            exports_1("default", QuillHtmlEditPlugin);
        }
    };
});

//# sourceMappingURL=quill-html-edit.plugin.js.map
