import { ProductsResponse } from '../../utils/interface/productInterface';
import apiEndpoints from '../apiEndpoints';

export const getAllProducts = async (
  pageIndex: number,
  category = 'All',
  filter = 'asc'
): Promise<ProductsResponse> => {
  const response = await fetch(
    `http://localhost:3000${apiEndpoints.products}?pageIndex=${pageIndex}&category=${category}&filterType=${filter}`
  );

  const data: ProductsResponse = await response.json();

  if (!response.ok) {
    throw new Error(data.message || 'Something went wrong!');
  }

  return data;
};
