import { useMemo, useState } from "react";
import { addAffordabilityComparisonToNeighborhood } from "./addAffordabilityComparisonToNeighborhood";
import { useResponsiveBreakpoints } from "~/hooks/useResponsiveBreakpoints";
import { useSimilarNeighborhoods as useClientLibrarySimilarNeighborhoods, } from "~/features/clientLibrary/useSimilarNeighborhoods";
const MIN_PER_ROW = 4;
const MAX_LIMIT = 8;
export const useSimilarNeighborhoods = (params) => {
    const breakpoints = useResponsiveBreakpoints();
    const [limit, setLimit] = useState(MIN_PER_ROW);
    const { isLoading: isSimilarNeighborhoodsLoading, isFetched: isSimilarNeighborhoodsFetched, data, } = useClientLibrarySimilarNeighborhoods(Object.assign(Object.assign({}, params), { limit: MAX_LIMIT }));
    const similarNeighborhoods = useMemo(() => {
        let neighborhoods = [];
        if (!data || !data.most_similar) {
            return neighborhoods;
        }
        if (data.most_similar.length < limit || !(breakpoints === null || breakpoints === void 0 ? void 0 : breakpoints.lg)) {
            // Somehow we wouldn't have enough to return.
            // So just return what we have
            // We'll also return everything on mobile
            neighborhoods = [...data.most_similar];
        }
        else {
            neighborhoods = data.most_similar.slice(0, limit);
        }
        return neighborhoods.map((similarNeighborhood) => addAffordabilityComparisonToNeighborhood((data === null || data === void 0 ? void 0 : data.affordability_index) || undefined, similarNeighborhood));
    }, [data, limit, breakpoints]);
    const hasMore = ((data === null || data === void 0 ? void 0 : data.most_similar) || []).length > MIN_PER_ROW && limit < MAX_LIMIT;
    const hasAffordabilityIndex = similarNeighborhoods.some((neighborhood) => typeof (neighborhood === null || neighborhood === void 0 ? void 0 : neighborhood.affordabilityComparison) !== "undefined");
    const viewMore = () => {
        if (limit === MAX_LIMIT)
            return;
        setLimit(limit + MIN_PER_ROW);
    };
    const viewLess = () => {
        if (limit < MAX_LIMIT)
            return;
        setLimit(limit - MIN_PER_ROW);
    };
    return {
        isLoading: isSimilarNeighborhoodsLoading,
        isFetched: isSimilarNeighborhoodsFetched,
        similarNeighborhoods,
        hasAffordabilityIndex,
        neighborhoodName: data === null || data === void 0 ? void 0 : data.name,
        limit,
        minimumLimit: MIN_PER_ROW,
        hasMore,
        viewMore,
        viewLess,
    };
};
//# sourceMappingURL=index.js.map