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/MWCreditCard/TDSPay/index.tsx
var TDSPay_exports = {};
__export(TDSPay_exports, {
  default: () => TDSPay_default
});
module.exports = __toCommonJS(TDSPay_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_ThreedsLoading = __toESM(require("./ThreedsLoading"));
var import_tds2 = require("../tds2");
var import_serve = require("../List/serve");
var import_model = require("../../../model");
var import_payGroup = require("../../../payGroup");
var import_index = require("./index.less");
var import_utils = require("@pisell/utils");
var TDSPay = (props, ref) => {
  const { onChange } = props;
  const [open, setOpen] = (0, import_react.useState)(false);
  const { state } = (0, import_react.useContext)(import_model.Context);
  const init = async (res, order) => {
    setOpen(true);
    if (res.id) {
      _fastPayNew(res);
    } else {
      newCardPay(res);
    }
  };
  const payCallback = (res) => {
    setOpen(false);
    if (res.key === "hideModal") {
      return;
    }
    onChange == null ? void 0 : onChange(res);
  };
  const _fastPayNew = async (res) => {
    var _a, _b, _c;
    try {
      const params = (0, import_payGroup.getPayGroupParamsFromStorage)(
        (_a = state.payGroup) == null ? void 0 : _a.order_id,
        {
          card_id: res.id
        }
      );
      if (!params) {
        payCallback == null ? void 0 : payCallback({
          key: "fail",
          data: {
            res,
            fn: "_fastPayNew1",
            message: import_utils.locales.getText("tocPay.text.getParamsFail")
          }
        });
        return;
      }
      const { data } = await (0, import_serve.fastPayNew)({
        order_id: (_b = state.payGroup) == null ? void 0 : _b.order_id,
        params
      });
      (0, import_payGroup.setPayNumber)((_c = state.payGroup) == null ? void 0 : _c.order_id, data == null ? void 0 : data.pay_number);
      if ((data == null ? void 0 : data.type) === "token") {
        payCallback == null ? void 0 : payCallback({ key: "success", data });
      }
      if ((data == null ? void 0 : data.type) === "3ds") {
        (0, import_tds2.startPayment)({ payData: data }, payCallback);
      }
    } catch (e) {
      payCallback == null ? void 0 : payCallback({
        key: "fail",
        data: {
          res,
          fn: "_fastPayNew2",
          message: import_utils.locales.getText("tocPay.text.getParamsFail")
        }
      });
    }
  };
  const newCardPay = (res) => {
    var _a, _b;
    let _cardInfo = {
      card_name: res.card_name,
      card_number: res.card_number,
      card_expiry_month: res.date.format("MM"),
      card_expiry_year: res.date.format("YY"),
      card_ccv: res.card_ccv,
      is_save: res.is_save ? 1 : 0
    };
    _cardInfo = (0, import_payGroup.getPayGroupParamsFromStorage)(
      (_a = state.payGroup) == null ? void 0 : _a.order_id,
      _cardInfo
    );
    if (!_cardInfo) {
      payCallback == null ? void 0 : payCallback({
        key: "fail",
        data: {
          _cardInfo,
          message: import_utils.locales.getText("tocPay.text.getParamsFail"),
          fn: "newCardPay"
        }
      });
      return;
    }
    (0, import_tds2.startPayment)(
      { orderId: (_b = state.payGroup) == null ? void 0 : _b.order_id, cardInfo: _cardInfo },
      payCallback
    );
  };
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      init
    })
  );
  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: "" }, /* @__PURE__ */ import_react.default.createElement(import_ThreedsLoading.default, null), /* @__PURE__ */ import_react.default.createElement("div", { id: "threeds-container" }))
  );
};
var TDSPay_default = (0, import_react.forwardRef)(TDSPay);
