require("../../../../_virtual/_rolldown/runtime.js");
let _pisell_utils = require("@pisell/utils");
//#region src/components/ticketBooking/components/ProductDisplayAdapter/utils.ts
/**
* 格式化 SKU 数据源，适配 SkuCard 组件
*/
const formatSkuDataSource = (dataSource) => {
	return {
		id: dataSource.id,
		title: dataSource.title,
		subtitle: dataSource.subtitle,
		cover: dataSource.cover,
		price: dataSource.price,
		original_price: dataSource.original_price,
		stock: dataSource.stock_quantity,
		sortKeys: [
			"title",
			"subtitle",
			"price"
		]
	};
};
/**
* 根据产品数据生成分类 Tabs（Switch 模式）
* 参考 TestResponsiveWrapper 的 generateSimpleTabItems 函数
* 
* @param products 产品列表
* @param tabStyle tab 样式类型
* @returns Switch 模式返回 CategoryTreeNode[]，Anchor 模式返回 TabItem[]
*/
const generateProductTabs = (products, tabStyle = "switch") => {
	if (!products || products.length === 0) {
		if (tabStyle === "switch") return [];
		return [{
			key: "all",
			label: _pisell_utils.locales.getText("pisell2.ticket-booking.all")
		}];
	}
	if (tabStyle === "switch") return buildCategoryTree(products);
	else {
		const categoryCount = products.reduce((acc, product) => {
			if (product.category && Array.isArray(product.category)) product.category.forEach((cat) => {
				const key = String(cat.id);
				if (!acc[key]) acc[key] = {
					count: 0,
					category: cat
				};
				acc[key].count += 1;
			});
			return acc;
		}, {});
		const tabs = [{
			key: "all",
			label: `${_pisell_utils.locales.getText("pisell2.ticket-booking.all")} (${products.length})`
		}];
		Object.entries(categoryCount).sort(([, a], [, b]) => (b.category.sort || 0) - (a.category.sort || 0)).forEach(([key, { count, category }]) => {
			tabs.push({
				key,
				label: `${category.name} (${count})`,
				group: key
			});
		});
		return tabs;
	}
};
/**
* 构建分类树形结构（Switch 模式专用）
* 
* @param products 产品列表
* @returns CategoryTreeNode[] 树形分类结构
*/
const buildCategoryTree = (products) => {
	const categoryMap = /* @__PURE__ */ new Map();
	const categoryProductMap = /* @__PURE__ */ new Map();
	products.forEach((product) => {
		if (product.category && Array.isArray(product.category)) product.category.forEach((cat) => {
			if (!categoryMap.has(cat.id)) categoryMap.set(cat.id, cat);
			if (!categoryProductMap.has(cat.id)) categoryProductMap.set(cat.id, /* @__PURE__ */ new Set());
			categoryProductMap.get(cat.id).add(product.id);
		});
	});
	const childrenMap = /* @__PURE__ */ new Map();
	const rootCategories = [];
	categoryMap.forEach((category, id) => {
		const parentId = category.parent_id;
		if (!parentId || parentId === 0) rootCategories.push(id);
		else {
			if (!childrenMap.has(parentId)) childrenMap.set(parentId, []);
			childrenMap.get(parentId).push(id);
		}
	});
	const sortBySort = (ids) => {
		return ids.sort((a, b) => {
			const catA = categoryMap.get(a);
			const catB = categoryMap.get(b);
			return ((catB === null || catB === void 0 ? void 0 : catB.sort) || 0) - ((catA === null || catA === void 0 ? void 0 : catA.sort) || 0);
		});
	};
	/**
	* 递归构建分类节点
	* 
	* @param categoryId 分类 ID
	* @param parentPath 父级路径（用于生成 pid）
	* @returns CategoryTreeNode 分类节点
	*/
	const buildNode = (categoryId, parentPath) => {
		const category = categoryMap.get(categoryId);
		const sortedChildIds = sortBySort([...childrenMap.get(categoryId) || []]);
		const directProductIds = Array.from(categoryProductMap.get(categoryId) || []);
		const currentPath = parentPath ? `${parentPath}_${categoryId}` : String(categoryId);
		const pid = parentPath || void 0;
		const node = {
			id: categoryId,
			key: categoryId,
			group: categoryId,
			label: category.name,
			name: category.name,
			icon: category.icon,
			productIds: directProductIds,
			childrenMaxLevel: 0
		};
		if (pid) node.pid = pid;
		if (sortedChildIds.length > 0) {
			const childNodes = sortedChildIds.map((childId) => buildNode(childId, currentPath));
			const allChildProductIds = childNodes.reduce((acc, child) => {
				return acc.concat(child.productIds);
			}, []);
			node.productIds = Array.from(new Set([...directProductIds, ...allChildProductIds]));
			node.children = [...childNodes];
			let maxChildLevel = 0;
			for (let i = 0; i < childNodes.length; i++) if (childNodes[i].childrenMaxLevel > maxChildLevel) maxChildLevel = childNodes[i].childrenMaxLevel;
			node.childrenMaxLevel = maxChildLevel + 1;
		}
		return node;
	};
	const rootNodes = sortBySort([...rootCategories]).map((rootId) => buildNode(rootId, ""));
	if (rootNodes.length > 0) {
		Array.from(new Set(rootNodes.flatMap((node) => node.productIds)));
		return [...rootNodes];
	}
	return rootNodes;
};
//#endregion
exports.formatSkuDataSource = formatSkuDataSource;
exports.generateProductTabs = generateProductTabs;
