/**
 * 判断对象类型
 * @param {*} obj 对象
 */
function objTypeOf(obj) {
  const toString = Object.prototype.toString;
  const map = {
    "[object Boolean]": "boolean",
    "[object Number]": "number",
    "[object String]": "string",
    "[object Function]": "function",
    "[object Array]": "array",
    "[object Date]": "date",
    "[object RegExp]": "regExp",
    "[object Undefined]": "undefined",
    "[object Null]": "null",
    "[object Object]": "object",
  };
  return map[toString.call(obj)];
}

/**
 * 深拷贝
 * @param {Object/Array} data 需要拷贝的数据
 */
function deepCopy(data) {
  return JSON.parse(JSON.stringify(data));
}

/**
 * 将目标对象中有的属性值与源对象中的属性值合并
 * @param {Object} target 目标对象
 * @param {Object} sources 源对象
 */
function objAssign(target, sources) {
  let targ = { ...target };
  for (let k in targ) {
    targ[k] = Object.prototype.hasOwnProperty.call(sources, k)
      ? sources[k]
      : targ[k];
  }
  return targ;
}

/**
 * 获取随机code,默认获取16位
 * @param {Number} len 推荐8的倍数
 *
 */
function randomCode(len = 16) {
  if (len <= 0) return;
  let steps = Math.ceil(len / 8);
  let stepNums = [];
  for (let i = 0; i < steps; i++) {
    let ranNum = Math.random().toString(32).slice(-8);
    stepNums.push(ranNum);
  }

  return stepNums.join("");
}

/**
 *
 * @param {String} format 时间格式
 * @param {Date} date 需要格式化的时间对象
 */
function formatDate(format = "YYYY/MM/DD HH:mm:ss", date = new Date()) {
  if (objTypeOf(date) !== "date") return;
  const options = {
    "Y+": date.getFullYear(),
    "M+": date.getMonth() + 1,
    "D+": date.getDate(),
    "H+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  Object.entries(options).map(([key, val]) => {
    if (new RegExp("(" + key + ")").test(format)) {
      const zeros = key === "Y+" ? "0000" : "00";
      const value = (zeros + val).substr(("" + val).length);
      format = format.replace(RegExp.$1, value);
    }
  });
  return format;
}

/**
 * 获取本地时间，格式：年月日时分秒
 */
function localNowDateTime() {
  return formatDate("YYYY年MM月DD日HH时mm分ss秒");
}

/**
 * 获取指定元素个数的数组
 * @param {Number} num
 */
function getNumList(num) {
  return "#".repeat(num).split("");
}

/**
 * 清除html标签
 * @param {String} str html字符串
 */
function removeHtmlTag(str) {
  return str.replace(/<[^>]+>/g, "");
}

/**
 * 计算总数
 * @param {Array} dataList 需要统计的数组
 */
function calcSum(dataList) {
  return dataList.reduce(function (total, item) {
    return total + item;
  }, 0);
}

function isEmptyObject(obj) {
  return !Object.keys(obj).length;
}

function getElementId() {
  return `element-${randomCode()}`;
}

function numberIsOdd(num) {
  return !!(num % 2);
}

/**
 * 百以下数字转中文汉字
 * @param {Number} num 大于0的100以下数字，其他数值直接转字符串
 *
 * @returns {String}
 */
function numberToChinese(num) {
  if (num >= 100 || num <= 0) return num + "";
  const cnNums = "一二三四五六七八九十".split("");
  if (num <= 10) return cnNums[num - 1];

  return (num + "")
    .split("")
    .map((item) => item * 1)
    .map((item, index) => {
      if (index) {
        return !item ? "" : cnNums[item - 1];
      } else {
        return item === 1 ? "" : cnNums[item - 1];
      }
    })
    .join("十");
}

export {
  objTypeOf,
  deepCopy,
  objAssign,
  randomCode,
  formatDate,
  localNowDateTime,
  getNumList,
  removeHtmlTag,
  calcSum,
  isEmptyObject,
  getElementId,
  numberIsOdd,
  numberToChinese,
};
