<commit_before>declare const window: any
declare const document: any
declare const Components: any
declare const Zotero: any
declare const Services: any

import { Preferences as Prefs } from './prefs'
import { Translators } from './translators'
import * as log from './debug'
import fastChunkString = require('fast-chunk-string')

const s3 = require('./s3.json')

const PACKAGE = require('../package.json')

Components.utils.import('resource://gre/modules/Services.jsm')
Components.utils.import('resource://gre/modules/osfile.jsm')

const kB = 1024
const MB = kB * kB

export = new class ErrorReport {
  private chunkSize
  private previewSize = 3 * kB // tslint:disable-line:no-magic-numbers binary-expression-operand-order

  private key: string
  private timestamp: string
  private bucket: string
  private params: any

  private errorlog: {
    info: string,
    errors: string,
    debug: string | string[],
    references?: string,
    db?: string
  }

  constructor() {
    window.addEventListener('load', () => this.init(), false)
  }

  public async send() {
    const wizard = document.getElementById('better-bibtex-error-report')
    wizard.getButton('next').disabled = true
    wizard.getButton('cancel').disabled = true
    wizard.canRewind = false

    try {
      const logs = [
        this.submit('debug', 'text/plain', this.errorlog.debug, `${this.errorlog.info}\n\n${this.errorlog.errors}\n\n`),
      ]

      if (document.getElementById('better-bibtex-error-report-include-db').checked) {
        await (new OS.File.DirectoryIterator(OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex'))).forEach(entry => {
          if (!entry.name.endsWith('.json') && !entry.name.endsWith('.json.bak')) return null

          const version = parseInt(entry.name.split('.')[1])
          if (!isNaN(version) && version > 0) return null

          logs.push(this.submit(entry.name.replace(/\.json(\.bak)?$/, ''), 'application/json', OS.File.read(entry.path, { encoding: 'utf-8' })))
        })
      }

      if (this.errorlog.references) logs.push(this.submit('references', 'application/json', this.errorlog.references))
      await Zotero.Promise.all(logs)
      wizard.advance()

      document.getElementById('better-bibtex-report-id').value = this.key
      document.getElementById('better-bibtex-report-result').hidden = false
    } catch (err) {
      const ps = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService)
      ps.alert(null, Zotero.getString('general.error'), `${err} (${this.key}, references: ${!!this.errorlog.references})`)
      if (wizard.rewind) wizard.rewind()
    }
  }

  public show() {
    const wizard = document.getElementById('better-bibtex-error-report')

    if (wizard.onLastPage) wizard.canRewind = false
    else if (wizard.pageIndex === 0) wizard.canRewind = false
    else if (wizard.pageIndex === 1 && Zotero.Debug.enabled) wizard.canRewind = false
    else wizard.canRewind = true
  }

  public restartWithDebugEnabled() {
    const ps = Services.prompt
    const buttonFlags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_IS_STRING<commit_msg>send cache in error report<commit_after>declare const window: any
declare const document: any
declare const Components: any
declare const Zotero: any
declare const Services: any

import { Preferences as Prefs } from './prefs'
import { Translators } from './translators'
import * as log from './debug'
import fastChunkString = require('fast-chunk-string')

import { DB } from './db/main'
import { DB as Cache } from './db/cache'

const s3 = require('./s3.json')

const PACKAGE = require('../package.json')

Components.utils.import('resource://gre/modules/Services.jsm')

const kB = 1024
const MB = kB * kB

export = new class ErrorReport {
  private chunkSize
  private previewSize = 3 * kB // tslint:disable-line:no-magic-numbers binary-expression-operand-order

  private key: string
  private timestamp: string
  private bucket: string
  private params: any

  private errorlog: {
    info: string,
    errors: string,
    debug: string | string[],
    references?: string,
    db?: string
  }

  constructor() {
    window.addEventListener('load', () => this.init(), false)
  }

  public async send() {
    const wizard = document.getElementById('better-bibtex-error-report')
    wizard.getButton('next').disabled = true
    wizard.getButton('cancel').disabled = true
    wizard.canRewind = false

    try {
      const logs = [
        this.submit('debug', 'text/plain', this.errorlog.debug, `${this.errorlog.info}\n\n${this.errorlog.errors}\n\n`),
      ]

      if (document.getElementById('better-bibtex-error-report-include-db').checked) {
        logs.push(this.submit('DB', 'application/json', DB.serialize({ serializationMethod: 'pretty' })))
        logs.push(this.submit('Cache', 'application/json', Cache.serialize({ serializationMethod: 'pretty' })))
      }

      if (this.errorlog.references) logs.push(this.submit('references', 'application/json', this.errorlog.references))
      await Zotero.Promise.all(logs)
      wizard.advance()

      document.getElementById('better-bibtex-report-id').value = this.key
      document.getElementById('better-bibtex-report-result').hidden = false
    } catch (err) {
      const ps = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService)
      ps.alert(null, Zotero.getString('general.error'), `${err} (${this.key}, references: ${!!this.errorlog.references})`)
      if (wizard.rewind) wizard.rewind()
    }
  }

  public show() {
    const wizard = document.getElementById('better-bibtex-error-report')

    if (wizard.onLastPage) wizard.canRewind = false
    else if (wizard.pageIndex === 0) wizard.canRewind = false
    else if (wizard.pageIndex === 1 && Zotero.Debug.enabled) wizard.canRewind = false
    else wizard.canRewind = true
  }

  public restartWithDebugEnabled() {
    const ps = Services.prompt
    const buttonFlags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_IS_STRING
{'fraction_non_alphanumeric': 0.14437086092715232, 'fraction_numerical': 0.0041390728476821195, 'mean_word_length': 3.6829457364341085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4082588', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1835, 'n_words': 395}