import {HttpRequest, RestConstant, StatusCode} from 'constants';

const initialState = {
  _data: undefined,
  _error: undefined,
};

export const userLogin = async payload => {
  const newState = {...initialState};
  try {
    const response = await HttpRequest.post(RestConstant.login, payload);
    switch (response.request.status) {
      case StatusCode.OK:
        newState._data = response.data;
        return newState;
      case StatusCode.UNAUTHORIZED:
        newState._error = response.response.data;
        return newState;
      case StatusCode.SERVER_ERROR:
        newState._error = response.response.data;
        return newState;
    }
  } catch (error) {
    console.log(error);
  }
};
export const userRegister = async payload => {
  const newState = {...initialState};
  try {
    const response = await HttpRequest.post(RestConstant.register, payload);
    switch (response.request.status) {
      case StatusCode.CREATED:
        newState._data = response.data;
        return newState;
      case StatusCode.NOT_FOUND:
        newState._error = response.response.data;
        return newState;
      case StatusCode.SERVER_ERROR:
        newState._error = response.response.data;
        return newState;
    }
  } catch (error) {
    console.log(error);
  }
};

export const verifyOTP = async (token, otp) => {
  const newState = {...initialState};
  try {
    const response = await HttpRequest.post(
      RestConstant.verifyOTP,
      {
        otp_code: otp,
      },
      {
        headers: {
          'eboga-auth': token,
        },
      },
    );
    switch (response.request.status) {
      case StatusCode.OK:
        newState._data = response.data;
        return newState;
      case StatusCode.UNAUTHORIZED:
        newState._error = response.response.data;
        return newState;
    }
  } catch (error) {
    console.log(error);
  }
};
