"use strict";
const common_vendor = require("../common/vendor.js");
const config_user = require("../config/user.js");
class OrderMonitor {
  constructor() {
    this.activeOrders = /* @__PURE__ */ new Map();
    this.timer = null;
    this.checkInterval = 1e4;
    this.isRunning = false;
    this.currentPage = null;
  }
  /**
   * 添加订单到监控队列
   * @param {Object} orderData 订单数据对象，必须包含orderId字段
   * @param {String} pageName 关联的页面名称，默认为'return'
   */
  addOrder(orderData, pageName = "return") {
    if (!orderData || !orderData.orderId) {
      common_vendor.index.__f__("error", "at utils/orderMonitor.js:23", "添加订单监控失败：无效的订单数据");
      return;
    }
    common_vendor.index.__f__("log", "at utils/orderMonitor.js:27", `添加订单到监控队列: ${orderData.orderId}, 页面: ${pageName}`);
    this.activeOrders.set(orderData.orderId, {
      ...orderData,
      pageName
    });
    if (!this.isRunning) {
      this.start();
    }
  }
  /**
   * 移除订单的监控
   * @param {Object} params 包含orderId或pageName的对象
   */
  removeOrder(params) {
    if (!params)
      return;
    if (params.orderId && this.activeOrders.has(params.orderId)) {
      common_vendor.index.__f__("log", "at utils/orderMonitor.js:48", "从监控队列移除订单:", params.orderId);
      this.activeOrders.delete(params.orderId);
    } else if (params.pageName) {
      common_vendor.index.__f__("log", "at utils/orderMonitor.js:53", "从监控队列移除页面相关订单:", params.pageName);
      for (const [orderId, data] of this.activeOrders.entries()) {
        if (data.pageName === params.pageName) {
          this.activeOrders.delete(orderId);
        }
      }
    }
    if (this.activeOrders.size === 0) {
      this.stop();
    }
  }
  /**
   * 设置当前活跃页面
   * @param {String} pageName 页面名称
   */
  setActivePage(pageName) {
    this.currentPage = pageName;
    common_vendor.index.__f__("log", "at utils/orderMonitor.js:73", "设置当前活跃页面:", pageName);
  }
  /**
   * 启动监控服务
   */
  start() {
    if (this.isRunning)
      return;
    common_vendor.index.__f__("log", "at utils/orderMonitor.js:82", "启动订单监控服务");
    this.isRunning = true;
    this.checkOrders();
    this.timer = setInterval(() => {
      this.checkOrders();
    }, this.checkInterval);
  }
  /**
   * 停止监控服务
   */
  stop() {
    if (!this.isRunning)
      return;
    common_vendor.index.__f__("log", "at utils/orderMonitor.js:97", "停止订单监控服务");
    this.isRunning = false;
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  }
  /**
   * 检查所有活跃订单的状态
   */
  async checkOrders() {
    if (this.activeOrders.size === 0)
      return;
    common_vendor.index.__f__("log", "at utils/orderMonitor.js:112", `检查 ${this.activeOrders.size} 个活跃订单状态, 当前页面: ${this.currentPage}`);
    for (const [orderId, data] of this.activeOrders.entries()) {
      try {
        if (!data.pageName || data.pageName === "return") {
          if (this.currentPage === "return" || this.currentPage === null) {
            await this.checkOrderStatus(orderId);
          } else {
          }
        }
      } catch (error) {
      }
    }
  }
  /**
   * 检查单个订单的状态
   * @param {String} orderId 订单ID
   */
  async checkOrderStatus(orderId) {
    try {
      common_vendor.index.__f__("log", "at utils/orderMonitor.js:137", `检查订单 ${orderId} 的状态`);
      const result = await config_user.queryById(orderId);
      if (result.code === 200 && result.data) {
        const orderData = result.data;
        const existingData = this.activeOrders.get(orderId);
        const pageName = existingData ? existingData.pageName : null;
        this.activeOrders.set(orderId, {
          ...orderData,
          pageName
        });
        if (orderData.orderStatus === "used_done" || orderData.orderStatus === "used_down") {
          common_vendor.index.__f__("log", "at utils/orderMonitor.js:153", `订单 ${orderId} 已完成!`);
          common_vendor.index.$emit("orderCompleted", orderData);
          common_vendor.index.showToast({
            title: "风扇归还成功",
            icon: "success",
            duration: 2e3
          });
          const innerAudioContext = common_vendor.index.createInnerAudioContext();
          innerAudioContext.src = "/static/audio/return_success.mp3";
          innerAudioContext.play();
          this.removeOrder({ orderId });
          setTimeout(() => {
            common_vendor.index.showModal({
              title: "归还成功",
              content: "风扇已归还成功，剩余押金将退还到您的账户",
              confirmText: "查看详情",
              success: (res) => {
                if (res.confirm) {
                  common_vendor.index.redirectTo({
                    url: `/pages/order/return-success?orderId=${orderId}`
                  });
                }
              }
            });
          }, 500);
        }
      }
    } catch (error) {
      common_vendor.index.__f__("error", "at utils/orderMonitor.js:192", `检查订单 ${orderId} 状态出错:`, error);
    }
  }
}
const orderMonitor = new OrderMonitor();
common_vendor.index.onAppRoute((route) => {
  const pagePath = route.path || "";
  const pageSegments = pagePath.split("/");
  const pageName = pageSegments[pageSegments.length - 1];
  orderMonitor.setActivePage(pageName || null);
  common_vendor.index.__f__("log", "at utils/orderMonitor.js:209", "页面切换:", pagePath, "当前活跃页面:", pageName);
});
const initOrderMonitor = () => {
  const lastActiveOrderId = common_vendor.index.getStorageSync("activeOrderId");
  if (lastActiveOrderId) {
    const lastOrderData = { orderId: lastActiveOrderId };
    orderMonitor.addOrder(lastOrderData, "return");
  }
};
initOrderMonitor();
exports.orderMonitor = orderMonitor;
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/orderMonitor.js.map
