var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/helper.ts
var helper_exports = {};
__export(helper_exports, {
  cloneDeep: () => cloneDeep,
  getAmonunt: () => getAmonunt,
  getBrowserVersion: () => getBrowserVersion,
  getPayAmonunt: () => getPayAmonunt,
  getPayParams: () => getPayParams,
  getUuid: () => getUuid,
  limitDecimals: () => limitDecimals,
  sleep: () => sleep
});
module.exports = __toCommonJS(helper_exports);
var import_decimal = require("decimal.js");
var import_const = require("./const");
function getUuid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx".replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    const v = c === "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
}
var sleep = (time) => new Promise((resove) => {
  setTimeout(() => {
    resove(void 0);
  }, time);
});
function cloneDeep(source, hash = /* @__PURE__ */ new WeakMap()) {
  if (typeof source !== "object" || source === null) {
    return source;
  }
  if (hash.has(source)) {
    return hash.get(source);
  }
  const target = Array.isArray(source) ? [] : {};
  Reflect.ownKeys(source).forEach((key) => {
    const val = source[key];
    if (typeof val === "object" && val != null) {
      target[key] = cloneDeep(val, hash);
    } else {
      target[key] = val;
    }
  });
  return target;
}
var getPayParams = (data, statue) => {
  var _a, _b, _c, _d, _e, _f;
  if (statue === "fail") {
    return {
      number: data.number,
      device: data.device,
      order_id: data.order_id
    };
  }
  const receipt = ((_a = data == null ? void 0 : data.custom) == null ? void 0 : _a.receipt) || [];
  if (data.mode === import_const.ModeEnum.Pay || data.mode === import_const.ModeEnum.FullPay) {
    const pay = data.pay;
    const card_reader_surcharge = ((_b = data.custom) == null ? void 0 : _b.card_reader_surcharge) || 0;
    const obj = pay || {};
    const cardReaderSurcharge = card_reader_surcharge;
    const surCardDeci = new import_decimal.Decimal(Number(cardReaderSurcharge));
    const surMoneyDeci = new import_decimal.Decimal(Number((obj == null ? void 0 : obj.surMoney) || 0));
    const surRateMoneyDeci = new import_decimal.Decimal(Number(obj == null ? void 0 : obj.surRateMoney));
    const surMoneyTotal = surMoneyDeci.add(surCardDeci);
    const surchargeDeci = surMoneyTotal.add(surRateMoneyDeci);
    const amount = new import_decimal.Decimal(Number(obj == null ? void 0 : obj.amount));
    const total = surchargeDeci.add(amount);
    return {
      ...obj,
      cardReaderSurcharge,
      surMoney: surMoneyTotal.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
      surcharge: surchargeDeci.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
      total: total.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
      number: data.number,
      uniquePaymentNumber: data.number,
      device: data.device,
      order_id: data.order_id,
      receipt: ((_d = (_c = data.device) == null ? void 0 : _c.setting) == null ? void 0 : _d.receipts) ? [] : receipt
    };
  }
  if (data.mode === import_const.ModeEnum.Refund) {
    return {
      amount: data.amount,
      total: data.amount,
      number: data.number,
      device: data.device,
      order_id: data.order_id,
      receipt: ((_f = (_e = data.device) == null ? void 0 : _e.setting) == null ? void 0 : _f.receipts) ? [] : receipt,
      uniquePaymentNumber: data.number
    };
  }
  return {
    amount: data.amount,
    total: data.amount,
    number: data.number,
    order_id: data.order_id
  };
};
var limitDecimals = (value) => {
  const 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 "";
};
function getAmonunt(store) {
  const { device, pay, name, symbol, amount, eftpos } = store;
  if (!device) {
    throw Error("device is not defined");
  }
  const setting = device.setting;
  if (pay) {
    return {
      name,
      symbol,
      // format,
      money: Number(amount),
      amount: Number(pay.amount),
      surRate: Number(pay.surRate),
      surMoney: Number(pay.surMoney),
      surcharge: (setting == null ? void 0 : setting.surcharge) || false,
      useOriginSurcharge: (pay == null ? void 0 : pay.useOriginSurcharge) || false
    };
  }
  if (!setting) {
    return {
      name,
      symbol,
      // format,
      money: Number(amount),
      amount: Number(amount),
      surRate: 0,
      surMoney: 0,
      surcharge: false,
      useOriginSurcharge: false
    };
  }
  const useOriginSurcharge = eftpos === import_const.EftposEnum.Tyro ? setting.useOriginSurcharge || false : false;
  const surRate = setting.constant_rate || 0;
  const surMoney = setting.constant_money || 0;
  const surcharge = setting.surcharge;
  return {
    name,
    symbol,
    money: Number(amount),
    amount: Number(amount),
    surRate: useOriginSurcharge ? 0 : surcharge ? surRate : 0,
    surMoney: useOriginSurcharge ? 0 : surcharge ? surMoney : 0,
    surcharge,
    useOriginSurcharge
  };
}
function getPayAmonunt({ money, amount, surRate, surMoney }, formatAmount) {
  const surRateDeci = new import_decimal.Decimal(surRate);
  const moneyDeci = new import_decimal.Decimal(amount);
  const surMoneyDeci = new import_decimal.Decimal(surMoney);
  const surRateMoney = moneyDeci.mul(surRateDeci.div(100)).toFixed(2, import_decimal.Decimal.ROUND_HALF_UP);
  const totalSurMoney = moneyDeci.mul(surRateDeci.div(100)).add(surMoneyDeci);
  const rate = new import_decimal.Decimal(surRate).div(100).toFixed(4, import_decimal.Decimal.ROUND_HALF_UP);
  const total = totalSurMoney.add(moneyDeci).toFixed(2, import_decimal.Decimal.ROUND_HALF_UP);
  return {
    sourceAmount: `${money}`,
    amount: `${amount}`,
    surcharge: `${totalSurMoney}`,
    total: `${total}`,
    surPercentRate: `${rate}`,
    surRate: `${surRate}`,
    surMoney: `${surMoney}`,
    surRateMoney: `${surRateMoney}`,
    totalUnit: formatAmount(total)
  };
}
function getBrowserVersion() {
  const Sys = {};
  const ua = navigator.userAgent.toLowerCase();
  let 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";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  cloneDeep,
  getAmonunt,
  getBrowserVersion,
  getPayAmonunt,
  getPayParams,
  getUuid,
  limitDecimals,
  sleep
});
