/**
 * @description: 防抖
 * @param {Function} fn
 * @param {Number} delays
 * @returns {Function}
 * @author: 无尘
 */
export function _debounce(fn, delays) {
  let delay = delays || 200;
  let timer;
  // console.log(fn)
  return function() {
    let that = this;
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function() {
      timer = null;
      fn.apply(that, args);
    }, delay);
  };
}
/**
 * @description: 节流
 * @param {Function} fn
 * @param {Number} intervals
 * @returns {Function}
 * @author: 无尘
 */
export function _throttle(fn, intervals) {
  let last;
  let timer;
  let interval = intervals || 200;
  return function() {
    let that = this;
    let args = arguments;
    let now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(function() {
        last = now;
        fn.apply(that, args);
      }, interval);
    } else {
      last = now;
      fn.apply(that, args);
    }
  };
}

/**
 * @description: 手机号格式化
 * @param {String} phone
 * @returns {String}
 * @author: 无尘
 */
export function formatPhone(phone) {
  phone = phone.toString();
  return phone.substr(0, 3) + '****' + phone.substr(7, 11);
}

/**
 * @description: 时间戳格式化
 * @param {Number} num
 * @author: 无尘
 */

function formatDig(num) {
  return num > 9 ? '' + num : '0' + num;
}
export function formatDate(time) {
  let now = new Date(time);
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  let data = year + '-' + formatDig(month) + '-' + formatDig(date) + ' ' + formatDig(hour) + ':' + formatDig(minute) + ':' + formatDig(second);
  return data;
}

/**
 * @description: 千位分割
 * @param {Number} num
 * @returns {String}
 * @author: 无尘
 */
export function formatNum(num) {
  let number = num.toString().split('.'); // 分隔小数点
  let dot = '0.' + (number[1] || 0);
  let reg = /\d{1,3}(?=(\d{3})+$)/g;
  return (
    (number[0] + '').replace(reg, '$&,') +
    '.' +
    Number(dot)
      .toFixed(2)
      .toString()
      .split('.')[1]
  );
}

/**
 * @description: 重复判断
 * @param {Array} arr
 * @returns {Boolean}
 * @author: 无尘
 */
export function isRepeat(arr) {
  let hash = {};
  for (let i in arr) {
    if (hash[arr[i]]) {
      return true;
    }
    hash[arr[i]] = true;
  }
  return false;
}
/**
 * @description: json < -- - > jsonSchema， 如果传入的是对象， 处理对象， 返回结果
 * @param {Object} object
 * @param {Object} output
 * @param {Boolean} nested
 * @returns {Array}
 * @author: 无尘
 */
export function processObject(object, output, nested) {
  let arr = [];
  for (let key in object.properties) {
    let value = object.properties[key]; // 拿到相当于数组的一列数据
    // 如果这一行数据的 type 是 object
    if (value.type == 'object' && Object.keys(value.properties).length) {
      value.children = [];
      value.children = processObject(value);
      arr.push(value);
      continue;
    }
    // 如果这一行数据的 type 是 array
    if (value.type == 'array' && Object.keys(value.items.properties).length) {
      value.children = [];
      value.children = processObject(value.items);
      arr.push(value);
      continue;
    }
    arr.push(value);
  }
  return arr;
}
