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/components/PaymentResultToast/index.tsx
var PaymentResultToast_exports = {};
__export(PaymentResultToast_exports, {
  PaymentResultToastProvider: () => import_PaymentResultToastProvider.PaymentResultToastProvider,
  clearAllPaymentResultToasts: () => import_PaymentResultToastProvider.clearAllPaymentResultToasts,
  closeAllPaymentResultToasts: () => import_utils2.closeAllPaymentResultToasts,
  closePaymentResultToast: () => import_PaymentResultToastProvider.closePaymentResultToast,
  debounce: () => import_utils2.debounce,
  default: () => PaymentResultToast_default,
  getDefaultModalConfig: () => import_utils2.getDefaultModalConfig,
  getPaymentResultToastCount: () => import_PaymentResultToastProvider.getPaymentResultToastCount,
  getToastCount: () => import_utils2.getToastCount,
  globalToastManager: () => import_PaymentResultToastProvider.globalToastManager,
  hasActivePaymentResultToasts: () => import_utils2.hasActivePaymentResultToasts,
  setTillButton: () => import_PaymentResultToastProvider.setTillButton,
  showPaymentResultToast: () => import_PaymentResultToastProvider.showPaymentResultToast,
  showPaymentResultToastFailed: () => import_PaymentResultToastProvider.showPaymentResultToastFailed,
  showPaymentResultToastPartiallyPaid: () => import_PaymentResultToastProvider.showPaymentResultToastPartiallyPaid,
  showPaymentResultToastSuccess: () => import_PaymentResultToastProvider.showPaymentResultToastSuccess,
  showPaymentResultToastWalletPassFailed: () => import_PaymentResultToastProvider.showPaymentResultToastWalletPassFailed,
  usePaymentResultToast: () => import_PaymentResultToastProvider.usePaymentResultToast
});
module.exports = __toCommonJS(PaymentResultToast_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_XClose = __toESM(require("@pisell/icon/es/XClose"));
var import_utils = require("./utils");
var import_index = require("./index.less");
var import_PaymentResultToastProvider = require("./PaymentResultToastProvider");
var import_utils2 = require("./utils");
var COMPONENT_PREFIX = "payment-result-toast";
var getClassName = (...classNames) => {
  return (0, import_classnames.default)(
    classNames.filter(Boolean).map(
      (className) => typeof className === "string" && !className.startsWith(COMPONENT_PREFIX) ? `${COMPONENT_PREFIX}-${className}` : className
    )
  );
};
var getBaseClassName = (extraClassName) => {
  return (0, import_classnames.default)(COMPONENT_PREFIX, extraClassName);
};
var PaymentResultToast = (0, import_react.memo)((props) => {
  const {
    // 基础属性
    visible = false,
    onClose,
    className,
    // 内容属性（已计算好）
    statusClassName = "success",
    title,
    subtitle,
    changeDueText,
    // 操作按钮
    actionButtons = [],
    // 倒计时配置
    autoClose = false,
    autoCloseDelay = 30,
    showCountdownProgress = true,
    onCountdownComplete,
    // Modal 配置
    width,
    height,
    mask = false,
    maskClosable = false,
    closable = true,
    zIndex,
    clickOutsideToClose = true
  } = props;
  const [isAnimating, setIsAnimating] = (0, import_react.useState)(false);
  const countdownTimeoutRef = (0, import_react.useRef)(null);
  const modalContentRef = (0, import_react.useRef)(null);
  const modalRef = (0, import_react.useRef)(null);
  const onCloseRef = (0, import_react.useRef)(onClose);
  const onCountdownCompleteRef = (0, import_react.useRef)(onCountdownComplete);
  onCloseRef.current = onClose;
  onCountdownCompleteRef.current = onCountdownComplete;
  const modalConfig = (0, import_utils.getDefaultModalConfig)();
  const clearCountdown = (0, import_react.useCallback)(() => {
    if (countdownTimeoutRef.current) {
      clearTimeout(countdownTimeoutRef.current);
      countdownTimeoutRef.current = null;
    }
    setIsAnimating(false);
  }, []);
  const startCountdown = (0, import_react.useCallback)(() => {
    if (!autoClose || !visible) return;
    clearCountdown();
    setIsAnimating(true);
    countdownTimeoutRef.current = setTimeout(() => {
      var _a, _b;
      countdownTimeoutRef.current = null;
      (_a = onCountdownCompleteRef.current) == null ? void 0 : _a.call(onCountdownCompleteRef);
      (_b = onCloseRef.current) == null ? void 0 : _b.call(onCloseRef);
    }, autoCloseDelay * 1e3);
  }, [autoClose, autoCloseDelay, visible, clearCountdown]);
  (0, import_react.useEffect)(() => {
    if (visible) {
      setIsAnimating(false);
      if (autoClose) {
        startCountdown();
      }
    } else {
      clearCountdown();
    }
    return clearCountdown;
  }, [visible, autoClose, startCountdown, clearCountdown]);
  (0, import_react.useEffect)(() => {
    return clearCountdown;
  }, [clearCountdown]);
  (0, import_react.useEffect)(() => {
    if (!clickOutsideToClose || !visible) {
      return;
    }
    const handleClickOutside = (event) => {
      var _a;
      const target = event.target;
      const modalContent = modalContentRef.current;
      if (!modalContent) {
        return;
      }
      if (!modalContent.contains(target)) {
        clearCountdown();
        (_a = onCloseRef.current) == null ? void 0 : _a.call(onCloseRef);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [clickOutsideToClose, visible, clearCountdown]);
  const handleClose = (0, import_react.useCallback)(() => {
    var _a;
    clearCountdown();
    (_a = onCloseRef.current) == null ? void 0 : _a.call(onCloseRef);
  }, [clearCountdown]);
  const renderTitle = () => {
    if (!subtitle) {
      return null;
    }
    if (closable) {
      return /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center", gap: "16px" } }, subtitle, /* @__PURE__ */ import_react.default.createElement(
        import_materials.Button,
        {
          style: { display: "flex", alignItems: "center", justifyContent: "center" },
          type: "text",
          icon: /* @__PURE__ */ import_react.default.createElement(import_XClose.default, { style: { fontSize: "40px", color: "white" } }),
          onClick: handleClose
        }
      ));
    }
    return subtitle;
  };
  const renderChangeDueInfo = () => {
    if (!changeDueText) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName("subtitle") }, /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName("subtitle-text change-due") }, changeDueText));
  };
  const renderCountdownProgress = () => {
    if (!autoClose || !showCountdownProgress || !visible) {
      return null;
    }
    const progressBarClassName = (0, import_classnames.default)(
      getClassName("countdown-progress-bar"),
      { "animating": isAnimating }
    );
    const animationStyle = isAnimating ? {
      animationDuration: `${autoCloseDelay}s`
    } : {};
    return /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName("countdown") }, /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName("countdown-text") }), /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName("countdown-progress") }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: progressBarClassName,
        style: animationStyle
      }
    )));
  };
  const handleButtonClick = (0, import_react.useCallback)(() => {
    if (autoClose && countdownTimeoutRef.current) {
      clearCountdown();
    }
  }, [autoClose, clearCountdown]);
  const renderActionButtons = () => {
    if (!actionButtons || actionButtons.length === 0) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { ...{ onClick: handleButtonClick } }, /* @__PURE__ */ import_react.default.createElement(import_materials.ButtonGroupPreview, { buttonGroupList: actionButtons }));
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open: visible,
      onCancel: handleClose,
      footer: null,
      width: width || modalConfig.width,
      mask,
      maskClosable,
      closable: false,
      className: getBaseClassName(`${className} ${statusClassName} ${mask ? "payment-result-toast-with-mask" : ""}`),
      wrapClassName: `toast-modal-wrap ${mask ? "toast-modal-wrap-with-mask" : ""}`,
      style: {
        ...height ? { height: `${height}px` } : {},
        // 确保Modal不会被默认定位影响
        position: "static",
        top: "auto",
        left: "auto",
        right: "auto",
        bottom: "auto",
        transform: "none",
        margin: 0,
        padding: 0,
        width: "100%",
        maxWidth: "none"
      },
      destroyOnClose: false,
      forceRender: true,
      focusTriggerAfterClose: false,
      transitionName: "",
      maskTransitionName: "",
      centered: mask,
      getContainer: mask ? void 0 : false
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: modalContentRef,
        className: getClassName("content", statusClassName),
        style: {
          position: "relative",
          zIndex: 1
        }
      },
      renderTitle(),
      renderChangeDueInfo(),
      renderActionButtons(),
      renderCountdownProgress()
    )
  );
});
PaymentResultToast.displayName = "PaymentResultToast";
var PaymentResultToast_default = PaymentResultToast;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PaymentResultToastProvider,
  clearAllPaymentResultToasts,
  closeAllPaymentResultToasts,
  closePaymentResultToast,
  debounce,
  getDefaultModalConfig,
  getPaymentResultToastCount,
  getToastCount,
  globalToastManager,
  hasActivePaymentResultToasts,
  setTillButton,
  showPaymentResultToast,
  showPaymentResultToastFailed,
  showPaymentResultToastPartiallyPaid,
  showPaymentResultToastSuccess,
  showPaymentResultToastWalletPassFailed,
  usePaymentResultToast
});
