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/SavePayLaterHandler/index.tsx
var SavePayLaterHandler_exports = {};
__export(SavePayLaterHandler_exports, {
  default: () => SavePayLaterHandler_default
});
module.exports = __toCommonJS(SavePayLaterHandler_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_decimal = __toESM(require("decimal.js"));
var import_locales = __toESM(require("../../locales"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var SavePayLaterHandler = (0, import_react.forwardRef)(({
  amountSymbol = "$",
  walletPassData,
  onSavePayLater
}, ref) => {
  const [showWalletBalanceConfirmModal, setShowWalletBalanceConfirmModal] = (0, import_react.useState)(false);
  const [walletBalanceDeductAmount, setWalletBalanceDeductAmount] = (0, import_react.useState)("");
  const context = (0, import_useEngineContext.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;
  };
  const getWalletTotalAmount = () => {
    if (!(walletPassData == null ? void 0 : walletPassData.selectedWallet) || !Array.isArray(walletPassData.selectedWallet)) {
      return 0;
    }
    const totalDecimal = walletPassData.selectedWallet.reduce((totalDecimal2, wallet) => {
      const redeemValue = parseFloat(wallet.redeem) || 0;
      const redeemDecimal = new import_decimal.default(redeemValue);
      return totalDecimal2.plus(redeemDecimal);
    }, new import_decimal.default(0));
    return totalDecimal.toNumber();
  };
  const handleSavePayLaterClick = () => {
    if ((walletPassData == null ? void 0 : walletPassData.selectedWallet) && walletPassData.selectedWallet.length > 0) {
      const amount = getWalletTotalAmount();
      const deductAmount = (0, import_utils.formatAmount)(amount, 2, amountSymbol);
      setWalletBalanceDeductAmount(deductAmount);
      setShowWalletBalanceConfirmModal(true);
    } else {
      onSavePayLater();
    }
  };
  const handleWalletBalanceConfirm = (action) => {
    setShowWalletBalanceConfirmModal(false);
    onSavePayLater(action);
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    handleSavePayLaterClick
  }));
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      visible: showWalletBalanceConfirmModal,
      onCancel: () => handleWalletBalanceConfirm("cancel"),
      closable: false,
      mask: true,
      maskClosable: false,
      centered: true,
      width: 530,
      title: getText("pisell2.checkout.wallet-balance-confirm.title"),
      footer: null,
      className: "wallet-balance-confirm-modal",
      styles: {
        header: {
          textAlign: "center",
          borderBottom: "1px solid #EAECF0",
          paddingBottom: "16px"
        }
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { style: { padding: "40px 0 0 0" } }, /* @__PURE__ */ import_react.default.createElement("div", { style: { textAlign: "center", marginBottom: "32px", lineHeight: "1.5" } }, /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#533795", fontWeight: 600, fontSize: "18px" } }, walletBalanceDeductAmount), /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#101828", fontSize: "16px" } }, getText("pisell2.checkout.wallet-balance-confirm.description-before")), /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#533795", fontWeight: 600, fontSize: "16px" } }, getText("pisell2.checkout.wallet-balance-confirm.description-after"))), /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", gap: "12px", flexDirection: "row" } }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        onClick: () => handleWalletBalanceConfirm("cancel"),
        style: {
          flex: "0 1 auto",
          minWidth: "fit-content",
          height: "48px",
          borderRadius: "12px",
          fontSize: "16px",
          fontWeight: 500,
          border: "1px solid #D0D5DD",
          color: "#344054",
          padding: "0 16px",
          whiteSpace: "nowrap"
        }
      },
      getText("pisell2.checkout.wallet-balance-confirm.cancel")
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        onClick: () => handleWalletBalanceConfirm("save-without-deduct"),
        style: {
          flex: "1 1 auto",
          height: "48px",
          borderRadius: "12px",
          fontSize: "16px",
          fontWeight: 500,
          border: "1px solid #D0D5DD",
          color: "#344054",
          padding: "0 16px",
          whiteSpace: "nowrap"
        }
      },
      getText("pisell2.checkout.wallet-balance-confirm.save-without-deduct")
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        type: "primary",
        style: {
          flex: "1.2 1 auto",
          height: "48px",
          backgroundColor: "#533795",
          borderColor: "#533795",
          borderRadius: "12px",
          fontWeight: 600,
          fontSize: "16px",
          border: "none",
          boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.05)",
          padding: "0 16px",
          whiteSpace: "nowrap"
        },
        onClick: () => handleWalletBalanceConfirm("apply-deduct-now")
      },
      getText("pisell2.checkout.wallet-balance-confirm.apply-deduct-now")
    )))
  ));
});
SavePayLaterHandler.displayName = "SavePayLaterHandler";
var SavePayLaterHandler_default = SavePayLaterHandler;
