/* eslint-disable no-mixed-operators */

/* eslint-disable no-bitwise */
import { fetch } from 'fishx';
/**
 * 安全获取数据
 * @param {function} func 获取函数
 * @param {any} defaults 默认值
 */

export function ensure(func, defaults) {
  try {
    return func() || defaults;
  } catch (error) {
    return defaults;
  }
}
export function toast(params) {
  if (!window.MParams || !window.MParams.api || !window.MParams.api.toast) {
    return;
  }

  window.MParams.api.toast(params);
}
/**
 * 计算折扣
 * @param {number} price 价格
 * @param {number} oldPrice 划线价
 * @returns int
 */

export var calcDiscount = function calcDiscount(price, oldPrice) {
  return ~~((oldPrice - price) * 100 / oldPrice);
}; // 校验文件名称,不能存在 / \ ,

export var checkFileName = function checkFileName(fileName) {
  if (fileName === undefined || fileName === null || fileName === '') {
    return false;
  }

  var reg = /[\\,/]+/;

  if (reg.test(fileName)) {
    return false;
  }

  return true;
};
/**
 * fish fetch
 * @param {*} url 请求地址
 * @param {*} data 入参
 * @param {*} method 请求方法
 */

export function fishFetch(url) {
  var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'GET';

  var _method = method.toUpperCase();

  if (_method === 'GET') {
    return fetch(url, {
      method: _method,
      params: data
    });
  }

  if (_method === 'POST') {
    return fetch(url, {
      method: _method,
      data: data
    });
  }

  return null;
}
/**
 * 获取图片URL
 * @param {object} imageObj 图片对象
 * @param {string?} type small | big
 */

export function convertImageUrl() {
  var imageObj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var type = arguments.length > 1 ? arguments[1] : undefined;
  if (!imageObj) return '';
  var typeMap = {
    small: 'smallImgUrl',
    big: 'bigImgUrl'
  }[type];
  return ensure(function () {
    return encodeURI(imageObj[typeMap] || imageObj.url);
  }, '');
}
/**
 * 补0 函数
 * @param {*} num 数字
 */

export var addZero = function addZero(num) {
  return String(num).length === 1 ? "0".concat(String(num)) : String(num);
};
/**
 * 计算倒计时
 * @param {number} lastTime 剩余时间 单位：毫秒
 * @param {string?} type 'day' 以日为单位
 * @returns
 */

export var calcLastTime = function calcLastTime(lastTime, type) {
  if (lastTime <= 0) {
    return 0;
  }

  var day = ~~(lastTime / 1000 / 60 / 60 / 24);
  var hours = ~~(lastTime / 1000 / 60 / 60);
  var mines = ~~(lastTime / 1000 / 60 % 60);
  var second = ~~(lastTime / 1000 % 60);

  if (type === 'day') {
    hours = ~~(lastTime / 1000 / 60 / 60 % 24);
    return "".concat(day, "DAYS  ").concat(addZero(hours), ":").concat(addZero(mines), ":").concat(addZero(second));
  }

  if (type === '_hour') {
    return hours;
  }

  if (type === '_mines') {
    return mines;
  }

  return "".concat(addZero(hours), ":").concat(addZero(mines), ":").concat(addZero(second));
};
export var navigateTo = function navigateTo(url) {
  if (!window.MParams || !window.MParams.history) {
    window.location.href = url;
    return;
  }

  window.MParams.history.push(url);
};