'use client'

import { IBaseResponse, CustomUseQueryOptions } from '@/@core/types/api'
import { useCustomQuery } from '@/hooks/api'
import { IProduct } from '@/types/data/simpleProduct/product.interface'
import { request } from '@/utils'

export const PRODUCTS_LIST_FEATURED_QUERY_KEY = 'get-products-featured-list-no-pagination'

export const useGetFeaturedProducts = (
  query?: Record<string, any>,
  options?: CustomUseQueryOptions<IBaseResponse<IProduct[]>>
) => {
  const queryKey = [PRODUCTS_LIST_FEATURED_QUERY_KEY, query]

  const productsList = useCustomQuery<IBaseResponse<IProduct[]>>(
    queryKey,
    () => {
      const params: Record<string, string> = {}

      if (query) {
        Object.entries(query).forEach(([key, value]) => {
          if (value !== undefined && value !== null && value !== '') {
            params[key] = String(value)
          }
        })
      }

      return request({ url: '/products/featured/list', params })
    },
    options
  )

  return productsList
}
