import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import ProductCard from "../../../../plus/productSelect/ProductCard/index.js";
import List_default from "../../../list/List.js";
import { useBigSaleProductListConfig } from "../../hooks/useBigSaleProductListConfig.js";
import locales_default from "../../locales/index.js";
import React, { useEffect, useState } from "react";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/bigSale/components/ProductList/index.tsx
/**
* 商品列表只做“加载商品 + 触发加购”，加购决策交给 salesSdk。
*/
const BigSaleProductList = ({ dataSource, productLoadParams, productDisplayConfig, variant }) => {
	var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3;
	const context = useEngineContext();
	const { sales, products, cart } = dataSource;
	const { load } = products;
	const [keyword, setKeyword] = useState("");
	const currentProducts = products.products;
	const productListConfig = useBigSaleProductListConfig({
		products: currentProducts,
		productDisplayConfig,
		variant,
		keyword
	});
	locales.init(locales_default, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || "en");
	useEffect(() => {
		if (!sales.isReady) return;
		load(productLoadParams).catch((error) => {
			console.error(error);
		});
	}, [
		sales.isReady,
		load,
		productLoadParams
	]);
	const handleSelectProduct = function() {
		var _ref = _asyncToGenerator(function* (product) {
			try {
				const result = yield cart.addProductWithFlow(product, { quantity: 1 });
				if (result.status === "requiresDetail" || result.status === "requiresBookingEdit") console.error(locales.getText("pisell2.big-sale.missing-hosts"));
			} catch (error) {
				console.error(error);
			}
		});
		return function handleSelectProduct(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-big-sale-product-list-wrapper" }, /* @__PURE__ */ React.createElement(List_default, _objectSpread2(_objectSpread2({
		className: "pisell-big-sale-product-list",
		data: productListConfig.data,
		onTabSearch: setKeyword,
		loading: products.loading
	}, productListConfig.listProps), {}, {
		emptyConfig: {
			show: !products.loading && productListConfig.data.length === 0,
			text: locales.getText("pisell2.big-sale.no-products")
		},
		renderItem: (item, index) => /* @__PURE__ */ React.createElement(ProductCard, {
			key: item.id,
			index,
			item,
			onSelectCover: () => handleSelectProduct(item),
			onSelectInfo: () => handleSelectProduct(item),
			isSelected: false,
			scenario: "booking2",
			customSkuCardAllConfig: productListConfig.skuCardAllConfig
		})
	})));
};
//#endregion
export { BigSaleProductList };
