<commit_before>declare const document: any
declare const window: any
declare const Zotero: any
declare const Zotero_Preferences: any

import { debug } from './debug.ts'
import { ZoteroConfig } from './zotero-config.ts'
import { patch as $patch$ } from './monkey-patch.ts'

import { Preferences as Prefs } from './prefs.ts'
import { Formatter } from './key-manager/formatter.ts'
import { KeyManager } from './key-manager.ts'
import { AutoExport } from './auto-export.ts'
import { Translators } from './translators.ts'

class AutoExportPrefPane {
  protected AutoExport: AutoExportPrefPane // tslint:disable-line:variable-name

  public remove() {
    const exportlist = document.getElementById('better-bibtex-export-list')
    if (!exportlist) return
    const selected = exportlist.currentIndex
    if (selected < 0) return

    const id = parseInt(exportlist.contentView.getItemAtIndex(selected).getAttribute('autoexport'))
    debug('AutoExport: removing', { id })
    AutoExport.db.remove(id)
    this.refresh()
  }

  public mark() {
    const exportlist = document.getElementById('better-bibtex-export-list')
    if (!exportlist) return
    const selected = exportlist.currentIndex
    if (selected < 0) return

    const id = parseInt(exportlist.contentView.getItemAtIndex(selected).getAttribute('autoexport'))
    AutoExport.run(id)
    this.refresh()
  }

  public refresh() {
    if (!AutoExport.db) {
      debug('AutoExportPrefPane.refresh: DB not loaded')
      return
    }

    const exportlist = document.getElementById('better-bibtex-auto-exports')
    if (!exportlist) return
    while (exportlist.hasChildNodes()) {
      exportlist.removeChild(exportlist.firstChild)
    }

    const columns = Array.from(
      document.getElementById('better-bibtex-export-list').getElementsByTagName('treecol') as NodeList
    ).map(
      col => (col as Element).getAttribute('id').replace(/^better-bibtex-preferences-auto-export-/, '')
    )

    const on = Zotero.BetterBibTeX.getString('Preferences.auto-export.setting.on')
    const off = Zotero.BetterBibTeX.getString('Preferences.auto-export.setting.off')

    for (const ae of AutoExport.db.chain().simplesort('path').data()) {
      const treeitem = exportlist.appendChild(document.createElement('treeitem'))
      treeitem.setAttribute('autoexport', `${ae.$loki}`)

      const treerow = treeitem.appendChild(document.createElement('treerow'))
      // TODO: https://github.com/Microsoft/TypeScript/issues/19186
      // TODO: https://github.com/Microsoft/TypeScript/issues/1260

      for (const column of columns) {
        debug('Preferences.AutoExport.refresh:', column)
        const treecell = treerow.appendChild(document.createElement('treecell'))
        treecell.setAttribute('editable', 'false')

        switch (column) {
          case 'collection':
            const type = Zotero.BetterBibTeX.getString(`Preferences.auto-export.type.${ae.type}`) || ae.type
            treecell.setAttribute('label', `${type}: ${this.autoExportName(ae)}`)
            break

          case 'status':
            let status = ae.error ? 'error' : ae.status
            status = Zotero.BetterBibTeX.getString(`Preferences.auto-export.status.${status}`) || status
            if (ae.error) status += `: ${ae.error}`
            treecell.setAttribute('label', status)
            break

          case 'updated':
            treecell.setAttribute('label', `${new Date(ae.meta.updated)}`)<commit_msg>lokijs doesn't set updated on freshly created docs<commit_after>declare const document: any
declare const window: any
declare const Zotero: any
declare const Zotero_Preferences: any

import { debug } from './debug.ts'
import { ZoteroConfig } from './zotero-config.ts'
import { patch as $patch$ } from './monkey-patch.ts'

import { Preferences as Prefs } from './prefs.ts'
import { Formatter } from './key-manager/formatter.ts'
import { KeyManager } from './key-manager.ts'
import { AutoExport } from './auto-export.ts'
import { Translators } from './translators.ts'

class AutoExportPrefPane {
  protected AutoExport: AutoExportPrefPane // tslint:disable-line:variable-name

  public remove() {
    const exportlist = document.getElementById('better-bibtex-export-list')
    if (!exportlist) return
    const selected = exportlist.currentIndex
    if (selected < 0) return

    const id = parseInt(exportlist.contentView.getItemAtIndex(selected).getAttribute('autoexport'))
    debug('AutoExport: removing', { id })
    AutoExport.db.remove(id)
    this.refresh()
  }

  public mark() {
    const exportlist = document.getElementById('better-bibtex-export-list')
    if (!exportlist) return
    const selected = exportlist.currentIndex
    if (selected < 0) return

    const id = parseInt(exportlist.contentView.getItemAtIndex(selected).getAttribute('autoexport'))
    AutoExport.run(id)
    this.refresh()
  }

  public refresh() {
    if (!AutoExport.db) {
      debug('AutoExportPrefPane.refresh: DB not loaded')
      return
    }

    const exportlist = document.getElementById('better-bibtex-auto-exports')
    if (!exportlist) return
    while (exportlist.hasChildNodes()) {
      exportlist.removeChild(exportlist.firstChild)
    }

    const columns = Array.from(
      document.getElementById('better-bibtex-export-list').getElementsByTagName('treecol') as NodeList
    ).map(
      col => (col as Element).getAttribute('id').replace(/^better-bibtex-preferences-auto-export-/, '')
    )

    const on = Zotero.BetterBibTeX.getString('Preferences.auto-export.setting.on')
    const off = Zotero.BetterBibTeX.getString('Preferences.auto-export.setting.off')

    for (const ae of AutoExport.db.chain().simplesort('path').data()) {
      const treeitem = exportlist.appendChild(document.createElement('treeitem'))
      treeitem.setAttribute('autoexport', `${ae.$loki}`)

      const treerow = treeitem.appendChild(document.createElement('treerow'))
      // TODO: https://github.com/Microsoft/TypeScript/issues/19186
      // TODO: https://github.com/Microsoft/TypeScript/issues/1260

      for (const column of columns) {
        debug('Preferences.AutoExport.refresh:', column)
        const treecell = treerow.appendChild(document.createElement('treecell'))
        treecell.setAttribute('editable', 'false')

        switch (column) {
          case 'collection':
            const type = Zotero.BetterBibTeX.getString(`Preferences.auto-export.type.${ae.type}`) || ae.type
            treecell.setAttribute('label', `${type}: ${this.autoExportName(ae)}`)
            break

          case 'status':
            let status = ae.error ? 'error' : ae.status
            status = Zotero.BetterBibTeX.getString(`Preferences.auto-export.status.${status}`) || status
            if (ae.error) status += `: ${ae.error}`
            treecell.setAttribute('label', status)
            break

          case 'updated':
            treecell.setAttribute('label', `${new Date(ae.meta.updated || ae.meta.created)}`)
{'fraction_non_alphanumeric': 0.13525530694205393, 'fraction_numerical': 0.003155479059093517, 'mean_word_length': 3.4301143583227445, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '3181309', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1974, 'n_words': 405}