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/linkly/hooks/normal.ts
var normal_exports = {};
__export(normal_exports, {
  useLinklyCancel: () => useLinklyCancel,
  useLinklyPayOrRefund: () => useLinklyPayOrRefund,
  useLinklySignature: () => useLinklySignature
});
module.exports = __toCommonJS(normal_exports);
var import_ahooks = require("ahooks");
var import_react = require("react");
var import_utils = require("@pisell/utils");
var import_service = require("../service");
var import_hooks = require("../../hooks");
var import_const = require("../../const");
var import_store = require("../../store");
var import_const2 = require("../const");
var import_payLog = __toESM(require("../../utils/payLog"));
var useLinklyPayOrRefund = (handleCheck) => {
  const dispatch = (0, import_store.useDispatch)();
  const { numRef, modeRef, orderIdRef, deviceRef, payRef, amountRef } = (0, import_hooks.useStoreRef)();
  const fail = (0, import_hooks.useFail)(0);
  const failRef = (0, import_react.useRef)(fail);
  const resultQueryRef = (0, import_react.useRef)(null);
  const handleLinklySuccessRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    failRef.current = fail;
  }, [fail]);
  const isPay = modeRef.current === import_const.ModeEnum.Pay || modeRef.current === import_const.ModeEnum.FullPay;
  const _pay = (0, import_ahooks.useRequest)(isPay ? import_service.linklyPayApi : import_service.linklyRefundApi, {
    manual: true,
    onSuccess: (res) => {
      onSuccess(res);
    },
    onError: (error) => {
      onError(error);
    }
  });
  const onSuccess = (res) => {
    var _a, _b, _c, _d;
    console.log(`linkly${isPay ? "Pay" : "Refund"}Success---->>>>>>>`, res);
    if (!handleCheck()) {
      return;
    }
    import_payLog.default.log(`Linkly：${isPay ? "支付" : "退款"}接口成功`, { res });
    const status = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.status;
    if (status == 0) {
      numRef.current = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b.session_id;
      dispatch((0, import_store.updateState)({ number: numRef.current }));
      dispatch((0, import_store.updateCustom)({ actionStatus: import_const2.LinklyActionStatus.Cancel }));
      setTimeout(() => {
        var _a2;
        (_a2 = resultQueryRef.current) == null ? void 0 : _a2.call(resultQueryRef);
      }, 300);
      return;
    }
    dispatch((0, import_store.updateCustom)({ actionStatus: import_const2.LinklyActionStatus.Empty }));
    if (status == 1) {
      (_c = handleLinklySuccessRef.current) == null ? void 0 : _c.call(handleLinklySuccessRef, res);
      return;
    }
    if (status < 0) {
      res.code = import_const.PayStatus.Unknown;
      (_d = failRef.current) == null ? void 0 : _d.call(failRef, res, false);
    }
  };
  const onError = (error) => {
    var _a;
    console.log(`linkly${isPay ? "Pay" : "Refund"}Error---->>>>>>>`, error);
    if (!handleCheck()) {
      return;
    }
    import_payLog.default.log(`Linkly：${isPay ? "支付" : "退款"}接口失败`, {
      error: JSON.stringify(error || {})
    });
    (_a = failRef.current) == null ? void 0 : _a.call(failRef, error, false);
  };
  const payOrRefund = (resultQuery, handleLinklySuccess) => {
    var _a, _b, _c;
    handleLinklySuccessRef.current = handleLinklySuccess;
    resultQueryRef.current = resultQuery;
    const param = {
      // 订单id
      order_id: orderIdRef.current,
      // 设备id
      pinpad_id: (_b = (_a = deviceRef == null ? void 0 : deviceRef.current) == null ? void 0 : _a.metadata) == null ? void 0 : _b.linkly_pinpad_id,
      // 交易金额, 包含选择的订单金额+手续费，订单金额+手续费=交易金额
      amount: isPay ? (_c = payRef.current) == null ? void 0 : _c.total : amountRef.current
    };
    import_payLog.default.setContext({ transactionNumber: String(orderIdRef.current) });
    import_payLog.default.log(`Linkly：发起${isPay ? "支付" : "退款"}`, { param });
    _pay.run(param);
  };
  return {
    action: { pay: _pay },
    payOrRefund
  };
};
var useLinklyCancel = (handleCheck) => {
  const dispatch = (0, import_store.useDispatch)();
  const { numRef, modeRef, deviceRef } = (0, import_hooks.useStoreRef)();
  const resultQueryRef = (0, import_react.useRef)(null);
  const isPay = modeRef.current === import_const.ModeEnum.Pay || modeRef.current === import_const.ModeEnum.FullPay;
  const cancelAction = (res, type) => {
    var _a;
    dispatch((0, import_store.updateCustom)({ buttonDisabled: false }));
    if (!handleCheck()) {
      return;
    }
    import_payLog.default.log(`Linkly：取消${type === "success" ? "成功" : "失败"}`, { res });
    (_a = resultQueryRef.current) == null ? void 0 : _a.call(resultQueryRef);
  };
  const _cancel = (0, import_ahooks.useRequest)(
    isPay ? import_service.linklyPayCancelApi : import_service.linklyRefundCancelApi,
    {
      manual: true,
      onSuccess: (res) => cancelAction(res, "success"),
      onError: (error) => cancelAction(error, "error")
    }
  );
  const linklyCancel = (resultQuery) => {
    var _a, _b;
    resultQueryRef.current = resultQuery;
    dispatch((0, import_store.updateCustom)({ buttonDisabled: true }));
    const param = {
      pinpad_id: (_b = (_a = deviceRef == null ? void 0 : deviceRef.current) == null ? void 0 : _a.metadata) == null ? void 0 : _b.linkly_pinpad_id,
      session_id: numRef.current
    };
    import_payLog.default.log("Linkly：发起取消", { param });
    _cancel.run(param);
    dispatch((0, import_store.updateCustom)({ actionStatus: import_const2.LinklyActionStatus.CancelWaiting }));
  };
  return {
    action: { cancel: _cancel },
    linklyCancel
  };
};
var useLinklySignature = (handleCheck) => {
  const dispatch = (0, import_store.useDispatch)();
  const { numRef, modeRef, deviceRef } = (0, import_hooks.useStoreRef)();
  const resultQueryRef = (0, import_react.useRef)(null);
  const isPay = modeRef.current === import_const.ModeEnum.Pay || modeRef.current === import_const.ModeEnum.FullPay;
  const signatureAction = (res, type) => {
    var _a;
    console.log("signatureAction---->>>>>>>", res);
    if (!handleCheck()) {
      return;
    }
    import_payLog.default.log(`Linkly：签名接口${type === "success" ? "成功" : "失败"}`, {
      res
    });
    (_a = resultQueryRef.current) == null ? void 0 : _a.call(resultQueryRef);
  };
  const _signature = (0, import_ahooks.useRequest)(
    isPay ? import_service.linklyPaySignatureApi : import_service.linklyRefundSignatureApi,
    {
      manual: true,
      onSuccess: (res) => signatureAction(res, "success"),
      onError: (error) => signatureAction(error, "error")
    }
  );
  const linklySignature = (status, resultQuery) => {
    var _a, _b;
    resultQueryRef.current = resultQuery;
    const param = {
      // 设备id
      pinpad_id: (_b = (_a = deviceRef == null ? void 0 : deviceRef.current) == null ? void 0 : _a.metadata) == null ? void 0 : _b.linkly_pinpad_id,
      // 交易流水号
      session_id: numRef.current,
      signature_flag: status
    };
    dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Loading));
    dispatch(
      (0, import_store.updateState)({
        title: import_utils.locales.getText("page.eftpos-pay.text.loading")(modeRef.current)
      })
    );
    dispatch(
      (0, import_store.updateCustom)({ actionStatus: import_const2.LinklyActionStatus.SignatureWaiting })
    );
    import_payLog.default.log("Linkly：发起签名", { param });
    _signature.run(param);
  };
  return {
    action: { signature: _signature },
    linklySignature
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useLinklyCancel,
  useLinklyPayOrRefund,
  useLinklySignature
});
