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/bigSale/components/ProductList/index.tsx
var ProductList_exports = {};
__export(ProductList_exports, {
  BigSaleProductList: () => BigSaleProductList
});
module.exports = __toCommonJS(ProductList_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_List = __toESM(require("../../../list/List"));
var import_ProductCard = require("../../../../plus/productSelect/ProductCard");
var import_locales = __toESM(require("../../locales"));
var import_hooks = require("../../hooks");
var import_index = require("./index.less");
var BigSaleProductList = ({
  isActive = true,
  context,
  productLoadParams,
  productDisplayConfig,
  productCatalogSessionKey,
  variant
}) => {
  var _a, _b, _c, _d, _e;
  const toast = (0, import_materials.useToast)();
  const engineContext = (0, import_useEngineContext.default)();
  const { sales, products, cart } = context;
  const { load } = products;
  const [keyword, setKeyword] = (0, import_react.useState)("");
  const listRef = (0, import_react.useRef)(
    null
  );
  const loadedKeyRef = (0, import_react.useRef)(null);
  const loadInFlightRef = (0, import_react.useRef)(false);
  const currentProducts = products.products;
  const productListConfig = (0, import_hooks.useBigSaleProductListConfig)({
    products: currentProducts,
    productDisplayConfig,
    variant,
    keyword
  });
  console.log(currentProducts, "currentProducts12345");
  import_utils.locales.init(
    import_locales.default,
    ((_e = (_d = (_c = (_b = (_a = engineContext.appHelper.utils) == null ? void 0 : _a.getApp) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.locales) == null ? void 0 : _d.getLocale) == null ? void 0 : _e.call(_d)) || "en"
  );
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c2, _d2;
    if (isActive) {
      return;
    }
    (_d2 = (_c2 = (_b2 = (_a2 = listRef.current) == null ? void 0 : _a2.getTabbarRef) == null ? void 0 : _b2.call(_a2)) == null ? void 0 : _c2.closeSearchMode) == null ? void 0 : _d2.call(_c2);
    setKeyword("");
  }, [isActive]);
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c2, _d2, _e2;
    if (!sales.bookingTicket) {
      return;
    }
    const catalogCount = ((_d2 = (_c2 = (_b2 = (_a2 = sales.bookingTicket).getProductCatalog) == null ? void 0 : _b2.call(_a2)) == null ? void 0 : _c2.products) == null ? void 0 : _d2.length) ?? 0;
    const hasCatalog = catalogCount > 0 || currentProducts.length > 0;
    if (!sales.isReady && !hasCatalog) {
      return;
    }
    if (typeof productCatalogSessionKey === "string" && /-p0$/.test(productCatalogSessionKey)) {
      return;
    }
    const loadKey = JSON.stringify({
      params: productLoadParams ?? {},
      sessionKey: productCatalogSessionKey ?? ""
    });
    const menuListIds = (productLoadParams == null ? void 0 : productLoadParams.menu_list_ids) ?? ((_e2 = sales.defaultProductLoadParams) == null ? void 0 : _e2.menu_list_ids);
    if (!hasCatalog && (!Array.isArray(menuListIds) || menuListIds.length === 0)) {
      return;
    }
    if (loadedKeyRef.current === loadKey) {
      return;
    }
    if (loadInFlightRef.current) {
      return;
    }
    loadInFlightRef.current = true;
    loadedKeyRef.current = loadKey;
    const useSilent = hasCatalog;
    load(productLoadParams, { silent: useSilent }).finally(() => {
      loadInFlightRef.current = false;
    }).catch((error) => {
      loadedKeyRef.current = null;
      console.error(error);
    });
  }, [
    sales.isReady,
    sales.status,
    sales.bookingTicket,
    load,
    productLoadParams,
    productCatalogSessionKey,
    currentProducts.length
  ]);
  const listShowsLoading = products.loading && currentProducts.length === 0;
  const handleSelectProduct = async (product) => {
    try {
      const result = await cart.addProductWithFlow(product, { quantity: 1 });
      if (result.status === "requiresDetail" || result.status === "requiresBookingEdit") {
        console.error(import_utils.locales.getText("pisell2.big-sale.missing-hosts"));
      }
    } catch (error) {
      console.error(error);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-big-sale-product-list-wrapper" }, /* @__PURE__ */ import_react.default.createElement(
    import_List.default,
    {
      ref: listRef,
      className: "pisell-big-sale-product-list",
      data: productListConfig.data,
      onTabSearch: setKeyword,
      loading: listShowsLoading,
      ...productListConfig.listProps,
      emptyConfig: {
        show: !products.loading && productListConfig.data.length === 0,
        text: import_utils.locales.getText("pisell2.big-sale.no-products")
      },
      renderItem: (item, index) => /* @__PURE__ */ import_react.default.createElement(
        import_ProductCard.ProductCard,
        {
          key: item.id,
          index,
          item,
          onSelectCover: () => handleSelectProduct(item),
          onSelectInfo: () => handleSelectProduct(item),
          isSelected: false,
          scenario: "booking2",
          customSkuCardAllConfig: productListConfig.skuCardAllConfig
        }
      )
    }
  ));
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BigSaleProductList
});
