/* eslint-disable arrow-parens */
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();
/**
 * 判断是否是一个Boolean
 * @param val
 */

export function isBoolean(val) {
  return Object.prototype.toString.call(val) === '[object Boolean]';
}
/**
 * deliverType 是否可以结算
 * 参数 在售状态，库存状态，库存数量, 限购
 * @params { status, stockStatus, stock }
 */

export const buyIsAvailable = list => {
  const typeObj = {
    AUTO: 0,
    MANUAL: 0,
    DELIVERY: 0
  };
  list.map(v => {
    const {
      deliverType = ''
    } = v;
    Object.keys(typeObj).map(typeValue => {
      if (deliverType.indexOf(typeValue) > -1) {
        typeObj[typeValue] += 1;
      }

      return '';
    });
    return '';
  });
  console.log(typeObj); // 自动和手动不能一起

  if (typeObj.AUTO * typeObj.MANUAL) {
    return false;
  }

  return true;
};
/**
 * 是否可买
 * 参数 在售状态，库存状态，库存数量, 限购
 * @params { status, stockStatus, stock }
 */

export const isActive = goodsItem => {
  const {
    status,
    // 是否停售
    stockStatus = 'N',
    // 是否有库存
    stock = 0,
    buyLimitCount = 1,
    quantity = 0,
    buyLimitType = '' // 是否限购

  } = goodsItem; // 不在售

  if (status === 'N') {
    return false;
  } // 库存为 0 不能买


  if (stockStatus === 'Y' && !stock) {
    return false;
  } // 限购了


  if (buyLimitType === 'GENERAL' && quantity >= buyLimitCount) {
    return false;
  }

  return true;
};
/**
 * 获取距离信息
 * 参数 meter
 * @param
 */

export const getMeter = meter => {
  const number = Number(meter);

  if (Number.isNaN(number)) {
    return meter;
  }

  if (number < 1000) {
    return `约${number.toFixed(0)}m`;
  } else {
    return `约${(number / 1000).toFixed(2)}km`;
  }
};