import { useEffect } from "react"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import { Checkbox } from "@/components/ui/checkbox"; import { useQuery } from "@tanstack/react-query"; import { taxonomyService } from "@/services/taxonomy.service"; import { lmsService } from "@/services/lms.service"; interface TaxonomyCascadeProps { subjectId: string; onSubjectChange: (id: string) => void; domainId: string; onDomainChange: (id: string) => void; topicIds: number[]; onTopicIdsChange: (ids: number[]) => void; objectiveIds: number[]; onObjectiveIdsChange: (ids: number[]) => void; } export function TaxonomyCascade({ subjectId, onSubjectChange, domainId, onDomainChange, topicIds, onTopicIdsChange, objectiveIds, onObjectiveIdsChange, }: TaxonomyCascadeProps) { const { data: subjects } = useQuery({ queryKey: ["taxonomy", "subjects"], queryFn: () => taxonomyService.listSubjects(), }); const numericSubjectId = subjectId !== "none" ? Number(subjectId) : undefined; const numericDomainId = domainId !== "none" ? Number(domainId) : undefined; const { data: domains } = useQuery({ queryKey: ["taxonomy", "domains", numericSubjectId], queryFn: () => taxonomyService.listDomains({ subject_id: numericSubjectId }), enabled: !!numericSubjectId, }); const { data: topics } = useQuery({ queryKey: ["taxonomy", "topics", numericDomainId], queryFn: () => taxonomyService.listTopics({ domain_id: numericDomainId }), enabled: !!numericDomainId, }); const { data: objectivesData } = useQuery({ queryKey: ["learning-objectives", topicIds], queryFn: () => lmsService.listLearningObjectives({ topic_ids: topicIds.join(",") }), enabled: topicIds.length > 0, }); const objectives = objectivesData?.items ?? []; useEffect(() => { if (subjectId === "none") { if (domainId !== "none") onDomainChange("none"); if (topicIds.length > 0) onTopicIdsChange([]); if (objectiveIds.length > 0) onObjectiveIdsChange([]); } }, [subjectId]); useEffect(() => { if (domainId === "none") { if (topicIds.length > 0) onTopicIdsChange([]); if (objectiveIds.length > 0) onObjectiveIdsChange([]); } }, [domainId]); const toggleTopic = (id: number) => { const next = topicIds.includes(id) ? topicIds.filter(t => t !== id) : [...topicIds, id]; onTopicIdsChange(next); if (next.length === 0 && objectiveIds.length > 0) onObjectiveIdsChange([]); }; const toggleObjective = (id: number) => { onObjectiveIdsChange( objectiveIds.includes(id) ? objectiveIds.filter(o => o !== id) : [...objectiveIds, id], ); }; return (