var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/amount.tsx
var amount_exports = {};
__export(amount_exports, {
  default: () => amount_default
});
module.exports = __toCommonJS(amount_exports);
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_decimal = require("decimal.js");
var import_react = __toESM(require("react"));
var import_alert = __toESM(require("./component/alert"));
var import_titlebar = __toESM(require("./component/header/titlebar"));
var import_manufacturer = require("./manufacturer");
var import_helper = require("./helper");
var import_store = require("./store");
var amount_default = ({
  className = "",
  onChange,
  onClose,
  formatAmount = (number, precision, symbol) => `${symbol}${number}`,
  isMobile,
  net,
  client
}) => {
  const dispatch = (0, import_store.useDispatch)();
  const [status, setStatus] = (0, import_react.useState)(true);
  const store = (0, import_store.useContextData)();
  const {
    name,
    symbol,
    money,
    amount: lastMoney,
    surRate,
    surMoney,
    useOriginSurcharge
  } = (0, import_manufacturer.getAmonunt)(store);
  const [curSurRate, setSurRate] = (0, import_react.useState)(surRate);
  const [curSurMoney, setSurMoney] = (0, import_react.useState)(surMoney);
  const [currentMoney, setCurrentMoney] = (0, import_react.useState)(lastMoney);
  const [tempSurRate, setTempSurRate] = (0, import_react.useState)(curSurRate);
  const [tempSurMoney, setTempSurMoney] = (0, import_react.useState)(curSurMoney);
  const surRateMoney = (0, import_react.useMemo)(() => {
    const surRateDeci = new import_decimal.Decimal(curSurRate);
    const moneyDeci = new import_decimal.Decimal(currentMoney);
    return moneyDeci.mul(surRateDeci.div(100)).toFixed(2, import_decimal.Decimal.ROUND_HALF_UP);
  }, [currentMoney, curSurRate]);
  const totalSurMoney = (0, import_react.useMemo)(() => {
    const surRateDeci = new import_decimal.Decimal(curSurRate);
    const moneyDeci = new import_decimal.Decimal(currentMoney);
    const surMoneyDeci = new import_decimal.Decimal(curSurMoney);
    return moneyDeci.mul(surRateDeci.div(100)).add(surMoneyDeci).toFixed(2, import_decimal.Decimal.ROUND_HALF_UP);
  }, [currentMoney, curSurRate, curSurMoney]);
  const total = (0, import_react.useMemo)(() => {
    const totalSurDeci = new import_decimal.Decimal(totalSurMoney);
    const moneyDeci = new import_decimal.Decimal(currentMoney);
    return totalSurDeci.add(moneyDeci).toFixed(2, import_decimal.Decimal.ROUND_HALF_UP);
  }, [currentMoney, totalSurMoney]);
  const subtracted = (0, import_react.useMemo)(() => {
    const moneyDeci = new import_decimal.Decimal(money);
    const currentMoneyDeci = new import_decimal.Decimal(currentMoney);
    return moneyDeci.sub(currentMoneyDeci).toFixed(2, import_decimal.Decimal.ROUND_HALF_UP);
  }, [currentMoney, money]);
  const onSubmit = () => {
    if (!status) {
      setStatus(true);
      setSurRate(tempSurRate);
      setSurMoney(tempSurMoney);
      return;
    }
    if (!net) {
      import_antd.message.open({
        key: "net-warning",
        type: "warning",
        content: import_utils.locales.getText("page.eftpos-pay.text.not.connected")
      });
      return;
    }
    const rate = new import_decimal.Decimal(curSurRate).div(100).toFixed(4, import_decimal.Decimal.ROUND_HALF_UP);
    const pay = {
      sourceAmount: `${money}`,
      amount: `${currentMoney}`,
      surcharge: `${totalSurMoney}`,
      total: `${total}`,
      surPercentRate: `${rate}`,
      surRate: `${curSurRate}`,
      surMoney: `${curSurMoney}`,
      surRateMoney: `${surRateMoney}`,
      totalUnit: formatAmount(total, 2, symbol)
    };
    console.log("amount pay = ", pay);
    dispatch((0, import_store.updateState)({ pay }));
    dispatch((0, import_store.updateComponent)(""));
    onChange && onChange("page", "pay");
  };
  const onCancel = () => {
    if (!status) {
      setStatus(true);
      return;
    }
    onClose();
  };
  const onEdit = () => {
    setStatus(false);
    setTempSurRate(curSurRate);
    setTempSurMoney(curSurMoney);
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `pisell-lowcode__eft-pay-amount ${className}` }, isMobile ? null : /* @__PURE__ */ import_react.default.createElement(import_titlebar.default, { type: "left-title-close", onClose }, status ? name : import_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.title")), status ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-body" }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-lowcode__eft-pay-cash",
      "data-money": formatAmount(money, 2, symbol)
    },
    import_utils.locales.getText("page.eftpos-pay.text.amount.to.pay")
  ), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money-title" }, import_utils.locales.getText("page.eftpos-pay.text.input.title")(name)), /* @__PURE__ */ import_react.default.createElement(
    import_antd.InputNumber,
    {
      type: "number",
      addonBefore: symbol || "$",
      max: money,
      precision: 2,
      min: 0,
      style: { width: "100%" },
      value: currentMoney,
      formatter: import_helper.limitDecimals,
      parser: import_helper.limitDecimals,
      onChange: (event) => {
        if (event !== null && event !== void 0) {
          setCurrentMoney(event || 0);
        } else {
          setCurrentMoney(0);
        }
      }
    }
  ), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money-tip" }, import_utils.locales.getText(
    "page.eftpos-pay.text.amount.cash.left.pay.paid"
  )(formatAmount(`${subtracted}`, 2, symbol)))), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-surcharge" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-details" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-total" }, import_utils.locales.getText(
    "page.eftpos-pay.text.surcharge.edit.actual.pay"
  ), ":", formatAmount(`${total}`, 2, symbol)), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money" }, import_utils.locales.getText(
    "page.eftpos-pay.text.surcharge.edit.inc.surcharge"
  ), ": ", formatAmount(`${totalSurMoney}`, 2, symbol), " (", import_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.rate"), ":", curSurRate, "% + ", formatAmount(`${curSurMoney}`, 2, symbol), ")")), /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "link", onClick: onEdit, disabled: useOriginSurcharge }, import_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.surcharge")))) : /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-body" }, /* @__PURE__ */ import_react.default.createElement(import_alert.default, null, import_utils.locales.getText("page.eftpos-pay.text.surcharge.percentage-fixed")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-form" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item-title" }, import_utils.locales.getText("page.eftpos-pay.text.surcharge.percentage")), /* @__PURE__ */ import_react.default.createElement(
    import_antd.InputNumber,
    {
      type: "number",
      value: tempSurRate,
      min: 0,
      max: 100,
      precision: 2,
      addonAfter: "%",
      style: { width: "100%" },
      onChange: (e) => {
        setTempSurRate(e || 0);
      }
    }
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item-title" }, import_utils.locales.getText("page.eftpos-pay.text.surcharge.fixed")), /* @__PURE__ */ import_react.default.createElement(
    import_antd.InputNumber,
    {
      type: "number",
      value: tempSurMoney,
      min: 0,
      max: 1e5,
      precision: 2,
      style: { width: "100%" },
      addonBefore: symbol || "$",
      onChange: (e) => {
        setTempSurMoney(e || 0);
      }
    }
  )))), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      className: "pisell-lowcode__eft-pay-step-item-btn",
      onClick: onCancel
    },
    import_utils.locales.getText("page.eftpos-pay.text.cancel")
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      className: "pisell-lowcode__eft-pay-step-item-btn",
      type: "primary",
      onClick: onSubmit,
      disabled: currentMoney <= 0 && status
    },
    import_utils.locales.getText("page.eftpos-pay.text.sure")
  )));
};
