import { useState, useMemo } from 'react'
import type { FilterValues } from '@/components/Filters'
import type { GetFoodsParams } from '../types/Types'

const DEFAULT_PAGE = 1
const DEFAULT_LIMIT = 10

export const useFoodFilters = () => {
    const [filters, setFilters] = useState<FilterValues>({})
    const [currentPage, setCurrentPage] = useState(DEFAULT_PAGE)
    const [limit] = useState(DEFAULT_LIMIT)

    const apiParams = useMemo<GetFoodsParams>(() => {
        const params: GetFoodsParams = {
            page: currentPage,
            limit: limit,
        }

        if (filters.search) {
            params.search = filters.search
        }

        return params
    }, [filters, currentPage, limit])

    const handleFiltersChange = (newFilters: FilterValues) => {
        setFilters(newFilters)
        setCurrentPage(DEFAULT_PAGE)
    }

    const handlePageChange = (page: number) => {
        setCurrentPage(page)
    }

    return {
        filters,
        currentPage,
        apiParams,
        handleFiltersChange,
        handlePageChange,
        limit,
    }
}

