text: import arrayFrom from './arrayFrom';
  import closestTable from './closestTable';
  import contains from './contains';
  import createDiv from './cElement/createDiv';
  import getArrayByTagName from './getArrayByTagName';
  import getText from './getText';
  import getTextTrim from './getTextTrim';
  import insertHtmlBeforeBegin from './insertHtmlBeforeBegin';
  import insertHtmlBeforeEnd from './insertHtmlBeforeEnd';
  import partial from './partial';
  
  function cellOneHazText(curr) {
    return curr.cells[1] && getText(curr.cells[1]);
  }
  
  function reduceStatTable(acc, curr, index) {
    const key = getTextTrim(curr.cells[0]).replace(':', '');
    if (!key) { return acc; }
    acc[key] = { ind: index };
    if (cellOneHazText(curr)) {
      acc[key].value = Number(getTextTrim(curr.cells[1]).replace('+', ''));
    }
    return acc;
  }
  
  function getVal(prop, obj) {
    if (obj[prop] && obj[prop].value) {
      return obj[prop].value;
    }
    return 0;
  }
  
  function getLastIndex(obj, tbl) {
    if (obj.Enhancements) {
      return tbl.rows[obj.Enhancements.ind - 1];
    }
    return tbl.rows[tbl.rows.length - 1];
  }
  
  function sum(statObj, acc, curr) { return acc + getVal(curr, statObj); }
  
  function calcTotalStats(statObj) {
    return ['Attack', 'Defense', 'Armor', 'Damage', 'HP']
      .reduce(partial(sum, statObj), 0);
  }
  
  function addStats(el) {
    const statTable = closestTable(el);
    const statObj = arrayFrom(statTable.rows).reduce(reduceStatTable, {});
    const totalStats = calcTotalStats(statObj);
    insertHtmlBeforeBegin(getLastIndex(statObj, statTable),
      `<tr class="fshDodgerBlue"><td>Stat Total:</td><td align="right">${
        totalStats}&nbsp;</td></tr>`);
  }
  
  function fshDataFilter(data) {
    const container = createDiv();
    insertHtmlBeforeEnd(container, data);
    getArrayByTagName('font', container).filter(contains('Bonuses'))
      .forEach(addStats);
    return container.innerHTML;
  }
  
  function fshPreFilter(options) {
    if (options.url.startsWith('fetchitem')) {
      // eslint-disable-next-line no-param-reassign
      options.dataFilter = fshDataFilter;
    }
  }
  
  export default function addStatTotalToMouseover() { // jQuery
    $.ajaxPrefilter(fshPreFilter);
  }
  