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/eftposPay/adyenPos/index.tsx
var adyenPos_exports = {};
__export(adyenPos_exports, {
  default: () => adyenPos_default
});
module.exports = __toCommonJS(adyenPos_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_index = require("./index.less");
var import_store = require("../store");
var import_hooks = require("../hooks");
var import_const = require("../const");
var import_helper = require("../helper");
var import_aggregatePayment = require("../aggregatePayment");
var import_types = require("../aggregatePayment/types");
var import_payLog = __toESM(require("../utils/payLog"));
var AdyenPos = ({ onChange, isNativeApp }) => {
  var _a, _b, _c;
  const data = (0, import_store.useContextData)();
  const dispatch = (0, import_store.useDispatch)();
  const {
    statusRef,
    modeRef,
    amountRef,
    orderIdRef,
    numRef,
    deviceRef,
    payRef,
    netRef
  } = (0, import_hooks.useStoreRef)();
  const fail = (0, import_hooks.useFail)(0);
  const failRef = (0, import_react.useRef)(fail);
  const initRef = (0, import_react.useRef)(false);
  const unloadRef = (0, import_react.useRef)(false);
  const clearRef = (0, import_react.useRef)(null);
  const [showDeclineModal, setShowDeclineModal] = (0, import_react.useState)(false);
  const payment = (0, import_aggregatePayment.usePayment)({
    paymentCode: "ADYEN_POS",
    paymentNum: numRef.current || "",
    shouldContinue: () => !(unloadRef.current || !netRef.current),
    callback: (_instance, response) => {
      console.log("response =>>>> ", response);
      const { status, data: responseData } = response || {};
      if (status === import_types.EPaymentStatus.Success) {
        handleSuccess(responseData);
      } else if (status === import_types.EPaymentStatus.Failed) {
        handleFail(responseData);
      } else if (status === import_types.EPaymentStatus.Processing) {
        handleProcessing(responseData);
      }
    }
  });
  (0, import_react.useEffect)(() => {
    failRef.current = fail;
  }, [fail]);
  (0, import_react.useEffect)(() => {
    return () => {
      unloadRef.current = true;
      if (clearRef.current) {
        clearTimeout(clearRef.current);
      }
    };
  }, []);
  (0, import_react.useEffect)(() => {
    var _a2;
    if (statusRef.current === import_const.StatusEnum.Success || statusRef.current === import_const.StatusEnum.Fail) {
      return;
    }
    if (initRef.current) {
      return;
    }
    initRef.current = true;
    if (numRef.current) {
      console.log("payment.query");
      payment.query();
      return;
    }
    const isPay = modeRef.current === import_const.ModeEnum.Pay || modeRef.current === import_const.ModeEnum.FullPay;
    if (isPay) {
      payment.run();
    } else {
      payment.run({
        params: {
          adyenpos: { order_payment_id: (_a2 = data == null ? void 0 : data.custom) == null ? void 0 : _a2.order_payment_id }
        }
      });
    }
  }, []);
  const handleSuccess = (responseData) => {
    var _a2, _b2;
    const _response = ((_a2 = responseData == null ? void 0 : responseData.data) == null ? void 0 : _a2.external_unified_response) || {};
    const signatureRequired = (_b2 = _response == null ? void 0 : _response.cvm_result) == null ? void 0 : _b2.signature_required;
    dispatch(
      (0, import_store.updateState)({
        status: "success",
        title: import_utils.locales.getText("page.eftpos-pay.text.successful")(
          modeRef.current
        )
      })
    );
    dispatch((0, import_store.updateNextStep)());
    dispatch((0, import_store.updateCustom)({ card_reader_surcharge: 0, receipt: [] }));
    const params = (0, import_helper.getPayParams)(
      {
        mode: modeRef.current,
        amount: amountRef.current,
        order_id: orderIdRef.current,
        number: numRef.current,
        device: deviceRef.current,
        pay: payRef.current,
        custom: { card_reader_surcharge: 0, receipt: [] }
      },
      "success"
    );
    const onChangeParams = { ...params, receipt: [] };
    if (signatureRequired) {
      dispatch(
        (0, import_store.updateCustom)({ successParams: onChangeParams, signatureCheck: true })
      );
    } else {
      clearRef.current = setTimeout(() => {
        if (unloadRef.current) return;
        onChange("success", params);
      }, 1e3);
    }
  };
  const handleSignatureAccept = () => {
    var _a2, _b2;
    import_payLog.default.log("点击签名核查通过");
    if ((_a2 = data == null ? void 0 : data.custom) == null ? void 0 : _a2.successParams) {
      onChange("success", (_b2 = data == null ? void 0 : data.custom) == null ? void 0 : _b2.successParams);
      dispatch((0, import_store.updateCustom)({ successParams: null, signatureCheck: false }));
    }
  };
  const handleSignatureDecline = () => {
    import_payLog.default.log("点击拒绝签名-弹出二次确认弹窗");
    setShowDeclineModal(true);
  };
  const handleDeclineModalConfirm = () => {
    var _a2, _b2;
    import_payLog.default.log("拒绝签名二次确认弹窗-点击确认");
    setShowDeclineModal(false);
    if ((_a2 = data == null ? void 0 : data.custom) == null ? void 0 : _a2.successParams) {
      onChange("success", (_b2 = data == null ? void 0 : data.custom) == null ? void 0 : _b2.successParams);
      dispatch((0, import_store.updateCustom)({ successParams: null, signatureCheck: false }));
    }
  };
  const handleDeclineModalCancel = () => {
    import_payLog.default.log("拒绝签名二次确认弹窗-点击取消");
    setShowDeclineModal(false);
  };
  const handleFail = (errorData) => {
    var _a2, _b2;
    const { message, response_code } = ((_a2 = errorData == null ? void 0 : errorData.data) == null ? void 0 : _a2.external_unified_response) || {};
    const _code = `${response_code || ""}` || import_const.PayStatus.PaymentFailed;
    const _message = message || (errorData == null ? void 0 : errorData.message) || "";
    console.log("handleFail =>>>> ", errorData, _code, _message);
    (_b2 = failRef.current) == null ? void 0 : _b2.call(failRef, { code: _code, message: _message }, true);
  };
  const handleProcessing = (processingData) => {
    var _a2, _b2;
    console.log("handleProcessing =>>>> ", processingData);
    const _response = ((_a2 = processingData == null ? void 0 : processingData.data) == null ? void 0 : _a2.external_unified_response) || {};
    if ((_b2 = _response == null ? void 0 : _response.cvm_result) == null ? void 0 : _b2.signature_required) {
      dispatch(
        (0, import_store.updateCustom)({
          buttonDisabled: true,
          signingInProgress: true
        })
      );
    }
  };
  const onCancel = () => {
    import_payLog.default.log("点击取消按钮");
    payment.action({ action: "abort" });
    dispatch((0, import_store.updateCustom)({ buttonDisabled: true }));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-eftpos-pay-adyen" }, (data == null ? void 0 : data.status) === import_const.StatusEnum.Success && ((_a = data == null ? void 0 : data.custom) == null ? void 0 : _a.signatureCheck) && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText("page.eftpos-pay.adyen.pos.signature.check.title")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText("page.eftpos-pay.adyen.pos.signature.check.text")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      className: "pisell-lowcode__eft-pay-step-item-btn",
      onClick: handleSignatureDecline
    },
    import_utils.locales.getText("page.eftpos-pay.adyen.pos.signature.declined")
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      type: "primary",
      className: "pisell-lowcode__eft-pay-step-item-btn",
      onClick: handleSignatureAccept
    },
    import_utils.locales.getText("page.eftpos-pay.adyen.pos.signature.approved")
  )), /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open: showDeclineModal,
      title: import_utils.locales.getText(
        "page.eftpos-pay.adyen.pos.signature.declined.modal.title"
      ),
      okText: import_utils.locales.getText(
        "page.eftpos-pay.adyen.pos.signature.declined.modal.ok"
      ),
      cancelText: import_utils.locales.getText("page.eftpos-pay.text.back"),
      onOk: handleDeclineModalConfirm,
      onCancel: handleDeclineModalCancel
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip",
        style: { paddingTop: "24px" }
      },
      import_utils.locales.getText(
        "page.eftpos-pay.adyen.pos.signature.declined.modal.content"
      )
    )
  )), (data == null ? void 0 : data.status) === import_const.StatusEnum.Loading && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, ((_b = data == null ? void 0 : data.custom) == null ? void 0 : _b.signingInProgress) ? (
    // 客户正在签名：不可取消，展示等待提示
    /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText("page.eftpos-pay.adyen.pos.signature.signing")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText(
      "page.eftpos-pay.adyen.pos.signature.signing.tip"
    )))
  ) : (
    // 普通处理中
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText("page.eftpos-pay.text.processing"))
  ), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      className: "pisell-lowcode__eft-pay-step-item-btn",
      onClick: onCancel,
      disabled: !!((_c = data == null ? void 0 : data.custom) == null ? void 0 : _c.buttonDisabled)
    },
    import_utils.locales.getText("page.eftpos-pay.text.cancel")
  ))));
};
var adyenPos_default = AdyenPos;
