"use client"; import { STATUS_MAP } from "@/config/constants"; import { useState, useEffect, useMemo } from 'react'; import Link from 'next/link'; function getDistance(lat1: number, lon1: number, lat2: number, lon2: number) { try { const R = 6371; const dLat = (lat2 - lat1) * Math.PI / 180; const dLon = (lon2 - lon1) * Math.PI / 180; const a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon/2) * Math.sin(dLon/2); return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } catch (e) { return Infinity; } } export default function FacilitySearch({ initialFacilities }: { initialFacilities: any[] }) { const [searchQuery, setSearchQuery] = useState(""); const [userLocation, setUserLocation] = useState<{ lat: number, lng: number } | null>(null); const [sortMethod, setSortMethod] = useState<'dist' | 'alpha'>('alpha'); useEffect(() => { if ("geolocation" in navigator) { navigator.geolocation.getCurrentPosition(p => { setUserLocation({ lat: p.coords.latitude, lng: p.coords.longitude }); setSortMethod('dist'); }); } }, []); const processed = useMemo(() => { if (!Array.isArray(initialFacilities)) return []; const words = searchQuery.toLowerCase().trim().split(/\s+/).filter(w => w.length > 0); return initialFacilities.map(f => { const dist = userLocation && f.lat && f.lng ? getDistance(userLocation.lat, userLocation.lng, f.lat, f.lng) : Infinity; const hasNSG = f.nsg_data && Object.keys(f.nsg_data).length > 0; const hasGolfamore = f.golfamore && (f.golfamore_data?.terms || f.golfamore === true); const blob = `${f.name} ${f.city} ${f.county} ${hasNSG ? 'nsg seniorgolf' : ''} ${hasGolfamore ? 'golfamore' : ''}`.toLowerCase(); const matches = words.every(w => blob.includes(w)); return { ...f, dist, hasNSG, hasGolfamore, matches }; }) .filter(f => f.matches) .sort((a, b) => { if (sortMethod === 'dist' && a.dist !== b.dist) return a.dist - b.dist; return a.name.localeCompare(b.name, 'nb'); }); }, [searchQuery, initialFacilities, userLocation, sortMethod]); return (
{f.city} • {f.county}