"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = require("react");
const getLoginInfo_1 = tslib_1.__importDefault(require("../getLoginInfo"));
const school_year_v3_1 = require("../../../query/school-year-v3");
const PHXGrpcClientV3_1 = require("../GRPC/PHXGrpcClientV3");
function PHXGetCurrentYearWithTermHocVuV3() {
    const schoolInfo = (0, getLoginInfo_1.default)();
    const [currentYear, setCurrentYear] = (0, react_1.useState)();
    const today = new Date();
    (0, react_1.useEffect)(() => {
        const getData = async () => {
            try {
                const res = await (0, PHXGrpcClientV3_1.PHXClientQueryV3)({
                    query: school_year_v3_1.querySchoolYearHocvuV3,
                    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 (0, PHXGrpcClientV3_1.PHXClientQueryV3)({
                        query: school_year_v3_1.querySchoolYearHocvuV3,
                        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;
}
exports.default = PHXGetCurrentYearWithTermHocVuV3;
//# sourceMappingURL=getSchoolYearHocVuV3.js.map