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/pay/toC/PaymentMethods/index.tsx
var PaymentMethods_exports = {};
__export(PaymentMethods_exports, {
  default: () => PaymentMethods_default
});
module.exports = __toCommonJS(PaymentMethods_exports);
var import_react = __toESM(require("react"));
var import_MWCreditCard = __toESM(require("./MWCreditCard"));
var import_SuperPayAliPayH5 = __toESM(require("./SuperPayAliPayH5"));
var import_StripePay = __toESM(require("./StripePay"));
var import_OfflinePayment = __toESM(require("./OfflinePayment"));
var import_BankDeposit = __toESM(require("./BankDeposit"));
var import_SuperPayWxPayH5 = __toESM(require("./SuperPayWxPayH5"));
var import_CustomPayment = __toESM(require("./CustomPayment"));
var import_status = require("../PaymentList/status");
var import_CashManual = __toESM(require("./CashManual"));
var import_GooglePay = __toESM(require("./GooglePay"));
var import_payGroup = require("../payGroup");
var import_ApplePay = __toESM(require("./ApplePay"));
var import_GlobePayAliPayH5 = __toESM(require("./GlobePayAliPayH5"));
var import_GlobePayPaypalH5 = __toESM(require("./GlobePayPaypalH5"));
var import_GlobePayWxH5 = __toESM(require("./GlobePayWxH5"));
var import_GlobePayQrcode = __toESM(require("./GlobePayQrcode"));
var import_serve = require("./WalletPass/serve");
var import_utils = require("../utils");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_utils2 = require("@pisell/utils");
var isOfflinePayment = (type) => {
  if ((0, import_status.isCustomPayment)(type)) {
    return true;
  }
  return [
    "pisell.bank_deposit",
    "pisell.offline_payment",
    "pisell.other.cashmanual",
    "pisell.custom_bank_transfer"
  ].includes(type);
};
var PaymentMethods = (props, ref) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const MWCreditCardRef2 = (0, import_react.useRef)(null);
  const SuperPayAliPayH5Ref2 = (0, import_react.useRef)(null);
  const StripePayRef2 = (0, import_react.useRef)(null);
  const OfflinePaymentRef2 = (0, import_react.useRef)(null);
  const BankDepositRef2 = (0, import_react.useRef)(null);
  const SuperPayWxPayH5Ref2 = (0, import_react.useRef)(null);
  const CustomPaymentRef2 = (0, import_react.useRef)(null);
  const ApplePayRef2 = (0, import_react.useRef)(null);
  const GooglePayRef2 = (0, import_react.useRef)(null);
  const CashManualRef2 = (0, import_react.useRef)(null);
  const GlobePayAliPayH5Ref2 = (0, import_react.useRef)(null);
  const GlobePayPaypalH5Ref2 = (0, import_react.useRef)(null);
  const GlobePayWxH5Ref2 = (0, import_react.useRef)(null);
  const GlobePayQrcodeRef2 = (0, import_react.useRef)(null);
  const context = (0, import_useEngineContext.default)();
  const { interaction, token } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const dvaStore = (_e = (_d = (_c = (_b = context.appHelper) == null ? void 0 : _b.utils) == null ? void 0 : _c.getStore) == null ? void 0 : _d.call(_c)) == null ? void 0 : _e.getState();
  const shop = ((_g = (_f = dvaStore == null ? void 0 : dvaStore.core) == null ? void 0 : _f.core) == null ? void 0 : _g.shop) || {};
  const shopName = (shop == null ? void 0 : shop.name) || "";
  const shopPhone = (shop == null ? void 0 : shop.phone) || "";
  const shopEmail = (shop == null ? void 0 : shop.contact_email) || "";
  const init = async ({ type, payGroup }) => {
    var _a2, _b2;
    const waitAmount = (payGroup == null ? void 0 : payGroup.wait_amount) || 0;
    const cards = (_b2 = (_a2 = payGroup == null ? void 0 : payGroup.walletPass) == null ? void 0 : _a2.cards) == null ? void 0 : _b2.length;
    (0, import_utils2.sendWarningLog)({
      title: "支付初始化",
      content: [
        { key: "type", value: type },
        { key: "payGroup", value: JSON.stringify(payGroup) },
        { key: "waitAmount", value: waitAmount },
        { key: "cards", value: cards }
      ]
    });
    if (cards && (waitAmount <= 0 || isOfflinePayment(type))) {
      try {
        await (0, import_serve.walletPassPay)((0, import_utils.formatWalletPassParams)(payGroup));
        await (0, import_payGroup.clearStashPayGroup)(payGroup.order_id);
        (0, import_payGroup.syncPayGroupToStorage)({
          ...payGroup,
          walletPass: {
            ...payGroup.walletPass,
            cards: [],
            paid: true
          }
        });
      } catch (err) {
        (0, import_utils2.sendWarningLog)({
          title: "钱包支付失败 catch",
          content: [
            { key: "错误信息", value: (err == null ? void 0 : err.message) || "未知错误" },
            { key: "错误堆栈", value: (err == null ? void 0 : err.stack) || "无堆栈信息" },
            { key: "错误类型", value: (err == null ? void 0 : err.name) || "未知错误类型" }
          ]
        });
        return props.payCallback({
          key: "fail",
          data: {
            err,
            payGroup,
            fn: "init"
          }
        });
      }
    }
    if (waitAmount <= 0) {
      try {
        (0, import_payGroup.removePayGroupFromStorage)(payGroup.order_id);
        await (0, import_payGroup.clearStashPayGroup)(payGroup.order_id);
        props.payCallback({ key: "success" });
      } catch (err) {
        (0, import_utils2.sendWarningLog)({
          title: "跳转成功页面 catch",
          content: [
            { key: "错误信息", value: (err == null ? void 0 : err.message) || "未知错误" },
            { key: "错误堆栈", value: (err == null ? void 0 : err.stack) || "无堆栈信息" },
            { key: "错误类型", value: (err == null ? void 0 : err.name) || "未知错误类型" }
          ]
        });
      }
      return;
    }
    await onPayByType({ type, payGroup });
  };
  console.log("paymentResult isAppEnv", (_i = (_h = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _h.isAppEnv) == null ? void 0 : _i.call(_h), interaction, context);
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c2, _d2;
    console.log("paymentResult isAppEnv", (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.isAppEnv) == null ? void 0 : _b2.call(_a2));
    if ((_d2 = (_c2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _c2.isAppEnv) == null ? void 0 : _d2.call(_c2)) {
      interaction.utils.mountFunction("global", "paymentResult", (strResult) => {
        console.log("paymentResult", strResult);
        if (!(0, import_utils2.isJson)(strResult)) return;
        const result = JSON.parse(strResult);
        const { type, pay_number, order_id } = result;
        void (async () => {
          var _a3;
          if (type && order_id && pay_number) {
            await (0, import_payGroup.setPayNumber)(order_id, pay_number, (_a3 = token == null ? void 0 : token.get) == null ? void 0 : _a3.call(token));
          }
          props.payCallback({
            key: "success"
          });
        })();
      });
    }
  }, []);
  const onPayByType = async ({ type, payGroup }) => {
    var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i2, _j, _k, _l, _m, _n;
    console.log(type, payGroup);
    if (type === "mw.apple") {
      await ((_a2 = ApplePayRef2.current) == null ? void 0 : _a2.init({ payGroup }));
      return;
    }
    if (type === "mw.google") {
      await ((_b2 = GooglePayRef2.current) == null ? void 0 : _b2.init({ payGroup }));
      return;
    }
    if (type === "mw.credit_card") {
      (_c2 = MWCreditCardRef2.current) == null ? void 0 : _c2.init({ payGroup });
      return;
    }
    if (type === "superpay.alipay.h5") {
      await ((_d2 = SuperPayAliPayH5Ref2.current) == null ? void 0 : _d2.init({ payGroup }));
      return;
    }
    if (type === "superpay.wechat.h5") {
      await ((_e2 = SuperPayWxPayH5Ref2.current) == null ? void 0 : _e2.init({ payGroup }));
      return;
    }
    if (type === "stripe") {
      (_f2 = StripePayRef2.current) == null ? void 0 : _f2.init({ payGroup });
      return;
    }
    if (type === "pisell.offline_payment") {
      await ((_g2 = OfflinePaymentRef2.current) == null ? void 0 : _g2.init({ payGroup }));
      return;
    }
    if (type === "pisell.bank_deposit") {
      await ((_h2 = BankDepositRef2.current) == null ? void 0 : _h2.init({ payGroup }));
      return;
    }
    if (type === "pisell.other.cashmanual") {
      await ((_i2 = CashManualRef2.current) == null ? void 0 : _i2.init({ payGroup }));
      return;
    }
    if (type === "globepay.alipay.h5") {
      await ((_j = GlobePayAliPayH5Ref2.current) == null ? void 0 : _j.init({ payGroup }));
      return;
    }
    if (type === "globepay.paypal.h5") {
      await ((_k = GlobePayPaypalH5Ref2.current) == null ? void 0 : _k.init({ payGroup }));
      return;
    }
    if (type === "globepay.wechat.jsapi") {
      await ((_l = GlobePayWxH5Ref2.current) == null ? void 0 : _l.init({ payGroup }));
      return;
    }
    if (type === "globepay.alipay.qrcode" || type === "globepay.wechat.qrcode") {
      await ((_m = GlobePayQrcodeRef2.current) == null ? void 0 : _m.init({ payGroup }));
      return;
    }
    if ((0, import_status.isCustomPayment)(type)) {
      await ((_n = CustomPaymentRef2.current) == null ? void 0 : _n.init({ payGroup }));
      return;
    }
  };
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      onPay: init
    })
  );
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, /* @__PURE__ */ import_react.default.createElement(import_MWCreditCard.default, { ref: MWCreditCardRef2, shopName, shopPhone, shopEmail }), /* @__PURE__ */ import_react.default.createElement(import_SuperPayAliPayH5.default, { ref: SuperPayAliPayH5Ref2 }), /* @__PURE__ */ import_react.default.createElement(import_StripePay.default, { ref: StripePayRef2 }), /* @__PURE__ */ import_react.default.createElement(import_OfflinePayment.default, { ref: OfflinePaymentRef2 }), /* @__PURE__ */ import_react.default.createElement(import_BankDeposit.default, { ref: BankDepositRef2 }), /* @__PURE__ */ import_react.default.createElement(import_SuperPayWxPayH5.default, { ref: SuperPayWxPayH5Ref2 }), /* @__PURE__ */ import_react.default.createElement(import_CustomPayment.default, { ref: CustomPaymentRef2 }), /* @__PURE__ */ import_react.default.createElement(import_ApplePay.default, { ref: ApplePayRef2 }), /* @__PURE__ */ import_react.default.createElement(import_GooglePay.default, { ref: GooglePayRef2 }), /* @__PURE__ */ import_react.default.createElement(import_CashManual.default, { ref: CashManualRef2 }), /* @__PURE__ */ import_react.default.createElement(import_GlobePayAliPayH5.default, { ref: GlobePayAliPayH5Ref2 }), /* @__PURE__ */ import_react.default.createElement(import_GlobePayWxH5.default, { ref: GlobePayWxH5Ref2 }), /* @__PURE__ */ import_react.default.createElement(import_GlobePayPaypalH5.default, { ref: GlobePayPaypalH5Ref2 }), /* @__PURE__ */ import_react.default.createElement(import_GlobePayQrcode.default, { ref: GlobePayQrcodeRef2 }));
};
var PaymentMethods_default = (0, import_react.forwardRef)(PaymentMethods);
