import Typograf from 'typograf';
import ElementHelper from '../helpers/ElementHelper';
import helpTextRu from '../helpTextRu';
import utils from '../utils';
function debounce(func, wait, immediate = false) {
    let timeout;
    return (...args) => {
        const later = () => {
            timeout = undefined;
            if (!immediate) {
                func(...args);
            }
        };
        const callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) {
            func(...args);
        }
    };
}
export default class SymbolCounter {
    constructor(asm) {
        this.asm = asm;
        this.lastDocumentOverflow = '';
        this.elem = new ElementHelper('ARENA-COUNTER', 'textarena-counter');
        this.content = new ElementHelper('DIV', 'textarena-counter__content');
        const typoButton = new ElementHelper('BUTTON', 'textarena-typograf', 'Типограф <sup>beta</sup>');
        typoButton.onClick(() => {
            this.typograf();
        });
        const helpButton = new ElementHelper('BUTTON', 'textarena-help', '?');
        helpButton.onClick(() => {
            this.openHelp();
        });
        this.elem.appendChild(this.content);
        this.elem.appendChild(typoButton);
        this.elem.appendChild(helpButton);
        this.container = this.asm.textarena.getContainerElement();
        this.container.appendChild(this.elem);
        this.asm.eventManager.subscribe('ready', this.update.bind(this));
        this.asm.eventManager.subscribe('modelChanged', debounce(this.update.bind(this), 1000));
    }
    update() {
        let symbols = 0;
        let words = 0;
        utils.modelTree.runOfChildren(this.asm.model.model, (n) => {
            if (n.hasText) {
                symbols += n.getTextLength();
                words += n.getRawText().split(/\s/).filter((w) => w !== '').length;
                // TODO считать количество картинок
            }
        });
        const time = Math.round(symbols / 1500);
        const readTime = time > 0 ? `${time} мин.` : 'меньше минуты';
        this.content.setInnerHTML(`Символов: ${symbols}, слов: ${words}, время чтения: ${readTime}`);
    }
    typograf() {
        const tp = new Typograf({ locale: ['ru', 'en-US'] });
        utils.modelTree.runOfChildren(this.asm.model.model, (n) => {
            if (n.hasText) {
                n.clearSpaces();
                n.setRawText(tp.execute(n.getRawText()));
            }
        });
        const sel = this.asm.view.getCurrentSelection();
        if (sel) {
            this.asm.history.save(sel);
        }
        this.asm.eventManager.fire('modelChanged', sel);
    }
    openHelp() {
        const wrap = new ElementHelper('DIV', 'textarena-help-wrap');
        wrap.onClick(() => {
            this.closeHelp();
        });
        const close = new ElementHelper('BUTTON', 'textarena-help-close', `<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
    viewBox="0 0 512.001 512.001" xml:space="preserve">
    <g>
      <path d="M294.111,256.001L504.109,46.003c10.523-10.524,10.523-27.586,0-38.109c-10.524-10.524-27.587-10.524-38.11,0L256,217.892
        L46.002,7.894c-10.524-10.524-27.586-10.524-38.109,0s-10.524,27.586,0,38.109l209.998,209.998L7.893,465.999
        c-10.524,10.524-10.524,27.586,0,38.109c10.524,10.524,27.586,10.523,38.109,0L256,294.11l209.997,209.998
        c10.524,10.524,27.587,10.523,38.11,0c10.523-10.524,10.523-27.586,0-38.109L294.111,256.001z" fill="currentColor"/>
    </g>
    </svg>`);
        close.onClick(() => {
            this.closeHelp();
        });
        const help = new ElementHelper('DIV', 'textarena-help-popup', helpTextRu);
        help.appendChild(close);
        wrap.appendChild(help);
        this.lastDocumentOverflow = document.body.style.overflow;
        document.body.style.overflow = 'hidden';
        this.container.appendChild(wrap);
        this.helpWrap = wrap;
    }
    closeHelp() {
        if (this.helpWrap) {
            this.helpWrap.remove();
            this.helpWrap = undefined;
        }
        document.body.style.overflow = this.lastDocumentOverflow;
    }
}
//# sourceMappingURL=SymbolCounter.js.map