const deepmerge = require("deepmerge");

/**
 * 判断对象类型
 * @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",
    "[object FormData]": "formData"
  };
  return map[toString.call(obj)];
}

/**
 * 深拷贝
 * @param {Object/Array} data 需要拷贝的数据
 */
function deepCopy(data, options) {
  const defObj = objTypeOf(data) === "array" ? [] : {};
  return deepmerge(defObj, data, options || {});
}

/**
 * 将目标对象中有的属性值与源对象中的属性值合并
 * @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 {Object} params 参数对象
 */
function qsParams(params) {
  return Object.entries(params)
    .map(([key, val]) => `${key}=${val}`)
    .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;
}

/**
 * 获取本地日期，格式YYYY-M-D
 */
function getLocalDate() {
  return formatDate("YYYY-MM-DD");
}

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

/**
 *  获取时间长度文字
 * @param {Number} timeNumber 时间数值，单位：毫秒
 */
function timeNumberToText(timeNumber) {
  const DAY_TIME = 24 * 60 * 60 * 1000;
  const HOUR_TIME = 60 * 60 * 1000;
  const MINUTE_TIME = 60 * 1000;
  const SECOND_TIME = 1000;
  let [day, hour, minute, second] = [0, 0, 0, 0];
  let residueTime = timeNumber;

  if (residueTime >= DAY_TIME) {
    day = Math.floor(residueTime / DAY_TIME);
    residueTime -= day * DAY_TIME;
    day += "天";
  }
  if (residueTime >= HOUR_TIME) {
    hour = Math.floor(residueTime / HOUR_TIME);
    residueTime -= hour * HOUR_TIME;
    hour += "小时";
  }
  if (residueTime >= MINUTE_TIME) {
    minute = Math.floor(residueTime / MINUTE_TIME);
    residueTime -= minute * MINUTE_TIME;
    minute += "分";
  }
  if (residueTime >= SECOND_TIME) {
    second = Math.round(residueTime / SECOND_TIME);
    second += "秒";
  }

  return [day, hour, minute, second].filter(item => !!item).join("");
}

export {
  objTypeOf,
  deepCopy,
  objAssign,
  randomCode,
  qsParams,
  formatDate,
  removeHtmlTag,
  getLocalDate,
  timeNumberToText
};
