import { API_URL } from '../../config'
import { FiltersType, URLEncodeFilters } from '../../utils/filters'
import { Announcement } from '../announcement/types'
import { AnnouncementsResponse } from './types'

const initialAnnouncements: Announcement[] = []

const composeAnnouncementsURL = (filters: FiltersType) =>
    API_URL + '/announcements?' + new URLSearchParams(URLEncodeFilters(filters)).toString()

const processAnnouncements = (data: AnnouncementsResponse): Announcement[] => {
    const annList = data.list_of_announcements

    return annList.map(ann => ({
        ...ann,
        lat: ann.latitude,
        lng: ann.longtitude,
        bestBy: ann.best_by,
        bookedBy: ann.booked_by,
        userId: ann.user_id
    }))
}

export { initialAnnouncements, composeAnnouncementsURL, processAnnouncements }
