var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pay/toC/PaymentMethods/hooks/index.tsx
var hooks_exports = {};
__export(hooks_exports, {
  usePaymentRedirect: () => usePaymentRedirect
});
module.exports = __toCommonJS(hooks_exports);
var import_ahooks = require("ahooks");
var import_react = require("react");
var usePaymentRedirect = (options) => {
  const { orderId, paymentType, onAfterReturn } = options;
  const [isReturned, setIsReturned] = (0, import_react.useState)(false);
  const handleRedirect = (0, import_ahooks.useMemoizedFn)(
    async (url) => {
      try {
        const currentState = window.history.state || {};
        window.history.replaceState(
          {
            ...currentState,
            paymentRedirected: true,
            paymentType,
            orderId,
            timestamp: Date.now()
          },
          "",
          window.location.href
        );
        window.location.href = url;
      } catch (error) {
        throw error;
      }
    }
  );
  const resetState = (0, import_ahooks.useMemoizedFn)(() => {
    setIsReturned(false);
  });
  (0, import_react.useEffect)(() => {
    const handlePageShow = (event) => {
      if (!orderId) return;
      const historyState = window.history.state;
      if ((historyState == null ? void 0 : historyState.paymentRedirected) && (historyState == null ? void 0 : historyState.paymentType) === paymentType && (historyState == null ? void 0 : historyState.orderId) === orderId) {
        console.log(
          `检测到从 ${paymentType} 返回, event.persisted:`,
          event.persisted
        );
        setIsReturned(true);
        if (onAfterReturn) {
          onAfterReturn();
        }
        const newState = { ...historyState };
        delete newState.paymentRedirected;
        delete newState.paymentType;
        delete newState.timestamp;
        delete newState.orderId;
        window.history.replaceState(newState, "", window.location.href);
      }
    };
    handlePageShow({ persisted: false });
    window.addEventListener("pageshow", handlePageShow);
    return () => {
      window.removeEventListener("pageshow", handlePageShow);
    };
  }, [orderId, paymentType]);
  return {
    isReturned,
    handleRedirect,
    resetState
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  usePaymentRedirect
});
