import axiosInstance from 'api/axios';
import { GET_ME, LOGIN, REGISTER, RESEND_OTP, VERIFY_OTP } from 'api/URL';
import {
  getMeData,
  loginPayload,
  otpPayload,
  registerPayload,
  resendPayload,
} from './type';

const auth = {
  async register(payload: registerPayload) {
    try {
      const data = await axiosInstance.post(REGISTER, payload);
      return data;
    } catch (error) {
      throw error;
    }
  },
  async verifyOtp(payload: otpPayload) {
    try {
      const data = await axiosInstance.post(VERIFY_OTP, payload);
      return data;
    } catch (error) {
      throw error;
    }
  },
  async resendOtp(payload: resendPayload) {
    try {
      const data = await axiosInstance.post(RESEND_OTP, payload);
      return data;
    } catch (error) {
      throw error;
    }
  },
  async login(payload: loginPayload) {
    try {
      const data = await axiosInstance.post(LOGIN, payload);
      return data;
    } catch (error) {
      throw error;
    }
  },
  async getMe(): Promise<getMeData> {
    const { data } = await axiosInstance.get(GET_ME);
    return data;
  },
};

export const authApi = auth;
