"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.dealWithLoadedFileTips = dealWithLoadedFileTips;
exports.generateSelectedMark = generateSelectedMark;
exports.getByteLen = getByteLen;
exports.htmlDecodeByRegExp = htmlDecodeByRegExp;
exports.htmlEncodeByRegExp = htmlEncodeByRegExp;
exports.insertTextAtPointer = insertTextAtPointer;
exports.textareaAutoResizing = textareaAutoResizing;

// 修改textarea高度
function textareaAutoResizing(id, initialTextHeight) {
  var observe;
  var isInit = true; // @ts-ignore

  if (window.attachEvent) {
    observe = function observe(element, event, handler) {
      element.attachEvent("on".concat(event), handler);
    };
  } else {
    observe = function observe(element, event, handler) {
      element.addEventListener(event, handler, false);
    };
  }

  function init() {
    // @ts-ignore
    var text = document.getElementById(id);

    function resize() {
      // @ts-ignore
      text.style.height = "auto"; // @ts-ignore

      text.style.height = isInit ? "".concat(initialTextHeight, "px") : text.scrollHeight > initialTextHeight ? "".concat(text.scrollHeight, "px") : "".concat(initialTextHeight, "px");
      isInit = false;
    }
    /* 0-timeout to get the already changed text */


    function delayedResize() {
      window.setTimeout(resize, 0);
    }

    observe(text, "change", resize);
    observe(text, "cut", delayedResize);
    observe(text, "paste", delayedResize);
    observe(text, "drop", delayedResize);
    observe(text, "keydown", delayedResize); // @ts-ignore

    text.focus(); // @ts-ignore
    // text.select();

    resize();
  }

  init();
} // 在textraea的光标处插入指定内容


function insertTextAtPointer(textEle, insertValue) {
  var start = textEle.selectionStart;
  var preValue = textEle.value;
  return preValue.substring(0, start) + insertValue + preValue.substring(start);
} // 分享文案


function dealWithLoadedFileTips(content, num) {
  var index = content.match(/分享了\d+个文件/);
  if (!index && num > 0) return "".concat(content, "\u5206\u4EAB\u4E86").concat(num, "\u4E2A\u6587\u4EF6");
  if (num == 0) return content.replace(/分享了\d+个文件/, "");
  return content.replace(/分享了\d+个文件/, "\u5206\u4EAB\u4E86".concat(num, "\u4E2A\u6587\u4EF6"));
} // 生成@人的字符串


function generateSelectedMark(users, textEle) {
  var arr = users.map(function (item) {
    if (item.hasOwnProperty("Name")) return "[".concat(item.Name, ":").concat(item.Email, "]"); // 人

    return "[".concat(item.DepartmentName, ":").concat(item.Id, ":dep_members]"); // 部门
  });
  var start = textEle.selectionStart;
  var value = textEle.value;
  if (value.charAt(start - 1) == "@") return arr.join(" @");
  return "@".concat(arr.join(" @"));
} // 检测字符数 一个汉字两个字符


function getByteLen(val) {
  var len = 0;
  var en = 0; // 英文数

  var cn = 0; // 汉字数

  for (var i = 0; i < val.length; i++) {
    var a = val.charAt(i);

    if (a.match(/[^\x00-\xff]/ig) != null) {
      len += 2;
      ++cn;
    } else {
      len += 1;
      ++en;
    }
  }

  return {
    len: len,
    en: en,
    cn: cn
  };
} // html标签转义


function htmlEncodeByRegExp(str) {
  var temp = "";
  if (str.length == 0) return "";
  temp = str.replace(/</g, "&lt;");
  temp = temp.replace(/>/g, "&gt;");
  temp = temp.replace(/\'/g, "&#39;");
  temp = temp.replace(/\"/g, "&quot;");
  temp = temp.replace(/\n/g, "<br/>");
  return temp;
}

function htmlDecodeByRegExp(str) {
  var temp = "";
  if (str.length == 0) return "";
  temp = str.replace(/&amp;/g, "&");
  temp = temp.replace(/&lt;/g, "<");
  temp = temp.replace(/&gt;/g, ">");
  temp = temp.replace(/&nbsp;/g, " ");
  temp = temp.replace(/&#39;/g, "'");
  temp = temp.replace(/&quot;/g, "\"");
  temp = temp.replace(/<br\/>/g, "\n");
  return temp;
}