const {
  windowHeight,
  titleBarHeight,
  statusBarHeight
} = my.getSystemInfoSync();
/**
 * 时间搓分开返回日期、时间、星期
 * @param time
 * @returns {{date: string, time: string, am: (string), day: string}}
 */

export function cuttingDate(time = '') {
  const d = time ? new Date(time.replace(/-/g, '/')) : new Date();
  const hours = d.getHours();
  return {
    date: d.toLocaleDateString().replace(/\//g, '-'),
    day: getWeek(d),
    am: hours < 12 ? '上午' : '下午',
    type: hours < 12 ? 'am' : 'pm',
    time: `${`0${hours}`.slice(-2)}:${`0${d.getMinutes()}`.slice(-2)}`
  };
}
/**
 * 返回星期
 * @param date
 * @returns {string}
 */

export function getWeek(date) {
  const d = Object.prototype.toString.call(date) === '[object Date]' ? date : new Date(date);
  return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][d.getDay()];
}

const excludeEmptyValue = data => {
  const obj = {};
  Object.keys(data).forEach(key => {
    const value = data[key];
    if (value !== undefined) obj[key] = value;
  });
  return obj;
};

export const queryToUrl = query => Object.keys(excludeEmptyValue(query)).reduce((sum, item) => `${sum}&${item}=${query[item]}`, '');
export const getHeaderHeight = () => titleBarHeight + statusBarHeight;
export const getContentHeight = () => windowHeight - getHeaderHeight();
export const ID_CARD_18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
export const ID_CARD_15 = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/;
export const PHONE_CHECK = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
/* 校验身份证 */

export function checkIdCard(idCardNo) {
  return ID_CARD_18.test(idCardNo) || ID_CARD_15.test(idCardNo) || idCardNo.indexOf('*') > -1;
}
/* 校验手机 */

export function checkPhone(phoneNumber) {
  return PHONE_CHECK.test(phoneNumber) || phoneNumber.indexOf('*') > -1;
} // 防抖函数

export function debounce(fn, delay = 0) {
  let timer;
  return function _(...args) {
    clearTimeout(timer);
    const context = this;
    timer = setTimeout(() => {
      fn.apply(context, [...args, context]);
    }, delay);
  };
}