import apiEndpoints from '../apiEndpoints';
import {
  OrderResponsePost,
  OrderData,
} from '../../utils/interface/orderInterface';

export const postOrder = async (
  orderData: OrderData
): Promise<OrderResponsePost> => {
  const response = await fetch(`http://localhost:3000${apiEndpoints.order}`, {
    method: 'POST',
    body: JSON.stringify(orderData),
    headers: {
      'Content-Type': 'application/json',
    },
  });

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

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

  return data;
};
