import React, { useEffect, useState } from 'react'; interface USCertificationSelectorProps { type: string; certification?: string; onChange: (params: { certificationCountry?: string; certification?: string; }) => void; } const US_MOVIE_CERTIFICATIONS = ['NR', 'G', 'PG', 'PG-13', 'R', 'NC-17']; const US_TV_CERTIFICATIONS = [ 'NR', 'TV-Y', 'TV-Y7', 'TV-G', 'TV-PG', 'TV-14', 'TV-MA', ]; const USCertificationSelector: React.FC = ({ type, certification, onChange, }) => { const [selectedRatings, setSelectedRatings] = useState(() => certification ? certification.split('|') : [] ); const certifications = type === 'movie' ? US_MOVIE_CERTIFICATIONS : US_TV_CERTIFICATIONS; useEffect(() => { if (certification) { setSelectedRatings(certification.split('|')); } else { setSelectedRatings([]); } }, [certification]); const toggleRating = (rating: string) => { setSelectedRatings((prevSelected) => { let newSelected; if (prevSelected.includes(rating)) { newSelected = prevSelected.filter((r) => r !== rating); } else { newSelected = [...prevSelected, rating]; } const newCertification = newSelected.length > 0 ? newSelected.join('|') : undefined; onChange({ certificationCountry: 'US', certification: newCertification, }); return newSelected; }); }; return (
{certifications.map((rating) => ( ))}
); }; export default USCertificationSelector;