import CourseList from "@web/src/components/models/course/list/CourseList";
import { useMainContext } from "../../layout/MainProvider";
import { PostType, Prisma } from "@nice/common";
import { useMemo } from "react";

export function CoursesContainer() {
	const { selectedTerms, searchCondition } = useMainContext();
	const termFilters = useMemo(() => {
		return Object.entries(selectedTerms)
			.filter(([, terms]) => terms.length > 0)
			.map(([, terms]) => terms);
	}, [selectedTerms]);

	return (
		<>
			<CourseList
				params={{
					pageSize: 12,
					where: {
						type: PostType.COURSE,
						AND: termFilters.map((termFilter) => ({
							terms: {
								some: {
									id: {
										in: termFilter, // 确保至少有一个 term.id 在当前 termFilter 中
									},
								},
							},
						})),
						...searchCondition,
					},
				}}
				cols={4}></CourseList>
		</>
	);
}

export default CoursesContainer;
