import BaseCourseController from '@/services/apis/BaseCourseController';
import BaseGradeController from '@/services/apis/BaseGradeController';
import BaseSemesterController from '@/services/apis/BaseSemesterController';
import NceeCourseCombController from '@/services/apis/NceeCourseCombController';
import SysOrgController from '@/services/apis/SysOrgController';
import { useCallback, useEffect, useState } from 'react';

/** 基础数据类型 */
export type BaseDataModelType = {
    /** 学期 */
    semesters?: API.SemesterOutput[];
    /** 学科 */
    courses?: API.CourseOutput[];
    /** 选科组合 */
    nceeCourseCombs?: API.NceeCourseCombOutput[];
    /** 年级 */
    grades?: API.GradeOutput[];
    /** 机构 */
    sysOrgs?: API.SysOrgLiteOutput[];
};

/**
 * 基础数据
 */
export default () => {
    const [baseData, setBaseData] = useState<BaseDataModelType>();

    const fetchBaseData = useCallback(async () => {
        const semesters = (await BaseSemesterController.getAllList()) ?? [];
        const courses = (await BaseCourseController.getAllList()) ?? [];
        const nceeCourseCombs = (await NceeCourseCombController.getAllList()) ?? [];
        const grades = (await BaseGradeController.getAllList()) ?? [];
        const sysOrgs = (await SysOrgController.getAllList()) ?? [];
        setBaseData({
            semesters,
            courses,
            nceeCourseCombs,
            grades,
            sysOrgs,
        });
    }, []);

    useEffect(() => { fetchBaseData(); }, []);

    return { baseData, fetchBaseData };
};
