import { createReducer } from '@reduxjs/toolkit';
import { IAuthorizationState , IAuthorizationPayload} from '../../../typescript/redux/authorization/interfaces';
import {
  actionLogInSuccess,
  actionLogInReject,
  actionLogOutSuccess,
  actionLogOutReject,
  actionCurrentSuccess,
  actionCurrentReject
} from '../action';

const initialState:IAuthorizationState = {
  name: "",
  lastName: "",
  originalName: "",
  originalLastName: "",
  avatarUrl: "",
  color: "",
  code: "",
  _id: "",
  token: "",
  online: "",
  number: "" ,
  country: "",
  sort: false,
  nightMode: false,
  createdAt: "",
  updatedAt: "",
  __v: 0,
}

const reducerAuthorization = createReducer(initialState, {
  [actionLogInSuccess.type]: (state, { payload:token }: { payload: string }) => {
    return {...state,token};
  },
  [actionLogInReject.type]: (state, _) => {
    return state;
  },
  [actionLogOutSuccess.type]: (_state, _) => {
    return initialState;
  },
  [actionLogOutReject.type]: (state, _) => {
    return state;
  },
  [actionCurrentSuccess.type]: (_state, { payload }:IAuthorizationPayload) => {
    return payload
  },
  [actionCurrentReject.type]: (_state, _) => {
    return initialState;
  },
});

export default reducerAuthorization;
