import { createSlice } from "@reduxjs/toolkit";

export const searchSlice = createSlice({
  name: "search",
  initialState: {
    search: {},
    temp: [],
  },
  reducers: {
    setTemp: (state, action) => {
      if (state.temp.length > 0) return;
      state.temp = action.payload;
    },

    setSearchData: (state, action) => {
      const { key, value } = action.payload;
      state.search = { ...state.search, [key]: value };
    },

    resetSearch: (state) => {
      state.search = {};
      state.temp = [];
    },
  },
  selectors: {
    selectTemp: (state) => state.temp,
    selectSearch: (state) => state.search,
  },
});

export const { setSearchData, setTemp, resetSearch } = searchSlice.actions;
export const { selectSearch, selectTemp } = searchSlice.selectors;

export default searchSlice.reducer;
