import { useState, useEffect } from 'react';
import PHXFuncGetLoggedInfo from '../getLoginInfo';
import { querySchoolYearV3 } from '../../../query/school-year-v3';
import { PHXClientQueryV3 } from '../GRPC/PHXGrpcClientV3';
function PHXGetCurrentYearWithTermV3() {
    const schoolInfo = PHXFuncGetLoggedInfo();
    const [currentYear, setCurrentYear] = useState();
    const today = new Date();
    useEffect(() => {
        const getData = async () => {
            try {
                const res = await PHXClientQueryV3({
                    query: querySchoolYearV3,
                    variables: { lte: today, gte: today, school_id: schoolInfo === null || schoolInfo === void 0 ? void 0 : schoolInfo.school_id },
                });
                const { data: { school_year }, } = res;
                if (school_year.length > 0) {
                    // @ts-ignore
                    const { school_terms, current_term, ...schoolYear } = school_year[0] ? school_year[0] : {};
                    const schoolTerm = current_term ? current_term[0] : {};
                    setCurrentYear({ current_school_year: { schoolYear, school_terms }, current_school_term: schoolTerm });
                }
                else {
                    const currentMonth = today.getMonth();
                    const monthBeforeActive = currentMonth - 3;
                    const gteAndLte = new Date(today.getFullYear(), monthBeforeActive, today.getDate());
                    const res = await PHXClientQueryV3({
                        query: querySchoolYearV3,
                        variables: { lte: gteAndLte, gte: gteAndLte, school_id: schoolInfo === null || schoolInfo === void 0 ? void 0 : schoolInfo.school_id },
                    });
                    const { data: { school_year }, } = res;
                    // @ts-ignore
                    const { school_terms, current_term, ...schoolYear } = school_year[0] ? school_year[0] : {};
                    const schoolTerm = current_term ? current_term[0] : {};
                    setCurrentYear({ current_school_year: { schoolYear, school_terms }, current_school_term: schoolTerm });
                }
            }
            catch (err) {
                console.log(err);
            }
        };
        getData();
    }, []);
    return currentYear;
}
export default PHXGetCurrentYearWithTermV3;
//# sourceMappingURL=getSchoolYearV3.js.map