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/checkout/components/CashPaymentModule/index.tsx
var CashPaymentModule_exports = {};
__export(CashPaymentModule_exports, {
  CashPaymentModule: () => CashPaymentModule,
  default: () => CashPaymentModule_default
});
module.exports = __toCommonJS(CashPaymentModule_exports);
var import_react = __toESM(require("react"));
var import_index = require("./index.less");
var import_classnames = __toESM(require("classnames"));
var import_icon = require("@pisell/icon");
var import_pisellPriceKeyboardPopover = __toESM(require("../../../../pro/pisellPriceKeyboardPopover"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_utils = require("@pisell/utils");
var import_locales = __toESM(require("../../locales"));
var import_useToast = __toESM(require("../../../../hooks/useToast"));
function CashPaymentModule({
  amountSymbol,
  orderInfo,
  onPaymentComplete,
  customAmount,
  amountButtons = [],
  // 按钮列表
  disabled = false,
  // 禁用状态
  roundingFunction,
  balanceDueAmount = "0"
  // 剩余支付金额
}) {
  const [amount, setAmount] = (0, import_react.useState)(customAmount || "");
  const [roundingAmount, setRoundingAmount] = (0, import_react.useState)(0);
  const [amountList, setAmountList] = (0, import_react.useState)(amountButtons.length ? amountButtons : new Array(6).fill(0));
  (0, import_react.useEffect)(() => {
    setAmountList(amountButtons.length ? amountButtons : new Array(6).fill(0));
  }, [amountButtons]);
  const context = (0, import_useEngineContext.default)();
  const Toast = (0, import_useToast.default)();
  const getText = (key, fallback) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const currentLang = ((_e = (_d = (_c = (_b = (_a = context.appHelper.utils) == null ? void 0 : _a.getApp) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.locales) == null ? void 0 : _d.getLocale) == null ? void 0 : _e.call(_d)) || ((_g = (_f = context.appHelper.utils) == null ? void 0 : _f.storage) == null ? void 0 : _g.get("umi_locale")) || "en";
    const checkoutText = (_h = import_locales.default[currentLang]) == null ? void 0 : _h[key];
    if (checkoutText) {
      return checkoutText;
    }
    return import_utils.locales.getText(key) || fallback;
  };
  (0, import_react.useEffect)(() => {
    handleKeyboardConfirm(customAmount || "");
  }, [customAmount, balanceDueAmount]);
  const isAmountButtonsDisabled = disabled || Number(customAmount || 0) === 0;
  const isInputDisabled = disabled || Number(customAmount || 0) === 0;
  const handleAmountClick = (clickedAmount) => {
    if (isAmountButtonsDisabled) return;
    const amountStr = clickedAmount.toString();
    setAmount(amountStr);
    setRoundingAmount(0);
  };
  const handleCashPay = () => {
    if (isAmountButtonsDisabled) return;
    if (onPaymentComplete) {
      if (Number(amount) === 0 && Math.abs(roundingAmount) === 0) {
        Toast.fail(getText("pisell2.amount-summary.amount-cannot-be-zero", "The amount cannot be 0"));
        return;
      }
      const finalAmount = typeof amount === "number" ? amount : Number(amount) || 0;
      onPaymentComplete({
        type: "CASHMANUAL",
        amount: finalAmount,
        totalAmount: Number(customAmount) || 0,
        change: finalAmount - (Number(customAmount) || 0),
        roundingAmount
      });
    }
  };
  const handleKeyboardConfirm = async (value) => {
    if (isInputDisabled) {
      setAmount("0.00");
      setRoundingAmount(0);
      return;
    }
    ;
    const finalValue = String(value);
    console.log("键盘确认:", finalValue);
    if (Number(finalValue) === 0) {
      Toast.fail(getText("pisell2.amount-summary.amount-cannot-be-zero", "The amount cannot be 0"));
      return;
    }
    if (finalValue === "" || finalValue === null || finalValue === void 0) {
      setAmount("");
    } else {
      const cleanValue = finalValue.replace(/[$\s]/g, "");
      const numValue = parseFloat(cleanValue);
      const balanceDue = parseFloat(balanceDueAmount);
      const shouldApplyRounding = Math.abs(numValue - balanceDue) < 0.01;
      let finalAmount = numValue;
      let roundingDiff = 0;
      if (shouldApplyRounding && roundingFunction) {
        const result = await roundingFunction(numValue);
        const { roundedAmount = numValue, roundingDifference = 0 } = result || {};
        finalAmount = roundedAmount;
        roundingDiff = roundingDifference;
        console.log("应用抹零：", numValue, "->", roundedAmount, "差额:", roundingDifference);
      } else {
        console.log("不应用抹零，用户输入金额:", numValue, "剩余支付金额:", balanceDue);
      }
      setRoundingAmount(roundingDiff);
      if (isNaN(numValue)) {
        setAmount("");
      } else {
        setAmount(finalAmount.toString());
        console.log("设置金额为:", finalAmount);
      }
    }
  };
  const handleKeyboardCancel = () => {
    console.log("键盘取消");
  };
  const handleKeyboardChange = (value) => {
    if (isInputDisabled) return;
    const stringValue = String(value);
    console.log("键盘输入变化:", stringValue, "类型:", typeof stringValue);
    setAmount(stringValue);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("cash-pay-container", {
        "cash-pay-container--disabled": disabled
      })
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("cash-pay-header") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("cash-pay-header-title") }, getText("cash") || "Cash"), Math.abs(roundingAmount) > 0 && /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("cash-pay-header-rounding") }, getText("rounding") || "Rounding", ":", " ", (0, import_utils.formatAmount)(roundingAmount, 2, amountSymbol))),
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)("cash-pay-main-content", {
          "cash-pay-main-content--disabled": disabled
        })
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("cash-pay-payment-area") }, /* @__PURE__ */ import_react.default.createElement(
        import_pisellPriceKeyboardPopover.default,
        {
          value: amount || "0",
          onConfirm: handleKeyboardConfirm,
          onCancel: handleKeyboardCancel,
          showCurrencySymbol: true,
          useThousandsSeparator: true,
          selectType: "dark",
          overlayClassName: "cash-payment-keyboard-popover",
          disabled: isInputDisabled
        },
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: (0, import_classnames.default)("cash-pay-amount-input-area", {
              "cash-pay-amount-input-area--disabled": isInputDisabled
            })
          },
          /* @__PURE__ */ import_react.default.createElement(
            import_icon.Edit05,
            {
              className: (0, import_classnames.default)("cash-pay-amount-edit-icon", {
                "cash-pay-amount-edit-icon--disabled": isInputDisabled
              })
            }
          ),
          /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              className: (0, import_classnames.default)("cash-pay-amount-input-wrapper", {
                "cash-pay-amount-input-wrapper--disabled": isInputDisabled
              })
            },
            /* @__PURE__ */ import_react.default.createElement(
              "span",
              {
                className: (0, import_classnames.default)("cash-pay-currency-symbol", {
                  "cash-pay-currency-symbol--disabled": isInputDisabled
                })
              },
              amountSymbol
            ),
            /* @__PURE__ */ import_react.default.createElement(
              "div",
              {
                className: (0, import_classnames.default)("cash-pay-amount-display", {
                  "cash-pay-amount-display--disabled": isInputDisabled
                })
              },
              amount || "0.00"
            )
          )
        )
      ), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classnames.default)("cash-pay-button", {
            "cash-pay-button--disabled": isAmountButtonsDisabled
          }),
          ...isAmountButtonsDisabled ? {} : { onClick: handleCashPay },
          style: { cursor: isAmountButtonsDisabled ? "not-allowed" : "pointer" }
        },
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: (0, import_classnames.default)("cash-pay-button-text", {
              "cash-pay-button-text--disabled": isAmountButtonsDisabled
            })
          },
          getText("cash.payment") || "Cash Pay"
        )
      )),
      /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("cash-pay-amount-button-grid", {
        "cash-pay-amount-button-grid--disabled": isAmountButtonsDisabled
      }) }, amountList.slice(0, 6).map((amount2, index) => /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key: index,
          className: (0, import_classnames.default)("cash-pay-amount-button", {
            "cash-pay-amount-button--disabled": isAmountButtonsDisabled
          }),
          ...isAmountButtonsDisabled ? {} : { onClick: () => handleAmountClick(amount2) },
          style: { cursor: isAmountButtonsDisabled ? "not-allowed" : "pointer" }
        },
        /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: (0, import_classnames.default)("cash-pay-amount-button-text", {
              "cash-pay-amount-button-text--disabled": isAmountButtonsDisabled
            })
          },
          (0, import_utils.formatAmount)(amount2, 2, amountSymbol)
        )
      )))
    )
  );
}
var CashPaymentModule_default = CashPaymentModule;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CashPaymentModule
});
