import { useEffect, useState } from 'react'
import type { CuratedCatalog } from '../types'

export function useCatalog() {
  const [catalog, setCatalog] = useState<CuratedCatalog | null>(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    fetch('/curated.json')
      .then(r => {
        if (!r.ok) throw new Error(`Failed to load curated.json (${r.status})`)
        return r.json() as Promise<CuratedCatalog>
      })
      .then(data => setCatalog(data))
      .catch(err => setError(err.message))
      .finally(() => setLoading(false))
  }, [])

  return { catalog, loading, error }
}
