import { useSearchParams } from "next/navigation"
import { useEffect } from "react"

import { useHotelFilterStore } from "@/stores/hotel-filters"

export default function useInitializeFiltersFromUrl() {
  const searchParams = useSearchParams()
  const setFilters = useHotelFilterStore((state) => state.setFilters)

  useEffect(() => {
    const filtersFromUrl = searchParams.get("filters")
    if (filtersFromUrl) {
      setFilters(filtersFromUrl.split(","))
    } else {
      setFilters([])
    }
  }, [searchParams, setFilters])
}
