function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import { Decimal } from 'decimal.js';
import { ModeEnum, EftposEnum } from "./const";
/* eslint-disable no-bitwise */
export function getUuid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0;
    var v = c === 'x' ? r : r & 0x3 | 0x8;
    return v.toString(16);
  });
}
/**
 * @title: Promise化 setTimeout
 * @param {number} time
 * @Author: hongbing.wang
 * @Date: 2024-08-20 13:03
 */
export var sleep = function sleep(time) {
  return new Promise(function (resove) {
    setTimeout(function () {
      resove(undefined);
    }, time);
  });
};

/**
 * @title: 克隆
 * @Author: hongbing.wang
 * @Date: 2024-08-20 13:03
 */
export function cloneDeep(source) {
  var hash = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new WeakMap();
  if (_typeof(source) !== 'object' || source === null) {
    return source;
  }
  if (hash.has(source)) {
    return hash.get(source);
  }
  var target = Array.isArray(source) ? [] : {};
  Reflect.ownKeys(source).forEach(function (key) {
    var val = source[key];
    if (_typeof(val) === 'object' && val != null) {
      target[key] = cloneDeep(val, hash);
    } else {
      target[key] = val;
    }
  });
  return target;
}
/**
 * @title: 获取支付回调参数
 * @Author: hongbing.wang
 * @Date: 2024-08-20 20:26
 */
export var getPayParams = function getPayParams(data, statue) {
  var _data$custom;
  if (statue === 'fail') {
    return {
      number: data.number,
      device: data.device,
      order_id: data.order_id
    };
  }
  var receipt = (data === null || data === void 0 || (_data$custom = data.custom) === null || _data$custom === void 0 ? void 0 : _data$custom.receipt) || [];
  if (data.mode === ModeEnum.Pay || data.mode === ModeEnum.FullPay) {
    var _data$custom2, _data$device;
    var pay = data.pay;
    var card_reader_surcharge = ((_data$custom2 = data.custom) === null || _data$custom2 === void 0 ? void 0 : _data$custom2.card_reader_surcharge) || 0;
    var obj = pay || {};
    var cardReaderSurcharge = card_reader_surcharge;
    // 卡手续费
    var surCardDeci = new Decimal(Number(cardReaderSurcharge));

    // 手续费-固定金额
    var surMoneyDeci = new Decimal(Number((obj === null || obj === void 0 ? void 0 : obj.surMoney) || 0));

    // 手续费-比例金额
    var surRateMoneyDeci = new Decimal(Number(obj === null || obj === void 0 ? void 0 : obj.surRateMoney));

    // 手续费-固定金额 + 卡手续费  = 固定金额总额
    var surMoneyTotal = surMoneyDeci.add(surCardDeci);

    // 手续费-固定金额 + 卡手续费 + 手续费-比例金额  = 总手续费
    var surchargeDeci = surMoneyTotal.add(surRateMoneyDeci);
    var amount = new Decimal(Number(obj === null || obj === void 0 ? void 0 : obj.amount));
    var total = surchargeDeci.add(amount);
    return _objectSpread(_objectSpread({}, obj), {}, {
      cardReaderSurcharge: cardReaderSurcharge,
      surMoney: surMoneyTotal.toFixed(2, Decimal.ROUND_HALF_UP),
      surcharge: surchargeDeci.toFixed(2, Decimal.ROUND_HALF_UP),
      total: total.toFixed(2, Decimal.ROUND_HALF_UP),
      number: data.number,
      uniquePaymentNumber: data.number,
      device: data.device,
      order_id: data.order_id,
      receipt: (_data$device = data.device) !== null && _data$device !== void 0 && (_data$device = _data$device.setting) !== null && _data$device !== void 0 && _data$device.receipts ? [] : receipt
    });
  }
  if (data.mode === ModeEnum.Refund) {
    var _data$device2;
    return {
      amount: data.amount,
      total: data.amount,
      number: data.number,
      device: data.device,
      order_id: data.order_id,
      receipt: (_data$device2 = data.device) !== null && _data$device2 !== void 0 && (_data$device2 = _data$device2.setting) !== null && _data$device2 !== void 0 && _data$device2.receipts ? [] : receipt
    };
  }
  return {
    amount: data.amount,
    total: data.amount,
    number: data.number,
    order_id: data.order_id
  };
};

// inputNumber过滤
export var limitDecimals = function limitDecimals(value) {
  var reg = /^(\-)*(\d+)\.(\d\d).*$/;
  if (typeof value === 'string') {
    return !isNaN(Number(value)) ? value.replace(reg, '$1$2.$3') : '';
  }
  if (typeof value === 'number') {
    return !isNaN(value) ? String(value).replace(reg, '$1$2.$3') : '';
  }
  return '';
};

/**
 * @title:  通用获取 金额信息
 * @description: 在 amount 页面设置则使用 pay ,如果有 setting 则使用 setting 信息 ,都没有则设置0
 * @param {State} store : 全局store数据管理
 * @Author: hongbing.wang
 * @Date: 2024-08-12 10:24
 */
export function getAmonunt(store) {
  var device = store.device,
    pay = store.pay,
    name = store.name,
    symbol = store.symbol,
    amount = store.amount,
    eftpos = store.eftpos;
  if (!device) {
    throw Error('device is not defined');
  }
  var setting = device.setting;

  // 从 金额编辑页 获取到 支付数据
  if (pay) {
    return {
      name: name,
      symbol: symbol,
      // format,
      money: Number(amount),
      amount: Number(pay.amount),
      surRate: Number(pay.surRate),
      surMoney: Number(pay.surMoney),
      surcharge: (setting === null || setting === void 0 ? void 0 : setting.surcharge) || false,
      useOriginSurcharge: (pay === null || pay === void 0 ? void 0 : pay.useOriginSurcharge) || false
    };
  }
  // 原始支付总额
  if (!setting) {
    return {
      name: name,
      symbol: symbol,
      // format,
      money: Number(amount),
      amount: Number(amount),
      surRate: 0,
      surMoney: 0,
      surcharge: false,
      useOriginSurcharge: false
    };
  }
  // 原始手续费 是指 各个pos机厂商跟银行合作直接收取的手续费,一旦设置则清空,配置中设置的手续费
  var useOriginSurcharge = eftpos === EftposEnum.Tyro ? setting.useOriginSurcharge || false : false;

  // 直接设备中手续费金额
  var surRate = setting.constant_rate || 0;
  var surMoney = setting.constant_money || 0;

  // 是否开启手续费
  var surcharge = setting.surcharge;
  return {
    name: name,
    symbol: symbol,
    money: Number(amount),
    amount: Number(amount),
    surRate: useOriginSurcharge ? 0 : surcharge ? surRate : 0,
    surMoney: useOriginSurcharge ? 0 : surcharge ? surMoney : 0,
    surcharge: surcharge,
    useOriginSurcharge: useOriginSurcharge
  };
}
export function getPayAmonunt(_ref, formatAmount) {
  var money = _ref.money,
    amount = _ref.amount,
    surRate = _ref.surRate,
    surMoney = _ref.surMoney;
  var surRateDeci = new Decimal(surRate);
  var moneyDeci = new Decimal(amount);
  var surMoneyDeci = new Decimal(surMoney);
  var surRateMoney = moneyDeci.mul(surRateDeci.div(100)).toFixed(2, Decimal.ROUND_HALF_UP);
  var totalSurMoney = moneyDeci.mul(surRateDeci.div(100)).add(surMoneyDeci);
  var rate = new Decimal(surRate).div(100).toFixed(4, Decimal.ROUND_HALF_UP);
  var total = totalSurMoney.add(moneyDeci).toFixed(2, Decimal.ROUND_HALF_UP);
  return {
    sourceAmount: "".concat(money),
    amount: "".concat(amount),
    surcharge: "".concat(totalSurMoney),
    total: "".concat(total),
    surPercentRate: "".concat(rate),
    surRate: "".concat(surRate),
    surMoney: "".concat(surMoney),
    surRateMoney: "".concat(surRateMoney),
    totalUnit: formatAmount(total)
  };
}
export function getBrowserVersion() {
  var Sys = {};
  var ua = navigator.userAgent.toLowerCase();
  var s;
  (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] : (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/edge\/([\d\.]+)/)) ? Sys.edge = s[1] : (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/version\/([\d\.]+).*safari/)) ? Sys.safari = s[1] : 0;
  // 根据关系进行判断
  if (Sys.ie) return 'IE: ' + Sys.ie;
  if (Sys.edge) return 'EDGE: ' + Sys.edge;
  if (Sys.firefox) return 'Firefox: ' + Sys.firefox;
  if (Sys.chrome) return 'Chrome: ' + Sys.chrome;
  if (Sys.opera) return 'Opera: ' + Sys.opera;
  if (Sys.safari) return 'Safari: ' + Sys.safari;
  return 'Unkonwn';
}