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/eftpos/SettlementModal/index.tsx
var SettlementModal_exports = {};
__export(SettlementModal_exports, {
  default: () => SettlementModal_default
});
module.exports = __toCommonJS(SettlementModal_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_materials = require("@pisell/materials");
var import_index = require("./index.less");
var import_usePrefix = __toESM(require("../../../hooks/usePrefix"));
var import_Action = __toESM(require("./Action"));
var import_types = require("./types");
var import_utils = require("./utils");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var SettlementModal = ({
  open,
  onClose,
  data
}) => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  const prefix = (0, import_usePrefix.default)();
  const { isTerminal, interaction } = ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
  const [receiptModalShow, setReceiptModalShow] = (0, import_react.useState)(false);
  const [receiptModalData, setReceiptModalData] = (0, import_react.useState)(null);
  const [action, setAction] = (0, import_react.useState)(null);
  const [status, setStatus] = (0, import_react.useState)(
    import_types.ESettlementStatus.Processing
  );
  const runSettlementQuery = (0, import_ahooks.useMemoizedFn)(() => {
    setAction(null);
    setStatus(import_types.ESettlementStatus.Processing);
    console.log("settlementQuery_start");
    (0, import_utils.settlementQuery)(data, (status2, res) => {
      console.log("settlementQuery_callback", status2, res);
      const parsedData = (0, import_utils.parseMX51Params)(res == null ? void 0 : res.data, status2, res);
      console.log("settlementQuery_parsedData", parsedData);
      if (parsedData) {
        setAction(parsedData);
      }
      setStatus(status2);
    });
  });
  const onCloseReceiptModal = () => {
    setReceiptModalData(null);
    setReceiptModalShow(false);
  };
  const onElementClick = (element) => {
    var _a2, _b;
    console.log("onElementClick", element);
    switch (element.key) {
      case import_types.EButtonKeyType.SettlementComplete:
        onClose();
        break;
      case import_types.EButtonKeyType.RetrySettlement:
        runSettlementQuery();
        break;
      case import_types.EButtonKeyType.PrintMerchantReceipt:
        const _isTerminal = typeof isTerminal === "function" ? isTerminal() : false;
        if (_isTerminal) {
          const receipt = (element == null ? void 0 : element.receipt) ? Array.isArray(element == null ? void 0 : element.receipt) ? element == null ? void 0 : element.receipt : [element == null ? void 0 : element.receipt] : [];
          (_b = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.postMessageToApp) == null ? void 0 : _b.call(_a2, {
            module: "pos",
            key: "print",
            data: receipt
          });
        } else {
          setReceiptModalShow(true);
          setReceiptModalData(element == null ? void 0 : element.receipt);
        }
        break;
    }
  };
  (0, import_react.useEffect)(() => {
    if (open && data) {
      runSettlementQuery();
    }
  }, [open, data, runSettlementQuery]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      closable: false,
      header: false,
      footer: null,
      width: 600
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-eftpos-settlement-modal" }, status === import_types.ESettlementStatus.Processing && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          backgroundImage: `url(${prefix}/loading.png)`,
          marginBottom: "24px"
        },
        className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-load"
      }
    ), status === import_types.ESettlementStatus.Failed && /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          backgroundImage: `url(${prefix}/warn.png)`,
          marginBottom: "24px"
        },
        className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-warn"
      }
    ), /* @__PURE__ */ import_react.default.createElement(import_Action.default, { data: action, onClick: onElementClick })),
    receiptModalShow ? /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        open: receiptModalShow,
        onCancel: onCloseReceiptModal,
        maskClosable: true,
        footer: null
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            padding: "16px",
            display: "flex",
            alignItems: "center",
            justifyContent: "center"
          }
        },
        /* @__PURE__ */ import_react.default.createElement(
          "span",
          {
            style: {
              display: "inline-block",
              whiteSpace: "pre-wrap",
              fontFamily: "monospace",
              backgroundColor: "#fff"
            }
          },
          receiptModalData
        )
      )
    ) : null
  );
};
var SettlementModal_default = SettlementModal;
