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/PaymentResultToastV2/PaymentResultToastV2Provider.tsx
var PaymentResultToastV2Provider_exports = {};
__export(PaymentResultToastV2Provider_exports, {
  PaymentResultToastV2Provider: () => PaymentResultToastV2Provider,
  clearAllPaymentResultToastsV2: () => clearAllPaymentResultToastsV2,
  closePaymentResultToastV2: () => closePaymentResultToastV2,
  default: () => PaymentResultToastV2Provider_default,
  getPaymentResultToastV2Count: () => getPaymentResultToastV2Count,
  globalToastV2Manager: () => globalToastV2Manager,
  setTillButtonV2: () => setTillButtonV2,
  showPaymentResultToastV2: () => showPaymentResultToastV2
});
module.exports = __toCommonJS(PaymentResultToastV2Provider_exports);
var import_react = __toESM(require("react"));
var import_react_dom = require("react-dom");
var import_PaymentResultToastV2 = __toESM(require("./PaymentResultToastV2"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var GlobalToastV2Manager = class {
  instances = /* @__PURE__ */ new Map();
  nextId = 1;
  TOAST_HEIGHT = 200;
  TOAST_MARGIN = 8;
  BOTTOM_OFFSET = 110;
  LEFT_OFFSET = 96;
  // 窄屏（手机）下左偏移收小、并给右侧留边，避免卡片被推出视口
  NARROW_LEFT_OFFSET = 16;
  RIGHT_OFFSET = 16;
  NARROW_BREAKPOINT = 600;
  cachedBottomOffset = null;
  tillButton = null;
  /** 左偏移：窄屏收小，宽屏沿用钱箱避让偏移。 */
  getLeftOffset() {
    if (typeof window !== "undefined" && window.innerWidth <= this.NARROW_BREAKPOINT) {
      return this.NARROW_LEFT_OFFSET;
    }
    return this.LEFT_OFFSET;
  }
  setTillButton(tillButton) {
    this.tillButton = tillButton;
    this.cachedBottomOffset = null;
  }
  generateId() {
    return `toast-v2-${this.nextId++}`;
  }
  calcBottomOffset() {
    var _a;
    if (this.cachedBottomOffset !== null) return this.cachedBottomOffset;
    if ((_a = this.tillButton) == null ? void 0 : _a.y) {
      this.cachedBottomOffset = window.innerHeight - this.tillButton.y + 8 + this.tillButton.size;
      return this.cachedBottomOffset;
    }
    const body = document.querySelector("body");
    if (body) {
      const rect = body.getBoundingClientRect();
      this.cachedBottomOffset = window.innerHeight - rect.bottom + 16;
    } else {
      this.cachedBottomOffset = this.BOTTOM_OFFSET;
    }
    return this.cachedBottomOffset;
  }
  bottomPosition(offsetIndex, totalCount) {
    const reversedIndex = totalCount - 1 - offsetIndex;
    return this.calcBottomOffset() + (this.TOAST_HEIGHT + this.TOAST_MARGIN) * reversedIndex;
  }
  updatePositions() {
    const instances = Array.from(this.instances.values());
    const total = instances.length;
    instances.forEach((instance, index) => {
      instance.offsetIndex = index;
      const bottom = this.bottomPosition(index, total);
      const { container } = instance;
      if (!container) return;
      container.classList.add("toast-moving");
      container.style.position = "fixed";
      container.style.left = `${this.getLeftOffset()}px`;
      container.style.bottom = `${bottom}px`;
      container.style.top = "auto";
      container.style.right = `${this.RIGHT_OFFSET}px`;
      container.style.transition = "all 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94)";
      container.style.pointerEvents = "none";
      container.style.width = "auto";
      container.style.height = "auto";
      container.style.transform = "none";
      container.style.margin = "0";
      setTimeout(() => container == null ? void 0 : container.classList.remove("toast-moving"), 400);
    });
  }
  addToast(config) {
    const id = this.generateId();
    this.cachedBottomOffset = null;
    const container = document.createElement("div");
    container.setAttribute("data-toast-v2-id", id);
    const bottom = this.calcBottomOffset();
    container.style.position = "fixed";
    container.style.left = `${this.getLeftOffset()}px`;
    container.style.bottom = `${bottom}px`;
    container.style.top = "auto";
    container.style.right = `${this.RIGHT_OFFSET}px`;
    container.style.transform = "none";
    container.style.pointerEvents = "none";
    container.style.width = "auto";
    container.style.height = "auto";
    container.style.margin = "0";
    const modalMask = document.querySelector(
      ".payment-modal-wrapper"
    );
    if (modalMask) {
      container.style.zIndex = `${Number(modalMask.style.zIndex || 1e3) + 1}`;
    } else {
      container.style.zIndex = "1001";
    }
    document.body.appendChild(container);
    this.instances.set(id, { id, container, offsetIndex: this.instances.size });
    (0, import_react_dom.render)(
      /* @__PURE__ */ import_react.default.createElement(
        import_PaymentResultToastV2.default,
        {
          ...config,
          visible: true,
          onClose: () => {
            var _a;
            (_a = config.onClose) == null ? void 0 : _a.call(config);
            this.removeToast(id);
          }
        }
      ),
      container
    );
    return id;
  }
  removeToast(id) {
    const instance = this.instances.get(id);
    if (!instance) return;
    const { container } = instance;
    if (container) {
      container.classList.add("toast-exiting");
      setTimeout(() => {
        (0, import_react_dom.unmountComponentAtNode)(container);
        if (document.body.contains(container)) {
          document.body.removeChild(container);
        }
        this.instances.delete(id);
        this.updatePositions();
      }, 300);
    } else {
      this.instances.delete(id);
      this.updatePositions();
    }
  }
  clearAll() {
    this.instances.forEach(({ container }) => {
      if (container) {
        (0, import_react_dom.unmountComponentAtNode)(container);
        if (document.body.contains(container)) {
          document.body.removeChild(container);
        }
      }
    });
    this.instances.clear();
  }
  getCount() {
    return this.instances.size;
  }
  initResizeListener() {
    const handleResize = () => {
      this.cachedBottomOffset = null;
      this.updatePositions();
    };
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }
};
var globalToastV2Manager = new GlobalToastV2Manager();
if (typeof window !== "undefined") {
  globalToastV2Manager.initResizeListener();
}
var showPaymentResultToastV2 = (config) => globalToastV2Manager.addToast(config);
var closePaymentResultToastV2 = (id) => {
  if (id) {
    globalToastV2Manager.removeToast(id);
  } else {
    globalToastV2Manager.clearAll();
  }
};
var clearAllPaymentResultToastsV2 = () => globalToastV2Manager.clearAll();
var getPaymentResultToastV2Count = () => globalToastV2Manager.getCount();
var setTillButtonV2 = (tillButton) => globalToastV2Manager.setTillButton(tillButton);
var PaymentResultToastV2Provider = ({ children }) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  const interaction = (_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.interaction;
  (0, import_react.useEffect)(() => {
    var _a2, _b2;
    (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.asyncDataManager) == null ? void 0 : _b2.call(_a2, { module: "get", key: "till_button" }).then((res) => globalToastV2Manager.setTillButton(res)).catch((err) => {
      console.error("Failed to get till_button for V2 toast:", err);
    });
  }, [interaction]);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, children);
};
var PaymentResultToastV2Provider_default = PaymentResultToastV2Provider;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PaymentResultToastV2Provider,
  clearAllPaymentResultToastsV2,
  closePaymentResultToastV2,
  getPaymentResultToastV2Count,
  globalToastV2Manager,
  setTillButtonV2,
  showPaymentResultToastV2
});
