import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import withResponsive from "../../../../responsive/withResponsive.js";
import ProductCard from "../../../../plus/productSelect/ProductCard/index.js";
import index from "../../../../plus/productSelect/index.js";
import List_default from "../../../list/List.js";
import { generateProductTabs, sortProductsByTabOrder } from "./utils.js";
import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import "./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 = useRef(null);
	const [keyword, setKeyword] = useState("");
	const [activeTab, setActiveTab] = useState(void 0);
	const onActiveTabChange = useCallback((tab) => {
		setActiveTab(tab);
	}, []);
	const renderProductSelect = 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.createElement(index, {
			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 = 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
	]);
	useImperativeHandle(ref, () => ({ getTabbarRef: () => {
		var _listRef$current;
		return (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.getTabbarRef();
	} }));
	const tabData = 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 ? generateProductTabs(dataSource, tabStyle, isTreeTab) : void 0;
	}, [dataSource]);
	const productConfig = 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 = 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 _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)) sortProductsByTabOrder(filteredData, tabData);
		let emptyText = emptyDescription;
		if (Array.isArray(activeTab) && activeTab.length === 0 && keyword === "") emptyText = locales.getText("pisell2.ticket-booking.input-search-to-show-data");
		return /* @__PURE__ */ React.createElement("div", {
			className: `sku-list-container ${className || ""}`,
			style
		}, /* @__PURE__ */ React.createElement(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.createElement(ProductCard, {
				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 = forwardRef(ProductDisplayAdapter);
ForwardedProductDisplayAdapter.displayName = `ProductDisplayAdapter`;
var ProductDisplayAdapter_default = withResponsive(ForwardedProductDisplayAdapter);
//#endregion
export { ProductDisplayAdapter_default as default };
