"use client" import { useState, useEffect } from "react" import { Lightbulb, BookOpen, Bot, Users, TrendingUp } from "lucide-react" import heroFallback from "./hero-fallback.json" // JSON file is empty by default, so assert the expected shape to avoid `never` const fallbackData: ApiCard[] = heroFallback as ApiCard[] // Icons mapping to match the order of API response const ICONS = [Lightbulb, BookOpen, Bot, Users, TrendingUp] type ApiCard = { cardtitle?: string carddescription?: string svg_icon?: string | null } type ApiResponse = { heading?: string description?: string cards?: ApiCard[] } export default function HeroSection() { const [features, setFeatures] = useState<{ icon?: any; svg?: string; title: string; description: string }[]>([]) const [heading, setHeading] = useState("Why SFS") const [description, setDescription] = useState("We combine expertise in education with cutting-edge technology to deliver solutions that truly make a difference.") useEffect(() => { const fetchData = async () => { try { const response = await fetch("/api/whysfs") if (!response.ok) throw new Error("API failed") const json: ApiResponse = await response.json() if (json.heading) setHeading(json.heading) if (json.description) setDescription(json.description) const mappedFeatures = (json.cards ?? []).map((item: ApiCard, index: number) => ({ svg: item.svg_icon || undefined, icon: ICONS[index % ICONS.length], // fallback icon if svg missing title: item.cardtitle || "Feature", description: item.carddescription || "", })) // Swap positions: move 5th to 2nd and 2nd to 5th when available if (mappedFeatures.length >= 5) { const temp = mappedFeatures[1] mappedFeatures[1] = mappedFeatures[4] mappedFeatures[4] = temp } if (mappedFeatures.length === 0) { throw new Error("No data found") } setFeatures(mappedFeatures) } catch (error) { console.warn("Failed to fetch Why SFS content, using fallback:", error) // Use Fallback const mappedFeatures = fallbackData.map((item, index) => ({ icon: ICONS[index % ICONS.length], title: item.cardtitle || "Feature", description: item.carddescription || "", })) if (mappedFeatures.length >= 5) { const temp = mappedFeatures[1] mappedFeatures[1] = mappedFeatures[4] mappedFeatures[4] = temp } setFeatures(mappedFeatures) } } fetchData() }, []) if (features.length === 0) { return (

{heading}

{/* Loading state placeholder */}
) } return (
{/* Hero Content */}

{heading}

{description}

{/* Features Grid */}
{features.map((feature, index) => { return (
{feature.svg ? ( ) : ( feature.icon && ( ) )}

{feature.title}

{feature.description}

) })}
) }