import jwt_decode from "jwt-decode";
import * as actions from "../constants/actions/index";

const initialState = {
  data: {},
  token: "",
};

let authReducer = (state = initialState, action) => {
  // debugger
  switch (action.type) {
    case actions.LOGIN:
      var data = jwt_decode(action.token);
      console.log(data);

      // localStorage.authToken = data.data.login;
      const token = localStorage.authToken;

      localStorage.authToken = action.token;
      console.log({ data: { data }, token: action.token });
      return { ...state, data: { data }, token: action.token };

    case actions.LOGOUT:
      localStorage.authToken = "";
      return { ...state, data: initialState.data };

    default:
      return { ...state };
  }
};

export default authReducer;
