import { combineReducers } from "redux";
import { EMPTY_STORE_STATE, RESET_REDUX } from "../common/actionTypes";
import logInAsCompanyAdminReducer from "../views/ClinicDetails/store/logInAsClinicAdminReducer";
import loginReducer from "../views/Login/loginReducer";
import signupReducer from "../views/Signup/signupReducer";
import userRoleReducer from "./userRoleSlice";

const appReducer = combineReducers({
  signup: signupReducer,
  login: loginReducer,
  loginAsCompanyAdmin: logInAsCompanyAdminReducer,
  userRole: userRoleReducer,
});

const rootReducer = (state, action) => {
  if (action.type === EMPTY_STORE_STATE) {
    state = {};
  }
  if (action.type === RESET_REDUX) {
    if (action.payload.reducerKey && action.payload.key) {
      const { reducerKey, key, value } = action.payload;
      state = {
        ...state,
        [reducerKey]: { ...state[reducerKey], [key]: value },
      };
    }
  }
  return appReducer(state, action);
};

export default rootReducer;
