import { useDispatch } from "../components/FinsembleProvider";
import { FavoriteActions } from "../actions/favoriteActions";
import { useFavorites } from "./useFavorites";
import { useEffect, useState } from "react";
import { FEAGlobals } from "../../common/Globals";
let storageIsInitialized = false;
/**
 * The useFavoritesShell() hook is used by FavoritesShell to interact with the redux store
 * and storage.
 */
export const useFavoritesShell = () => {
    const [favouritesInitialized, setFavouritesInitialised] = useState(false);
    const dispatch = useDispatch();
    const { favorites, removeFavorite } = useFavorites();
    // Ideally in the future the following function will be decoupled from implementation, allowing Favorites to generically dispatch actions.
    /**
     * Given a favorite item, trigger the associated action on that item.
     * I.e. If it's a finsemble component, a new window of that component is launched. If it's a workspace, the workspace would be switched to.
     * @param id The favorite item's id
     * @param category The to be triggered favorite item's category
     */
    const triggerFavorite = async (id, category) => {
        if (category === "Application") {
            FEAGlobals.FSBL.Clients.LauncherClient.spawn(id, {
                addToWorkspace: true,
                monitor: "mine",
            });
        }
        else if (category === "Workspace") {
            FEAGlobals.FSBL.Clients.WorkspaceClient.switchTo({ name: id });
        }
    };
    /**
     * We need to persist the right order in whent he user reorders favorites in the toolbar. This ensures
     * that favorites will appear in the right order on future Toolbar launches.
     * @param favElements The ordered collection of favorite elements
     */
    const setFavoritesOrder = ({ oldIndex, newIndex }) => {
        dispatch(FavoriteActions.REORDER_FAVORITES({ oldIndex, newIndex }));
    };
    /**
     * This effect initializes the favorites redux store. The store must only be initialized once, even though useFavorites may be
     * referenced by many different React components. We use the module level variable `storageIsInitialized` as a simple flag to
     * prevent initialization from occuring more than once.
     */
    useEffect(() => {
        if (storageIsInitialized)
            return;
        storageIsInitialized = true;
        // Copy the persisted favorites from Storage to the redux store
        FEAGlobals.FSBL.Clients.StorageClient.getStandardized({ topic: "favorites", key: "allCurrentFavorites" }, (err, data) => {
            if (err) {
                return FEAGlobals.FSBL.Clients.Logger.system.error(`Favorite Hook -> Failed to retrieve favorites from StorageClient: ${err}`);
            }
            dispatch(FavoriteActions.INITIALIZE_FAVORITES(data));
            setFavouritesInitialised(true);
        });
    }, [dispatch]);
    // When the list of favorite items change, we want to persist that data to the store.
    useEffect(() => {
        if (!favouritesInitialized) {
            return;
        }
        FEAGlobals.FSBL.Clients.StorageClient.save({
            topic: "favorites",
            key: "allCurrentFavorites",
            value: favorites,
        });
    }, [favorites]);
    return {
        favorites,
        removeFavorite,
        triggerFavorite,
        setFavoritesOrder,
    };
};
//# sourceMappingURL=useFavoritesShell.js.map