import { AccountInfo } from "../../services/auth/types";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

const accountSlice = createSlice({
  name: "account",
  initialState: {
    account: null,
  },
  reducers: {
    accountLogin: (state, action: PayloadAction<AccountInfo>) => {
      localStorage.setItem("token", action.payload.accessToken);
      localStorage.setItem("refreshToken", action.payload.refreshToken);
      localStorage.setItem("accountInfo", JSON.stringify(action.payload));
      return {
        ...state,
        account: action.payload,
      };
    },
    accountLogout: (state) => {
      localStorage.removeItem("token");
      localStorage.removeItem("refreshToken");
      localStorage.removeItem("accountInfo");
      return {
        ...state,
        account: null,
      };
    },
  },
});

export const { accountLogin, accountLogout } = accountSlice.actions;
export default accountSlice.reducer;
