import Parchment from 'parchment';
import Quill from '../core/quill';
import Module from '../core/module';
import CodeBlock from '../formats/code';


class HighlightCodeBlock extends CodeBlock {
  static create(value) {
    let domNode = super.create(value);
    if (typeof value === 'string') {
      domNode.dataset.language = value;
    } else {
      domNode.dataset.language = HighlightCodeBlock.DEFAULT_LANGUAGE;
    }
    domNode.classList.add(domNode.dataset.language);
    return domNode;
  }

  static formats(domNode) {
    return domNode.dataset.language || HighlightCodeBlock.DEFAULT_LANGUAGE;
  }

  highlight() {
    if (this.cachedHTML !== this.domNode.innerHTML) {
      let text = this.domNode.textContent;
      if (text.trim().length > 0 || this.cachedHTML == null) {
        this.domNode.textContent = this.domNode.textContent;
        hljs.highlightBlock(this.domNode);
        this.attach();
      }
      this.cachedHTML = this.domNode.innerHTML;
    }
  }
}
HighlightCodeBlock.DEFAULT_LANGUAGE = 'javascript';


let CodeToken = new Parchment.Attributor.Class('token', 'hljs', {
  scope: Parchment.Scope.INLINE
});


class CodeHighlighter extends Module {
  constructor(quill, options) {
    super(quill, options);
    let timer = null;
    this.quill.on(Quill.events.SCROLL_OPTIMIZE, () => {
      if (timer != null) return;
      timer = setTimeout(() => {
        this.highlight();
        timer = null;
      }, 100);
    });
    this.highlight();
  }

  highlight() {
    let range = this.quill.getSelection();
    this.quill.scroll.descendants(HighlightCodeBlock).forEach(function(code) {
      code.highlight();
    });
    this.quill.update(Quill.sources.SILENT);
    if (range != null) {
      this.quill.setSelection(range, Quill.sources.SILENT);
    }
  }
}


export { HighlightCodeBlock as CodeBlock, CodeToken, CodeHighlighter as default};

@@ -601,24 +601,222 @@
 GUAGE;%0A  %7D%0A%0A
+  format(name, value) %7B%0A    if (name === this.statics.blotName && !value) %7B%0A      this.domNode.textContent = this.domNode.textContent;%0A      this.attach();%0A    %7D%0A    super.format(name, value);%0A  %7D%0A%0A
   highlight(
@@ -1015,38 +1015,18 @@
 tent = t
-his.domNode.textConten
+ex
 t;%0A     

remove syntax highlight on format removal