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/ticketBooking/hooks/pisellos/useProducts.ts
var useProducts_exports = {};
__export(useProducts_exports, {
  useProducts: () => useProducts
});
module.exports = __toCommonJS(useProducts_exports);
var import_ahooks = require("ahooks");
var import_bookingTicket = require("./bookingTicket");
var import_react = require("react");
var import_OsKeyContext = require("../../context/OsKeyContext");
var import_productCache = require("../../utils/productCache");
var import_hooks = require("../../../../hooks");
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
var useProducts = (notAvailableRef) => {
  var _a;
  const bookingTicket = (0, import_bookingTicket.useBookingTicket)();
  const osKey = (0, import_OsKeyContext.useOsKey)();
  const [products, setProducts] = (0, import_react.useState)([]);
  const [loading, setLoading] = (0, import_react.useState)(true);
  const isFirstLoadRef = (0, import_react.useRef)(true);
  const isFirstLoadDataRef = (0, import_react.useRef)(true);
  const context = (0, import_hooks.useEngineContext)();
  const { getApp } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const app = getApp();
  const getProducts = (0, import_ahooks.useMemoizedFn)(async (params) => {
    var _a2, _b, _c, _d, _e;
    if (isFirstLoadRef.current && osKey) {
      const cachedProducts = (0, import_productCache.getProductCache)(osKey);
      if (cachedProducts && cachedProducts.length > 0) {
        setProducts(cachedProducts);
        setLoading(false);
      }
      isFirstLoadRef.current = false;
    } else {
      setLoading(true);
    }
    const maxRetries = 3;
    let lastError = null;
    let freshProducts = [];
    for (let attempt = 1; attempt <= maxRetries; attempt++) {
      try {
        console.log(`[ProductCache] 尝试加载数据，第 ${attempt}/${maxRetries} 次`);
        freshProducts = await bookingTicket.loadProducts(params);
        if (freshProducts && freshProducts.length > 0) {
          console.log(`[ProductCache] 第 ${attempt} 次尝试成功，获取到 ${freshProducts.length} 条数据`);
          break;
        }
        console.log(`[ProductCache] 第 ${attempt} 次尝试返回空数据`);
        if (attempt < maxRetries) {
          console.log(`[ProductCache] 等待 1 秒后重试...`);
          await sleep(1e3);
        }
      } catch (error) {
        lastError = error;
        console.log(`[ProductCache] 第 ${attempt} 次尝试失败:`, error);
        (_b = (_a2 = app == null ? void 0 : app.logger) == null ? void 0 : _a2.addLog) == null ? void 0 : _b.call(_a2, {
          type: "info",
          title: `[getProduct] bookingTicket.loadProducts error (attempt ${attempt}/${maxRetries})`,
          metadata: {
            error,
            attempt
          }
        });
        if (attempt < maxRetries) {
          console.log(`[ProductCache] 等待 1 秒后重试...`);
          await sleep(1e3);
        }
      }
    }
    if (freshProducts && freshProducts.length > 0) {
      isFirstLoadDataRef.current = false;
      setProducts(freshProducts);
      if (osKey) {
        (0, import_productCache.setProductCache)(osKey, freshProducts);
        console.log("[ProductCache] 已更新缓存");
      }
      setLoading(false);
    } else {
      console.log("[ProductCache] 3次尝试后仍未获取到数据");
      (_d = (_c = app == null ? void 0 : app.logger) == null ? void 0 : _c.addLog) == null ? void 0 : _d.call(_c, {
        type: "error",
        title: "[getProduct] 3次尝试后仍未获取到数据",
        metadata: {
          error: lastError
        }
      });
      if ((lastError == null ? void 0 : lastError.code) === 607100 && (isFirstLoadDataRef.current || products.length > 0)) {
        (_e = notAvailableRef.current) == null ? void 0 : _e.open();
      }
      setLoading(false);
      setProducts([]);
      isFirstLoadDataRef.current = false;
    }
  });
  return {
    getProducts,
    products,
    loading
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useProducts
});
