export function copyToClipboard(copyValue, currentSelector) {
  const $temp = $('<textarea>').css({
    position: 'absolute',
    top: 0,
    left: 0,
    opacity: 0
  });
  $(currentSelector).parent().append($temp);
  const str = copyValue.replace(/(^\w|\s\w)/g, function (m) {
    return m.toUpperCase()
  });
  const textFiltered = str.split(' ').filter(function (value) {
    return value !== ''
  });

  const textTrimmed = textFiltered.join(' ');
  $temp.val(textTrimmed);

  if (navigator.userAgent.match(/TradeProMobile_iOS|DIYMobile_iOS/i)) {
    iosCopyToClipboard($temp);
  } else if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
    $temp.select();
    $temp[0].setSelectionRange(0, 999999);
  } else {
    $temp.select();
  }

  document.execCommand("copy");
  $temp.remove();
}

function iosCopyToClipboard($input) {
  const el = $input.get(0);
  const editable = el.contentEditable;
  const readOnly = el.readOnly;
  const range = document.createRange();
  const sel = window.getSelection();

  el.contentEditable = true;
  el.readOnly = false;
  range.selectNodeContents(el);
  sel.removeAllRanges();
  sel.addRange(range);
  el.setSelectionRange(0, 999999);
  el.contentEditable = editable;
  el.readOnly = readOnly;
}
