import { useMemo } from "react";
import { normalizeEnhancedPoisToPois } from "./normalizeEnhancedPoisToPois";
import { useEnhancedPois } from "~/features/clientLibrary/useEnhancedPois";
import { usePois } from "~/features/clientLibrary/usePois";
import { useSchools } from "~/features/clientLibrary/useSchools";
const DEFAULT_RADIUS = 2500;
const DEFAULT_NUMBER_OF_POIS = 30;
const DEFAULT_MINUMUM_POIS = 3;
// School category keys from the Schools type
const SCHOOL_CATEGORIES = [
    "primary_schools",
    "high_schools",
];
export const useMarkers = ({ lat, lng, geogId, categories, categoriesDeprecated, radius = DEFAULT_RADIUS, buffer = 250, limit = DEFAULT_NUMBER_OF_POIS, }) => {
    // Split categories into POI and school categories
    const enhancedPoiCategories = useMemo(() => categories === null || categories === void 0 ? void 0 : categories.filter((category) => !SCHOOL_CATEGORIES.includes(category)), [categories]);
    // Split categories into POI and school categories
    const poiCategoriesDeprecated = useMemo(() => categoriesDeprecated === null || categoriesDeprecated === void 0 ? void 0 : categoriesDeprecated.filter((category) => !SCHOOL_CATEGORIES.includes(category)), [categoriesDeprecated]);
    const schoolCategories = useMemo(() => {
        var _a;
        return (_a = (categories || categoriesDeprecated)) === null || _a === void 0 ? void 0 : _a.filter((cat) => SCHOOL_CATEGORIES.includes(cat));
    }, [categories, categoriesDeprecated]);
    const hasEnhancedPoiCategories = enhancedPoiCategories && enhancedPoiCategories.length > 0;
    const hasPoiCategoriesDeprecated = poiCategoriesDeprecated && poiCategoriesDeprecated.length > 0;
    const hasSchoolCategories = schoolCategories && schoolCategories.length > 0;
    const limits = useMemo(() => ({
        regular: limit > 30 ? 30 : limit,
        enhanced: limit > 500 ? 500 : limit,
    }), [limit]);
    const { isLoading: isEnhancedPoisLoading, data: enhancedPoisData } = useEnhancedPois({
        lat,
        lng,
        geogId,
        include: hasEnhancedPoiCategories ? enhancedPoiCategories : undefined,
        limit: limits.enhanced,
        attemptMinimum: DEFAULT_MINUMUM_POIS,
        radius,
        buffer,
    });
    const { isLoading: isPoisLoading, data: poisData } = usePois({
        lat,
        lng,
        include: hasPoiCategoriesDeprecated ? poiCategoriesDeprecated : undefined,
        // The pois endpoint has a maximum limit of 30, so we set it to 30 if the limit is greater than 30
        limit: limits.regular,
        attemptMinimum: DEFAULT_MINUMUM_POIS,
        radius,
    });
    const { isLoading: isSchoolsLoading, data: schoolsData } = useSchools({
        lat,
        lng,
        geogId,
        include: hasSchoolCategories ? schoolCategories : undefined,
        // The schools endpoint has a maximum limit of 30, so we set it to 30 if the limit is greater than 30
        limit: limits.regular,
        attemptMinimum: DEFAULT_MINUMUM_POIS,
        radius,
        buffer,
        includeCatchments: true,
    });
    const markers = useMemo(() => {
        const result = {};
        // Add Enhanced POI data if we requested enhanced POI categories
        if (hasEnhancedPoiCategories && enhancedPoisData) {
            const normalizedPois = normalizeEnhancedPoisToPois(enhancedPoiCategories, enhancedPoisData);
            Object.assign(result, normalizedPois);
        }
        // Add POI data if we requested POI categories
        if (hasPoiCategoriesDeprecated && poisData) {
            Object.assign(result, poisData);
        }
        // Add school data if we requested school categories
        if (hasSchoolCategories && schoolsData) {
            if (schoolCategories === null || schoolCategories === void 0 ? void 0 : schoolCategories.includes("high_schools")) {
                result.high_schools = schoolsData.high_schools || [];
            }
            if (schoolCategories === null || schoolCategories === void 0 ? void 0 : schoolCategories.includes("primary_schools")) {
                result.primary_schools = schoolsData.primary_schools || [];
            }
        }
        return result;
    }, [
        poisData,
        enhancedPoisData,
        schoolsData,
        hasEnhancedPoiCategories,
        hasPoiCategoriesDeprecated,
        hasSchoolCategories,
        enhancedPoiCategories,
        poiCategoriesDeprecated,
        schoolCategories,
    ]);
    return {
        isLoading: (hasEnhancedPoiCategories && isEnhancedPoisLoading) ||
            (hasSchoolCategories && isSchoolsLoading) ||
            (hasPoiCategoriesDeprecated && isPoisLoading) ||
            false,
        data: markers,
    };
};
//# sourceMappingURL=index.js.map