import apiEndpoints from '../apiEndpoints';
import { UserData, UpdateResponse } from '../../utils/interface/userInterface';

export const updateUser = async (
  userData: UserData,
  _id: string
): Promise<UpdateResponse> => {
  const response = await fetch(
    `http://localhost:3000${apiEndpoints.userUpdate}`,
    {
      method: 'PATCH',
      body: JSON.stringify({ userData, _id }),
      headers: {
        'Content-Type': 'application/json',
      },
    }
  );

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

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

  return data;
};
