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/SuperPayAliPayH5/index.tsx
var SuperPayAliPayH5_exports = {};
__export(SuperPayAliPayH5_exports, {
  default: () => SuperPayAliPayH5_default
});
module.exports = __toCommonJS(SuperPayAliPayH5_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_ThreedsLoading = __toESM(require("../MWCreditCard/TDSPay/ThreedsLoading"));
var import_serve = require("./serve");
var import_model = require("../../model");
var import_payGroup = require("../../payGroup");
var import_index = require("./index.less");
var import_useEngineContext = __toESM(require("../../../../../hooks/useEngineContext"));
var import_usePaymentLogger = __toESM(require("../../../../../hooks/usePaymentLogger"));
var import_utils2 = require("../../utils");
var import_hooks = require("../hooks");
var import_PaymentReturnContent = __toESM(require("../components/PaymentReturnContent"));
var SuperPayAliPayH5 = (props, ref) => {
  var _a, _b, _c, _d;
  const { state } = (0, import_react.useContext)(import_model.Context);
  const { onChange } = props;
  const [open, setOpen] = (0, import_react.useState)(false);
  const [showSuccessBackButton, setShowSuccessBackButton] = (0, import_react.useState)(false);
  const [statePayNumber, setStatePayNumber] = (0, import_react.useState)("");
  const context = (0, import_useEngineContext.default)();
  const { interaction, token } = (_a = context.appHelper) == null ? void 0 : _a.utils;
  const paymentLogger = (0, import_usePaymentLogger.default)({
    orderId: ((_b = state.payGroup) == null ? void 0 : _b.order_id) || "unknown",
    step: "2"
  });
  const { isReturned, handleRedirect, resetState } = (0, import_hooks.usePaymentRedirect)({
    orderId: (_c = state.payGroup) == null ? void 0 : _c.order_id,
    paymentType: "aliPay"
  });
  const init = async ({ payGroup }) => {
    var _a2, _b2, _c2;
    paymentLogger.addLog({
      key: "SuperPayAliPayH5初始化开始",
      value: ""
    });
    const values = (0, import_payGroup.getPayGroupParamsFromStorage)(payGroup.order_id);
    if ((_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.isAppEnv) == null ? void 0 : _b2.call(_a2)) {
      paymentLogger.addLog({
        key: "SuperPayAliPayH5App环境检测",
        value: "检测到App环境，调用客户端支付"
      });
      paymentLogger.sendLogs("SuperPayAliPayH5App环境支付");
      return (0, import_utils2.clientPayment)({
        order_id: payGroup.order_id,
        type: "aliPay",
        values,
        interaction
      });
    }
    setOpen(true);
    if (!values) {
      paymentLogger.addLog({
        key: "SuperPayAliPayH5获取支付参数失败",
        value: {
          title: `订单${payGroup == null ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
          data: {
            payGroup
          }
        }
      });
      paymentLogger.sendLogs("SuperPayAliPayH5获取支付参数失败");
      setOpen(false);
      return;
    }
    paymentLogger.addLog({
      key: "SuperPayAliPayH5获取支付参数成功",
      value: ""
    });
    try {
      const data = await (0, import_serve.getAliPayUrl)(payGroup.order_id, values);
      if (data == null ? void 0 : data.pay_url) {
        paymentLogger.addLog({
          key: "SuperPayAliPayH5获取支付链接成功",
          value: ""
        });
        await (0, import_payGroup.setPayNumber)(payGroup == null ? void 0 : payGroup.order_id, data == null ? void 0 : data.pay_number, (_c2 = token == null ? void 0 : token.get) == null ? void 0 : _c2.call(token));
        setStatePayNumber(data == null ? void 0 : data.pay_number);
        setShowSuccessBackButton(true);
        paymentLogger.addLog({
          key: "SuperPayAliPayH5准备跳转支付宝",
          value: ""
        });
        paymentLogger.sendLogs("SuperPayAliPayH5跳转成功");
        await handleRedirect(data == null ? void 0 : data.pay_url);
      } else {
        paymentLogger.addLog({
          key: "SuperPayAliPayH5支付链接为空",
          value: {
            title: `订单${payGroup == null ? void 0 : payGroup.order_id} 支付宝支付链接为空`,
            data: {
              response_data: data
            }
          }
        });
        paymentLogger.sendLogs("SuperPayAliPayH5支付链接为空");
        setOpen(false);
      }
    } catch (err) {
      paymentLogger.addLog({
        key: "SuperPayAliPayH5处理错误",
        value: {
          title: `订单${payGroup == null ? void 0 : payGroup.order_id} SuperPayAliPayH5处理失败: ${(err == null ? void 0 : err.message) || "未知错误"}`,
          data: {
            payGroup,
            values
          }
        }
      });
      paymentLogger.sendLogs("SuperPayAliPayH5处理失败", err);
      setOpen(false);
    }
  };
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      init
    })
  );
  const handleSuccessBack = () => {
    var _a2;
    (_a2 = state.payCallback) == null ? void 0 : _a2.call(state, {
      key: "success"
    });
  };
  const handleReturn = () => {
    setOpen(false);
    resetState();
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      platform: "h5",
      open,
      footer: false,
      header: false,
      closable: false,
      bodyStyle: { padding: 0 }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, !isReturned && /* @__PURE__ */ import_react.default.createElement(
      import_ThreedsLoading.default,
      {
        title: import_utils.locales.getText("tocPay.text.redirectingToAlipay"),
        onSuccessBack: handleSuccessBack,
        showSuccessBackButton
      }
    ), isReturned && /* @__PURE__ */ import_react.default.createElement(
      import_PaymentReturnContent.default,
      {
        pay_number: statePayNumber,
        handleReturn,
        state,
        orderId: (_d = state.payGroup) == null ? void 0 : _d.order_id,
        unique_payment_number: state.front_order_uuid
      }
    ))
  );
};
var SuperPayAliPayH5_default = (0, import_react.forwardRef)(SuperPayAliPayH5);
