import PHXFuncGetLoggedInfo from '../../Func/getLoginInfo';
import { PHXClientQueryV6 } from '../../Func/GRPC/PHXGrpcClientV6';
import PHXGetCurrentYearWithTermV3 from '../../Func/SchoolYearV3/getSchoolYearV3';
import { ECoreSchemaGroup, ERoleCode, EMPTY_VALUE } from '../constant';
import { FIND_USER_INFO, FIND_CLASSROOM_BY_USER_IDS, FIND_STUDENT_BY_USER_CODE, FIND_STAFF_BY_USER_CODE, FIND_TEACHER_BY_USER_CODE, } from '../graphql/query';
export const useImportUser = () => {
    var _a, _b;
    const userInfo = PHXFuncGetLoggedInfo();
    const schoolId = userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id;
    const schoolYear = PHXGetCurrentYearWithTermV3();
    const schoolYearId = (_b = (_a = schoolYear === null || schoolYear === void 0 ? void 0 : schoolYear.current_school_year) === null || _a === void 0 ? void 0 : _a.schoolYear) === null || _b === void 0 ? void 0 : _b.id;
    const formatStaffData = (data) => data.map((item) => ({
        id: item.id,
        code: item.coreAdminPublicProfileStaffByStaffIdAndSchoolId.userCode || EMPTY_VALUE,
        phone: item.phoneNumber || EMPTY_VALUE,
        email: item.email || EMPTY_VALUE,
        name: item.fullName || EMPTY_VALUE,
        emailSecondary: item.emailSecondary || EMPTY_VALUE,
        isUseEmailSecondary: item.isUseEmailSecondary,
        full_name: item.fullName,
    }));
    const formatTeacherData = (data) => data.map((item) => ({
        id: item.coreAdminPublicUserByTeacherIdAndSchoolId.id,
        code: item.userCode || EMPTY_VALUE,
        phone: item.coreAdminPublicUserByTeacherIdAndSchoolId.phoneNumber || EMPTY_VALUE,
        email: item.coreAdminPublicUserByTeacherIdAndSchoolId.email || EMPTY_VALUE,
        name: item.coreAdminPublicUserByTeacherIdAndSchoolId.fullName || EMPTY_VALUE,
        emailSecondary: item.coreAdminPublicUserByTeacherIdAndSchoolId.emailSecondary || EMPTY_VALUE,
        isUseEmailSecondary: item.coreAdminPublicUserByTeacherIdAndSchoolId.isUseEmailSecondary,
        full_name: item.coreAdminPublicUserByTeacherIdAndSchoolId.fullName,
    }));
    const formatStudentData = (data, userInfoMap = {}) => data.map((item) => {
        var _a, _b, _c, _d, _e, _f, _g;
        const parents = (_c = (_b = (_a = item.coreStudentPublicUserByStudentIdAndSchoolId) === null || _a === void 0 ? void 0 : _a.coreStudentPublicRelationshipUsersByStudentIdAndSchoolId) === null || _b === void 0 ? void 0 : _b.nodes) === null || _c === void 0 ? void 0 : _c.filter((node) => { var _a; return !!((_a = userInfoMap[node.parentId]) === null || _a === void 0 ? void 0 : _a.fullName); }).map((node) => {
            var _a;
            return ({
                id: node.parentId,
                relationship_code: node.relationshipCode || '',
                full_name: ((_a = userInfoMap[node.parentId]) === null || _a === void 0 ? void 0 : _a.fullName) || '',
            });
        });
        return {
            id: Number(item.studentId),
            code: item.userCode || EMPTY_VALUE,
            name: item.fullName || '',
            full_name: item.fullName || '',
            educational_level: ((_e = (_d = item.coreStudentPublicProgramMapEducationalLevelByProgramEducationalIdAndSchoolId) === null || _d === void 0 ? void 0 : _d.coreStudentPublicEducationalLevelByEducationalLevelIdAndSchoolId) === null || _e === void 0 ? void 0 : _e.name) || EMPTY_VALUE,
            grade: ((_f = item.coreStudentPublicGradeByGradeIdAndSchoolId) === null || _f === void 0 ? void 0 : _f.name) || EMPTY_VALUE,
            classroom: ((_g = item.classroom) === null || _g === void 0 ? void 0 : _g.name) || EMPTY_VALUE,
            parents: parents || [],
        };
    });
    const findUserInFo = async (ids) => {
        const { data } = await PHXClientQueryV6({
            schemaGroup: ECoreSchemaGroup.CORE_ADMIN,
            variables: {
                ids,
                schoolId,
            },
            query: FIND_USER_INFO,
        });
        return data.allCoreAdminPublicUsers.nodes;
    };
    const findClassroomByUserIds = async (userIds, querySchoolYearId, querySchoolId) => {
        const { data } = await PHXClientQueryV6({
            schemaGroup: ECoreSchemaGroup.CORE_ACADEMIC,
            variables: {
                userIds,
                schoolYearId: querySchoolYearId,
                schoolId: querySchoolId,
            },
            query: FIND_CLASSROOM_BY_USER_IDS,
        });
        return data.allCoreAcademicPublicClassroomStudents.nodes;
    };
    const findStudentByUserCodes = async (userCodes) => {
        const { data } = await PHXClientQueryV6({
            schemaGroup: ECoreSchemaGroup.CORE_STUDENT,
            variables: {
                userCodes,
                schoolId,
            },
            query: FIND_STUDENT_BY_USER_CODE,
        });
        const result = data.allCoreStudentPublicProfileStudents.nodes;
        const userIds = new Set();
        result.forEach((item) => {
            var _a, _b;
            userIds.add(item.studentId);
            const parentNodes = (_b = (_a = item.coreStudentPublicUserByStudentIdAndSchoolId) === null || _a === void 0 ? void 0 : _a.coreStudentPublicRelationshipUsersByStudentIdAndSchoolId) === null || _b === void 0 ? void 0 : _b.nodes;
            parentNodes === null || parentNodes === void 0 ? void 0 : parentNodes.forEach((node) => {
                if (node.parentId) {
                    userIds.add(node.parentId);
                }
            });
        });
        const studentInfo = await findUserInFo(Array.from(userIds));
        const userInfoMap = {};
        studentInfo.forEach((user) => {
            userInfoMap[user.id] = user;
        });
        const classroomData = await findClassroomByUserIds(Array.from(new Set(result.map((item) => item.studentId))), schoolYearId, schoolId);
        const classroomMap = {};
        classroomData.forEach((item) => {
            classroomMap[item.studentId] = item.coreAcademicPublicClassroomByClassroomIdAndSchoolId;
        });
        const mergeData = result.map((item) => {
            const userDetail = userInfoMap[item.studentId];
            const classroom = classroomMap[item.studentId];
            return {
                ...item,
                fullName: userDetail === null || userDetail === void 0 ? void 0 : userDetail.fullName,
                classroom: classroom
                    ? {
                        id: classroom.rowId,
                        name: classroom.name,
                    }
                    : undefined,
            };
        });
        return formatStudentData(mergeData, userInfoMap);
    };
    const findStaffByUserCodes = async (userCodes) => {
        const { data } = await PHXClientQueryV6({
            schemaGroup: ECoreSchemaGroup.CORE_ADMIN,
            variables: {
                userCodes,
                schoolId,
            },
            query: FIND_STAFF_BY_USER_CODE,
        });
        const result = data.allCoreAdminPublicUsers.nodes;
        return formatStaffData(result);
    };
    const findTeacherByUserCodes = async (userCodes) => {
        const { data } = await PHXClientQueryV6({
            schemaGroup: ECoreSchemaGroup.CORE_ADMIN,
            variables: {
                userCodes,
                schoolId,
            },
            query: FIND_TEACHER_BY_USER_CODE,
        });
        const result = data.allCoreAdminPublicProfileTeachers.nodes;
        return formatTeacherData(result);
    };
    const findUserByUserCodes = async (target, userCodes) => {
        switch (target) {
            case ERoleCode.STUDENT:
                return findStudentByUserCodes(userCodes);
            case ERoleCode.STAFF:
                return findStaffByUserCodes(userCodes);
            case ERoleCode.TEACHER:
                return findTeacherByUserCodes(userCodes);
            default:
                return [];
        }
    };
    return {
        findUserByUserCodes,
    };
};
//# sourceMappingURL=use-import-user.js.map