var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { useMemo } from "react";
import { useSchools } from "~/features/clientLibrary/useSchools";
export function useFilteredSchools(_a) {
    var { country, filterLevel, filterType } = _a, schoolsParams = __rest(_a, ["country", "filterLevel", "filterType"]);
    const { isLoading, data: schoolsData } = useSchools(Object.assign(Object.assign({}, schoolsParams), { include: ["schools"], radius: 2000, limit: 30, buffer: 250 }));
    const { schools, filteredSchools, schoolTypes, schoolLevels, hasCategories } = useMemo(() => {
        var _a, _b, _c, _d, _e, _f;
        const result = {
            schools: undefined,
            filteredSchools: undefined,
            schoolTypes: [],
            schoolLevels: [],
            hasCategories: false,
        };
        if (!schoolsData ||
            (!schoolsData.primary_schools && !schoolsData.high_schools)) {
            return result;
        }
        const schoolNames = new Set();
        const schoolTypesSet = new Set();
        const schoolLevelsSet = new Set();
        const mergedSchools = [
            ...(schoolsData.primary_schools || []),
            ...(schoolsData.high_schools || []),
        ];
        const dedupedSchools = [];
        const visibleSchools = [];
        for (const school of mergedSchools) {
            if (schoolNames.has(school.name)) {
                continue;
            }
            schoolNames.add(school.name);
            dedupedSchools.push(school);
            if (country === "USA") {
                result.hasCategories =
                    result.hasCategories ||
                        !!school.is_advanced_placement ||
                        !!school.is_charter ||
                        !!school.is_magnet;
            }
            for (const type of ((_a = school.school_attributes) === null || _a === void 0 ? void 0 : _a.types) || []) {
                schoolTypesSet.add(type);
            }
            for (const level of ((_b = school.school_attributes) === null || _b === void 0 ? void 0 : _b.levels) || []) {
                schoolLevelsSet.add(level);
            }
            const matchesLevel = filterLevel === undefined ||
                ((_d = (_c = school.school_attributes) === null || _c === void 0 ? void 0 : _c.levels) === null || _d === void 0 ? void 0 : _d.includes(filterLevel));
            const matchesType = filterType === undefined ||
                ((_f = (_e = school.school_attributes) === null || _e === void 0 ? void 0 : _e.types) === null || _f === void 0 ? void 0 : _f.includes(filterType));
            if (matchesLevel && matchesType) {
                visibleSchools.push(school);
            }
        }
        return {
            schools: dedupedSchools,
            filteredSchools: visibleSchools,
            schoolTypes: [...schoolTypesSet],
            schoolLevels: [...schoolLevelsSet],
            hasCategories: result.hasCategories,
        };
    }, [country, filterLevel, filterType, schoolsData]);
    return {
        isLoading,
        schools,
        filteredSchools,
        schoolTypes,
        schoolLevels,
        hasCategories,
    };
}
//# sourceMappingURL=index.js.map