import useFetch from './useFetch'
import { API_URL } from '../../config'
import { removeNull } from '../../utils'

const initialAnnouncements = { list_of_announcements: [], Success: true }

const useHomeAnnouncementList = (filters) => {
    const { data, loading, error } = useFetch(
        API_URL + '/announcements?' + new URLSearchParams(removeNull(filters)),
        null,
        initialAnnouncements
    )

    const annList = data.list_of_announcements

    const res = annList.map(ann => ({
        ...ann,
        lat: ann.latitude,
        lng: ann.longtitude,
        bestBy: ann.best_by
    }))

    return { data: error ? [] : res, loading, error }
}

export default useHomeAnnouncementList
