import getValue from '../system/getValue';
import {pCC} from '../support/layout';

function hasTextEntry() { // jQuery
  $('#pCC form').first().attr('id', 'dochat');
  $('#pCC input').slice(0, 7).each(function(i, e) {
    $(e).attr('form', 'dochat');
  });
  var theTable = $('#pCC table table').first();
  theTable.append('<tr id="fshMass"></tr>');
  $('td', theTable).eq(0).remove();
  var btnMass = $('input[value="Send As Mass"]', theTable);
  if (btnMass.length === 1) {
    btnMass.appendTo('#fshMass', theTable);
  }
  var ourTd = $('td', theTable).eq(0);
  ourTd.attr('rowspan', '2');
  $('input', ourTd).replaceWith('<textarea id="fshTxt" name="msg" cols' +
    '="72" rows="2" form="dochat" style="resize: none"></textarea>');
  var fshTxt = $('#fshTxt', ourTd);
  fshTxt.keydown(function(e) {
    if (e.keyCode === 13 && fshTxt.val() !== '') {
      $('input[value=Send]', theTable).click();
      return false;
    }
  });
}

export default function addChatTextArea() {
  if (!getValue('enhanceChatTextEntry') ||
      !pCC) {return;}
  hasTextEntry();
}

@@ -935,105 +935,249 @@
 %0A%7D%0A%0A
-export default function addChatTextArea() %7B%0A  if (!getValue('enhanceChatTextEntry') %7C%7C%0A      !pCC
+var tests = %5B%0A  function() %7Breturn !getValue('enhanceChatTextEntry');%7D,%0A  function() %7Breturn !pCC;%7D,%0A  function() %7Breturn typeof $ !== 'function';%7D%0A%5D;%0A%0Aexport default function addChatTextArea() %7B%0A  if (tests.some(function(el) %7Breturn el();%7D)
 ) %7Br

test for jQuery in addChatTextArea