import { produce } from "immer";
import { FavoriteActions } from "../actions/favoriteActions";
export const initialState = {
    favorites: [],
};
export const favoriteReducer = produce((state, action) => {
    FavoriteActions.match(action, {
        INITIALIZE_FAVORITES: (favorites) => {
            state.favorites = (favorites === null || favorites === void 0 ? void 0 : favorites.length) ? favorites : [];
        },
        ADD_FAVORITE: (favorite) => {
            state.favorites.push(favorite);
        },
        REMOVE_FAVORITE: (targetId) => {
            const newFavorites = state.favorites.filter(({ id }) => id !== targetId);
            state.favorites = newFavorites;
        },
        REORDER_FAVORITES: ({ oldIndex, newIndex }) => {
            const favoritesInNewOrder = [...state.favorites];
            const favoriteToMove = favoritesInNewOrder[oldIndex];
            favoritesInNewOrder.splice(oldIndex, 1);
            favoritesInNewOrder.splice(newIndex, 0, favoriteToMove);
            state.favorites = favoritesInNewOrder;
        },
        default: () => state,
    });
}, initialState);
//# sourceMappingURL=favoriteReducer.js.map