//#region src/components/list/utils/index.ts
/**
* 根据层级路径数组递归查找对应节点的 productIds
* @param nodes 当前层级的节点列表
* @param levelPath 层级路径数组，每个元素是对应层级的分类 ID
* @param currentLevel 当前遍历的层级索引
* @returns 找到的节点的 productIds，找不到返回空数组
* 
* @description
* 特殊逻辑：如果 levelPath 末尾有连续的 0，则去掉所有末尾的 0
* 例如：
* - [1, 2, 0] -> 取 id=2 节点的 productIds
* - [1, 0, 0] -> 取 id=1 节点的 productIds
* - [3, 0, 0, 0] -> 取 id=3 节点的 productIds
*/
const findProductIdsByPath = (nodes, levelPath, currentLevel = 0) => {
	let effectivePath = [...levelPath];
	while (effectivePath.length > 0 && (effectivePath[effectivePath.length - 1] === 0 || effectivePath[effectivePath.length - 1] === "0")) effectivePath.pop();
	if (!nodes || nodes.length === 0 || currentLevel >= effectivePath.length) return [];
	const targetId = effectivePath[currentLevel];
	const targetNode = nodes.find((node) => String(node.id) === String(targetId) || String(node.key) === String(targetId));
	if (!targetNode) return [];
	if (currentLevel === effectivePath.length - 1) return targetNode.productIds || [];
	if (targetNode.children && targetNode.children.length > 0) return findProductIdsByPath(targetNode.children, effectivePath, currentLevel + 1);
	return targetNode.productIds || [];
};
//#endregion
export { findProductIdsByPath };
