import React, { useMemo, useCallback } from "react";
import { transformScoresAPIResponse } from "./utils/transformScoresAPIResponse";
import { useScores } from "~/features/clientLibrary/useScores";
export function useScoreSelection({ scoresMenu, lat, lng, }) {
    const [activeScore, setActiveScore] = React.useState(null);
    const [activeCategory, setActiveCategory] = React.useState(null);
    const { data, isFetching: isScoresFetching } = useScores({
        location: {
            lat,
            lng,
        },
    });
    const scoresData = useMemo(() => transformScoresAPIResponse(data, scoresMenu), [data, scoresMenu]);
    const activeCategoryData = useMemo(() => {
        if (!activeCategory || !scoresMenu)
            return null;
        const category = scoresMenu.categories.find(([key]) => key === activeCategory);
        if (!category)
            return null;
        return category[1];
    }, [activeCategory, scoresMenu]);
    const handleSelectScore = useCallback((scoreKey) => () => {
        var _a, _b;
        setActiveScore((prev) => (prev === scoreKey ? null : scoreKey));
        if (scoreKey === null) {
            setActiveCategory(null);
            return;
        }
        const categoryKey = (_b = (_a = scoresMenu.categories.find(([, value]) => value.scores.some(([key]) => key === scoreKey))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
        setActiveCategory(categoryKey);
    }, [setActiveScore, setActiveCategory, scoresMenu.categories]);
    /**
     * Category selection is a mobile only feature, so when a category is selected,
     * we also clear the active score to avoid confusion.
     */
    const handleSelectCategory = useCallback((categoryKey) => () => {
        setActiveCategory((prev) => (prev === categoryKey ? null : categoryKey));
        setActiveScore(null);
    }, [setActiveCategory, setActiveScore]);
    return {
        isLoading: isScoresFetching,
        scoresData,
        activeScore,
        activeScoreData: activeScore === null ? undefined : scoresData[activeScore],
        activeCategory,
        activeCategoryData,
        handleSelectScore,
        handleSelectCategory,
    };
}
//# sourceMappingURL=index.js.map