<commit_before>import plugins from '../plugins/'
import config from 'lib/config'
import { shell, clipboard, remote } from 'electron'
import store from '../store'

import {
 UPDATE_TERM,
 MOVE_CURSOR,
 SELECT_ELEMENT,
 SHOW_RESULT,
 HIDE_RESULT,
 UPDATE_RESULT,
 RESET,
 CHANGE_VISIBLE_RESULTS,
} from '../constants/actionTypes'

/**
 * Default scope object would be first argument for plugins
 *
 * @type {Object}
 */
const DEFAULT_SCOPE = {
  config,
  actions: {
    open: (q) => shell.openExternal(q),
    reveal: (q) => shell.showItemInFolder(q),
    copyToClipboard: (q) => clipboard.writeText(q),
    replaceTerm: (term) => store.dispatch(updateTerm(term)),
    hideWindow: () => remote.getCurrentWindow().hide()
  }
}

/**
 * Pass search term to all plugins and handle their results
 * @param {String} term Search term
 * @param {Function} callback Callback function that receives used search term and found results
 */
const eachPlugin = (term, display) => {
  // TODO: order results by frequency?
  Object.keys(plugins).forEach(name => {
    try {
      plugins[name].fn({
        ...DEFAULT_SCOPE,
        term,
        hide: (id) => store.dispatch(hideElement(`${name}-${id}`)),
        update: (id, result) => store.dispatch(updateElement(`${name}-${id}`, result)),
        display: (payload) => display(name, payload)
      })
    } catch (error) {
      // Do not fail on plugin errors, just log them to console
      console.log('Error running plugin', name, error)
    }
  })
}


/**
 * Handle results found by plugin<commit_msg>Make plugin settings available to the plugin<commit_after>import plugins from '../plugins/'
import config from 'lib/config'
import { shell, clipboard, remote } from 'electron'
import store from '../store'

import {
 UPDATE_TERM,
 MOVE_CURSOR,
 SELECT_ELEMENT,
 SHOW_RESULT,
 HIDE_RESULT,
 UPDATE_RESULT,
 RESET,
 CHANGE_VISIBLE_RESULTS,
} from '../constants/actionTypes'

/**
 * Default scope object would be first argument for plugins
 *
 * @type {Object}
 */
const DEFAULT_SCOPE = {
  config,
  actions: {
    open: (q) => shell.openExternal(q),
    reveal: (q) => shell.showItemInFolder(q),
    copyToClipboard: (q) => clipboard.writeText(q),
    replaceTerm: (term) => store.dispatch(updateTerm(term)),
    hideWindow: () => remote.getCurrentWindow().hide()
  }
}

/**
 * Pass search term to all plugins and handle their results
 * @param {String} term Search term
 * @param {Function} callback Callback function that receives used search term and found results
 */
const eachPlugin = (term, display) => {
  const externalSettings = config.get('external') || {}
  // TODO: order results by frequency?
  Object.keys(plugins).forEach(name => {
    const settings = externalSettings[name]
    if (settings) {
      Object.keys(settings).forEach(key => settings[key] = settings[key].value)
    }

    try {
      plugins[name].fn({
        ...DEFAULT_SCOPE,
        term,
        hide: (id) => store.dispatch(hideElement(`${name}-${id}`)),
        update: (id, result) => store.dispatch(updateElement(`${name}-${id}`, result)),
        display: (payload) => display(name, payload),
        settings,
      })
    } catch (error) {
      // Do not fail on plugin errors, just log them to console
      console.log('Error running plugin', name, error)
    }
  })
}


/**
 * Handle results found by plugin
{'fraction_non_alphanumeric': 0.14371078577830892, 'fraction_numerical': 0.0, 'mean_word_length': 3.452127659574468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '5738792', 'n_tokens_mistral': 1017, 'n_tokens_neox': 999, 'n_words': 287}