const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_withResponsive = require("../../../../responsive/withResponsive.js");
const require_index = require("../../../../plus/productSelect/ProductCard/index.js");
const require_index$1 = require("../../../../plus/productSelect/index.js");
const require_List = require("../../../list/List.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/ticketBooking/components/ProductDisplayAdapter/ProductDisplayAdapter.tsx
/**
* 产品展示适配器组件
* 统一管理 ProductSelect 和 SkuList 两种展示模式
*/
const ProductDisplayAdapter = (props, ref) => {
	const { config, dataSource, loading = false, currentProductId, onSelectProduct, emptyDescription, style, className } = props;
	const { displayMode = "product-select" } = config;
	const listRef = (0, react.useRef)(null);
	const [keyword, setKeyword] = (0, react.useState)("");
	const [activeTab, setActiveTab] = (0, react.useState)(void 0);
	const onActiveTabChange = (0, react.useCallback)((tab) => {
		setActiveTab(tab);
	}, []);
	const renderProductSelect = (0, react.useMemo)(() => {
		const { showSearch = true, showTab = true, compact = false, layout = {
			grid: true,
			config: "12:12"
		}, productListSetting = {
			isProductCover: 1,
			isTitle: 1,
			isSubtitle: 1,
			isPrice: 1,
			isOriginPrice: 0,
			isStock: 0
		} } = config.productSelectConfig || {};
		console.log("renderProductSelect");
		return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			tabSwitchMode: "switch",
			dataSource,
			layout,
			onChange: onSelectProduct,
			value: currentProductId,
			loading,
			productListSetting,
			showSearch,
			showTab,
			emptyDescription,
			style,
			compact,
			scenario: "booking2"
		});
	}, [
		config.productSelectConfig,
		dataSource,
		currentProductId,
		onSelectProduct,
		loading,
		emptyDescription,
		style
	]);
	const _filteredList = (0, react.useMemo)(() => {
		if (Array.isArray(activeTab) && activeTab.length === 0 && !keyword) return [];
		if (!keyword) return dataSource;
		return dataSource.filter((product) => {
			if (typeof (product === null || product === void 0 ? void 0 : product.title) !== "string") return false;
			return product.title.toLowerCase().indexOf(keyword.toLowerCase()) !== -1;
		});
	}, [
		keyword,
		dataSource,
		activeTab
	]);
	(0, react.useImperativeHandle)(ref, () => ({ getTabbarRef: () => {
		var _listRef$current;
		return (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.getTabbarRef();
	} }));
	const tabData = (0, react.useMemo)(() => {
		var _listConfig$isTreeTab;
		const { listConfig = {} } = config.skuListConfig || {};
		const enableTab = listConfig.showTab || false;
		const tabStyle = listConfig.tabStyle || "switch";
		const isTreeTab = (_listConfig$isTreeTab = listConfig.isTreeTab) !== null && _listConfig$isTreeTab !== void 0 ? _listConfig$isTreeTab : true;
		return enableTab ? require_utils.generateProductTabs(dataSource, tabStyle, isTreeTab) : void 0;
	}, [dataSource]);
	const productConfig = (0, react.useMemo)(() => {
		return {
			isProductCover: 1,
			isTitle: 1,
			isSubtitle: 0,
			isPrice: 1,
			isOriginPrice: 1,
			isStock: 0,
			showZeroPrice: true,
			layoutDirection: "row",
			imgHeight: "126px",
			imgWidth: "100%",
			rightStyle: {
				minHeight: "auto",
				justifyContent: "space-between"
			}
		};
	}, []);
	const renderSkuList = (0, react.useMemo)(() => {
		var _listConfig$columnGap, _listConfig$rowGap, _listConfig$isTreeTab2;
		const { listConfig = {}, skuCardAllConfig = {} } = config.skuListConfig || {};
		const displayStyle = listConfig.layout || "grid";
		const columns = listConfig.columns || 2;
		const columnGap = (_listConfig$columnGap = listConfig.columnGap) !== null && _listConfig$columnGap !== void 0 ? _listConfig$columnGap : 12;
		const rowGap = (_listConfig$rowGap = listConfig.rowGap) !== null && _listConfig$rowGap !== void 0 ? _listConfig$rowGap : 12;
		const skeletonConfig = listConfig.skeletonConfig;
		const paginationType = listConfig.showPagination ? "pager" : "all";
		const enableTab = listConfig.showTab || false;
		const tabStyle = listConfig.tabStyle || "switch";
		const isTreeTab = (_listConfig$isTreeTab2 = listConfig.isTreeTab) !== null && _listConfig$isTreeTab2 !== void 0 ? _listConfig$isTreeTab2 : true;
		const stickyTop = listConfig.stickyTop !== void 0 ? listConfig.stickyTop : true;
		const filteredData = _filteredList.map((item) => {
			var _item$category, _item$category$map;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { category: ((_item$category = item.category) === null || _item$category === void 0 || (_item$category$map = _item$category.map) === null || _item$category$map === void 0 ? void 0 : _item$category$map.call(_item$category, (category) => `${category.id}`)) || [] });
		});
		if (!isTreeTab && tabData && Array.isArray(tabData)) require_utils.sortProductsByTabOrder(filteredData, tabData);
		let emptyText = emptyDescription;
		if (Array.isArray(activeTab) && activeTab.length === 0 && keyword === "") emptyText = _pisell_utils.locales.getText("pisell2.ticket-booking.input-search-to-show-data");
		return /* @__PURE__ */ react.default.createElement("div", {
			className: `sku-list-container ${className || ""}`,
			style
		}, /* @__PURE__ */ react.default.createElement(require_List.default, {
			ref: listRef,
			className: "sku-list",
			style: {
				maxHeight: "100%",
				overflow: "auto"
			},
			data: filteredData,
			renderSearch: () => null,
			loading,
			onTabSearch: setKeyword,
			displayStyle,
			columns,
			columnGap,
			rowGap,
			paginationType,
			layoutDirection: "vertical",
			tabStyle: enableTab ? tabStyle : "none",
			isTreeTab,
			tabData,
			onActiveTabChange,
			tabGroup: "category",
			stickyTop,
			emptyConfig: {
				show: !loading && filteredData.length === 0,
				text: emptyText
			},
			skeletonConfig,
			renderItem: (item, index) => /* @__PURE__ */ react.default.createElement(require_index.default, {
				key: item.id,
				index,
				item,
				onSelectCover: () => onSelectProduct(item),
				onSelectInfo: () => onSelectProduct(item),
				isSelected: item.id === currentProductId,
				scenario: "booking2",
				customSkuCardAllConfig: skuCardAllConfig
			})
		}));
	}, [
		config.skuListConfig,
		dataSource,
		loading,
		currentProductId,
		onSelectProduct,
		emptyDescription,
		style,
		className,
		_filteredList,
		tabData,
		productConfig,
		activeTab,
		keyword
	]);
	if (displayMode === "sku-list") return renderSkuList;
	return renderProductSelect;
};
const ForwardedProductDisplayAdapter = (0, react.forwardRef)(ProductDisplayAdapter);
ForwardedProductDisplayAdapter.displayName = `ProductDisplayAdapter`;
var ProductDisplayAdapter_default = require_withResponsive.default(ForwardedProductDisplayAdapter);
//#endregion
exports.default = ProductDisplayAdapter_default;
