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/PaymentPlugin.tsx
var PaymentPlugin_exports = {};
__export(PaymentPlugin_exports, {
  default: () => PaymentPlugin_default
});
module.exports = __toCommonJS(PaymentPlugin_exports);
var import_react = require("react");
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_useModal = __toESM(require("../../hooks/useModal"));
var import_content = __toESM(require("./content"));
var import_utils = require("@pisell/utils");
var import_locales = __toESM(require("./locales"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_react2 = __toESM(require("react"));
var modalStyle = document.createElement("style");
modalStyle.textContent = `
  :where(.payment-modal-wrapper) .ant-modal .ant-modal-content,
  .payment-modal-wrapper .ant-modal .ant-modal-content,
  .payment-modal-wrapper .ant-modal-content {
    padding: 12px 36px 36px 36px !important;
  }
`;
if (!document.querySelector(".payment-modal-style")) {
  modalStyle.className = "payment-modal-style";
  document.head.appendChild(modalStyle);
}
var PaymentPlugin = () => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  const { pisellos } = (_a = context.appHelper) == null ? void 0 : _a.utils;
  const paymentRef = (0, import_react.useRef)();
  const callback = (0, import_react.useRef)();
  const [totalAmount, setTotalAmount] = (0, import_react.useState)(0);
  const translation = (key, fallback) => {
    var _a2;
    const currentLang = import_utils.locales.locale || "en";
    const checkoutText = (_a2 = import_locales.default[currentLang]) == null ? void 0 : _a2[key];
    if (checkoutText) {
      return checkoutText;
    }
    return import_utils.locales.getText(key) || fallback;
  };
  const handlePaymentComplete = (result) => {
    var _a2;
    console.log("Payment completed:", result);
    (_a2 = callback.current) == null ? void 0 : _a2.call(callback, result);
    hideModal();
  };
  const handlePaymentCancel = () => {
    var _a2;
    console.log("Payment cancelled");
    hideModal();
    (_a2 = callback.current) == null ? void 0 : _a2.call(callback, { status: "cancelled" });
  };
  const CustomHeader = () => /* @__PURE__ */ import_react2.default.createElement("div", { style: {
    display: "flex",
    alignItems: "center",
    justifyContent: "space-between",
    padding: "16px 0",
    marginBottom: "16px",
    borderBottom: "1px solid #f0f0f0",
    backgroundColor: "#fafafa",
    margin: "-12px -36px 16px -36px",
    // 扩展到modal边缘
    paddingLeft: "36px",
    paddingRight: "36px"
  } }, /* @__PURE__ */ import_react2.default.createElement("div", { style: {
    display: "flex",
    alignItems: "center",
    gap: "12px"
  } }, "硬件状态"), /* @__PURE__ */ import_react2.default.createElement("div", null, translation("order.total"), "：", totalAmount), /* @__PURE__ */ import_react2.default.createElement("div", { style: {
    display: "flex",
    alignItems: "center",
    gap: "8px"
  } }, /* @__PURE__ */ import_react2.default.createElement(
    import_antd.Button,
    {
      type: "text",
      size: "small",
      icon: /* @__PURE__ */ import_react2.default.createElement(import_icons.CloseOutlined, null),
      onClick: handlePaymentCancel,
      style: {
        color: "#8c8c8c",
        border: "none",
        boxShadow: "none"
      }
    }
  )));
  const { showModal, hideModal, content } = (0, import_useModal.default)({
    title: null,
    // 移除默认标题，使用自定义头部
    children: /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, /* @__PURE__ */ import_react2.default.createElement(CustomHeader, null), /* @__PURE__ */ import_react2.default.createElement(
      import_content.default,
      {
        paymentData: paymentRef.current,
        moduleConfig: {
          showWalletPass: true,
          showCashPayment: true,
          showAdditionalModule: true,
          showPaymentOptions: true
        },
        onPaymentComplete: handlePaymentComplete,
        onPaymentCancel: handlePaymentCancel,
        onStatusChange: (status) => console.log("Status changed:", status),
        open_deposit: true
      }
    )),
    width: 880,
    // 808px 内容宽度 + 36px 左右内边距
    destroyOnClose: true,
    maskClosable: false,
    footer: null,
    // 移除默认的确定/取消按钮，让组件内部处理
    closeIcon: null,
    // 移除默认关闭图标，使用自定义的
    onCancel: handlePaymentCancel,
    wrapClassName: "payment-modal-wrapper"
  });
  const onOpen = async (props) => {
    var _a2, _b, _c;
    const checkout = pisellos.getModule("checkout");
    if (checkout) {
      await checkout.createLocalOrderAsync({
        orderData: (_a2 = props.data) == null ? void 0 : _a2.order_info,
        cartSummary: (_c = (_b = props.data) == null ? void 0 : _b.subtotal_info) == null ? void 0 : _c.subTotalList,
        autoPayment: true
      });
      const orderInfo = checkout.getCurrentOrderInfo();
      if (orderInfo && orderInfo.totalAmount) {
        setTotalAmount(orderInfo.totalAmount);
      }
    }
    paymentRef.current = props == null ? void 0 : props.data;
    callback.current = props == null ? void 0 : props.callback;
    showModal();
  };
  const onClose = () => {
    hideModal();
  };
  const init = () => {
    const state = {
      open: onOpen,
      close: onClose
    };
    console.log("PaymentPlugin", state);
  };
  (0, import_react.useEffect)(() => {
    init();
  }, []);
  return /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, content);
};
var PaymentPlugin_default = PaymentPlugin;
