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/PaymentList/index.tsx
var PaymentList_exports = {};
__export(PaymentList_exports, {
  default: () => PaymentList_default
});
module.exports = __toCommonJS(PaymentList_exports);
var import_react = __toESM(require("react"));
var import_serve = require("./serve");
var import_materials = require("@pisell/materials");
var import_utils = require("./utils");
var import_classnames = __toESM(require("classnames"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_index = require("./index.less");
var import_utils2 = require("@pisell/utils");
var offline = [
  "pisell.offline_payment",
  "pisell.other.cashmanual",
  "pisell.custom_payment"
];
var PaymentList = (props, ref) => {
  var _a, _b, _c;
  const [paymentList, setPaymentList] = (0, import_react.useState)([]);
  const [paymentType, setPaymentType] = (0, import_react.useState)("");
  const [loading, setLoading] = (0, import_react.useState)(true);
  const context = (0, import_useEngineContext.default)();
  const { interaction } = (_a = context.appHelper) == null ? void 0 : _a.utils;
  const _paymentList = (0, import_react.useMemo)(() => {
    var _a2;
    return ((_a2 = props == null ? void 0 : props.store) == null ? void 0 : _a2.paymentList) || paymentList;
  }, [paymentList, (_b = props == null ? void 0 : props.store) == null ? void 0 : _b.paymentList]);
  const _paymentType = (0, import_react.useMemo)(() => {
    var _a2;
    return ((_a2 = props == null ? void 0 : props.store) == null ? void 0 : _a2.paymentType) || paymentType;
  }, [paymentType, (_c = props == null ? void 0 : props.store) == null ? void 0 : _c.paymentType]);
  const _getPaymentList = async (initProps) => {
    var _a2;
    setLoading(true);
    const params = {
      order_id: initProps == null ? void 0 : initProps.order.order_id,
      total_amount: initProps == null ? void 0 : initProps.order.total_amount,
      sale_channel: (initProps == null ? void 0 : initProps.order.sale_channel) || "online-store"
    };
    const res = await (0, import_serve.getPaymentList)(params);
    const paymentList2 = res.paymentList;
    let first = paymentList2[0];
    (_a2 = props.paymentTypeChange) == null ? void 0 : _a2.call(props, first);
    setPaymentList(paymentList2);
    setPaymentType(first.service);
    isAutoPayment(paymentList2);
    setLoading(false);
    return res;
  };
  const _calculateSurcharge = (amount) => {
    if (!paymentList.length) {
      return;
    }
    let list = (0, import_utils.calculateSurcharge)(paymentList, amount);
    setPaymentList(list);
    return list;
  };
  const isApp = (0, import_react.useMemo)(() => {
    var _a2, _b2;
    return (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.isAppEnv) == null ? void 0 : _b2.call(_a2);
  }, []);
  const init = (list) => {
    var _a2, _b2;
    setLoading(true);
    const platform = ((_b2 = (_a2 = context.appHelper) == null ? void 0 : _a2.constants) == null ? void 0 : _b2.platform) || "h5";
    const { paymentList: paymentList2, walletPass } = (0, import_serve.filterPaymentList)(list, platform, isApp);
    if (paymentList2.length > 0) {
      let first = paymentList2[0];
      setPaymentList(paymentList2);
      paymentTypeChange(first.service, first);
      isAutoPayment(paymentList2, walletPass);
    }
    setLoading(false);
    return paymentList2;
  };
  const isAutoPayment = (list, walletPass) => {
    const _list = list;
    if (walletPass) {
      return;
    }
    if (_list.length === 1) {
      let isOfflinePayment = _list.find(
        (d) => offline.find((t) => d.service.includes(t))
      );
      if (isOfflinePayment) {
        setTimeout(() => {
          var _a2;
          (_a2 = props.handlePayment) == null ? void 0 : _a2.call(props, isOfflinePayment);
        }, 200);
        return true;
      }
    }
    return false;
  };
  const paymentTypeChange = (key, item) => {
    var _a2;
    setPaymentType(key);
    (_a2 = props.paymentTypeChange) == null ? void 0 : _a2.call(props, item);
  };
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      init,
      calculateSurcharge: _calculateSurcharge
    })
  );
  const renderToast = (_item) => {
    var _a2, _b2, _c2;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "surcharge-modal-wrap" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "surcharge-modal-title" }, _item.title + " " + (((_a2 = _item == null ? void 0 : _item.params) == null ? void 0 : _a2.description) ? import_utils2.locales.getText("tocPay.text.paymentInstructions") : import_utils2.locales.getText("tocPay.text.serviceCharge"))), /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "surcharge-info",
        dangerouslySetInnerHTML: {
          __html: ((_b2 = _item == null ? void 0 : _item.params) == null ? void 0 : _b2.description) ? (_c2 = _item == null ? void 0 : _item.params) == null ? void 0 : _c2.description : _item.surcharge_fee
        }
      }
    ));
  };
  const content = (0, import_react.useMemo)(() => {
    if (loading) {
      return null;
    }
    if (!_paymentList.length) {
      return /* @__PURE__ */ import_react.default.createElement(
        import_materials.PisellEmpty,
        {
          description: import_utils2.locales.getText("tocPay.text.noAvailablePaymentMethods")
        }
      );
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellCheckboxGroup,
      {
        options: _paymentList,
        value: _paymentType,
        iconPosition: "right",
        mode: "single",
        rowKey: "service",
        className: "payment-list",
        gap: 12,
        onChange: paymentTypeChange,
        optionClassName: "payment-item",
        renderOption: (d, active) => {
          var _a2, _b2;
          const showIcon = d.surcharge && d.service !== "pisell.other.cashmanual" && d.service !== "pisell.custom_payment" || ((_a2 = d == null ? void 0 : d.params) == null ? void 0 : _a2.description) && (d.service === "pisell.other.cashmanual" || d.service === "pisell.custom_payment");
          return /* @__PURE__ */ import_react.default.createElement(
            import_materials.PisellRow,
            {
              platform: "h5",
              key: d.service,
              gap: 12,
              centerContentFill: true,
              leftContent: /* @__PURE__ */ import_react.default.createElement("img", { className: "payment-type-image", src: d.url }),
              centerContent: /* @__PURE__ */ import_react.default.createElement(
                "div",
                {
                  className: (0, import_classnames.default)("payment-name", {
                    "text-active-color": active
                  })
                },
                /* @__PURE__ */ import_react.default.createElement(
                  "div",
                  {
                    className: (0, import_classnames.default)("payment-name-text", {
                      "text-active-color": active
                    })
                  },
                  d.title,
                  " "
                ),
                /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, (((_b2 = d == null ? void 0 : d.params) == null ? void 0 : _b2.description) || d.surcharge) && d.service !== "pisell.offline_payment" ? /* @__PURE__ */ import_react.default.createElement(
                  import_materials.PisellTooltip,
                  {
                    title: renderToast(d),
                    onClick: (e) => {
                      e.stopPropagation();
                    }
                  },
                  /* @__PURE__ */ import_react.default.createElement(
                    "span",
                    {
                      className: (0, import_classnames.default)("surcharge-wrap", {
                        "text-active-color": active
                      })
                    },
                    d.surcharge ? /* @__PURE__ */ import_react.default.createElement(
                      "span",
                      {
                        className: (0, import_classnames.default)("surcharge", {
                          "text-active-color": active
                        })
                      },
                      d.surcharge,
                      " "
                    ) : null,
                    showIcon ? /* @__PURE__ */ import_react.default.createElement(
                      import_materials.Iconfont,
                      {
                        type: "pisell2-help-circle",
                        className: (0, import_classnames.default)("icon", {
                          "text-active-color": active
                        })
                      }
                    ) : null
                  )
                ) : null, d.payment_method && d.service === "pisell.offline_payment" ? /* @__PURE__ */ import_react.default.createElement(
                  import_materials.PisellTooltip,
                  {
                    title: renderToast(d),
                    onClick: (e) => {
                      e.stopPropagation();
                    }
                  },
                  /* @__PURE__ */ import_react.default.createElement("div", { className: "surcharge-wrap" }, /* @__PURE__ */ import_react.default.createElement(
                    import_materials.Iconfont,
                    {
                      type: "pisell2-help-circle",
                      className: (0, import_classnames.default)("icon", {
                        "text-active-color": active
                      })
                    }
                  ))
                ) : null)
              )
            }
          );
        }
      }
    );
  }, [_paymentList, _paymentType, loading]);
  return props.hide ? null : /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "payment-header" }, props.renderTitle ? props.renderTitle() : import_utils2.locales.getText("tocPay.text.paymentMethod")), content);
};
var PaymentList_default = (0, import_react.forwardRef)(PaymentList);
