type PrismaCondition = Record<string, any>;
type SafeOROptions = {
	/**
	 * 当所有条件为空时的处理方式
	 * @default 'return-undefined' 返回 undefined (等效查询所有)
	 * 'throw-error' 抛出错误
	 * 'return-empty' 返回空对象
	 */
	emptyBehavior?: "return-undefined" | "throw-error" | "return-empty";
};

/**
 * 安全合并多个查询条件为 OR 关系
 * @param conditions 多个查询条件
 * @param options 配置选项
 * @returns 安全的 Prisma WHERE 条件
 */
export const safeOR = (
	conditions: PrismaCondition[],
	options?: SafeOROptions
): PrismaCondition | undefined => {
	const { emptyBehavior = "return-undefined" } = options || {};

	// 过滤空条件和无效值
	const validConditions = conditions.filter(
		(cond) => cond && Object.keys(cond).length > 0
	);

	// 处理全空情况
	if (validConditions.length === 0) {
		switch (emptyBehavior) {
			case "throw-error":
				throw new Error("No valid conditions provided to OR query");
			case "return-empty":
				return {};
			case "return-undefined":
			default:
				return undefined;
		}
	}

	// 优化单条件查询
	return validConditions.length === 1
		? validConditions[0]
		: { OR: validConditions };
};
