"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useImportUser = void 0;
const tslib_1 = require("tslib");
const getLoginInfo_1 = tslib_1.__importDefault(require("../../Func/getLoginInfo"));
const PHXGrpcClientV6_1 = require("../../Func/GRPC/PHXGrpcClientV6");
const getSchoolYearV3_1 = tslib_1.__importDefault(require("../../Func/SchoolYearV3/getSchoolYearV3"));
const constant_1 = require("../constant");
const query_1 = require("../graphql/query");
const useImportUser = () => {
    var _a, _b;
    const userInfo = (0, getLoginInfo_1.default)();
    const schoolId = userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id;
    const schoolYear = (0, getSchoolYearV3_1.default)();
    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 || constant_1.EMPTY_VALUE,
        phone: item.phoneNumber || constant_1.EMPTY_VALUE,
        email: item.email || constant_1.EMPTY_VALUE,
        name: item.fullName || constant_1.EMPTY_VALUE,
        emailSecondary: item.emailSecondary || constant_1.EMPTY_VALUE,
        isUseEmailSecondary: item.isUseEmailSecondary,
        full_name: item.fullName,
    }));
    const formatTeacherData = (data) => data.map((item) => ({
        id: item.coreAdminPublicUserByTeacherIdAndSchoolId.id,
        code: item.userCode || constant_1.EMPTY_VALUE,
        phone: item.coreAdminPublicUserByTeacherIdAndSchoolId.phoneNumber || constant_1.EMPTY_VALUE,
        email: item.coreAdminPublicUserByTeacherIdAndSchoolId.email || constant_1.EMPTY_VALUE,
        name: item.coreAdminPublicUserByTeacherIdAndSchoolId.fullName || constant_1.EMPTY_VALUE,
        emailSecondary: item.coreAdminPublicUserByTeacherIdAndSchoolId.emailSecondary || constant_1.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 || constant_1.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) || constant_1.EMPTY_VALUE,
            grade: ((_f = item.coreStudentPublicGradeByGradeIdAndSchoolId) === null || _f === void 0 ? void 0 : _f.name) || constant_1.EMPTY_VALUE,
            classroom: ((_g = item.classroom) === null || _g === void 0 ? void 0 : _g.name) || constant_1.EMPTY_VALUE,
            parents: parents || [],
        };
    });
    const findUserInFo = async (ids) => {
        const { data } = await (0, PHXGrpcClientV6_1.PHXClientQueryV6)({
            schemaGroup: constant_1.ECoreSchemaGroup.CORE_ADMIN,
            variables: {
                ids,
                schoolId,
            },
            query: query_1.FIND_USER_INFO,
        });
        return data.allCoreAdminPublicUsers.nodes;
    };
    const findClassroomByUserIds = async (userIds, querySchoolYearId, querySchoolId) => {
        const { data } = await (0, PHXGrpcClientV6_1.PHXClientQueryV6)({
            schemaGroup: constant_1.ECoreSchemaGroup.CORE_ACADEMIC,
            variables: {
                userIds,
                schoolYearId: querySchoolYearId,
                schoolId: querySchoolId,
            },
            query: query_1.FIND_CLASSROOM_BY_USER_IDS,
        });
        return data.allCoreAcademicPublicClassroomStudents.nodes;
    };
    const findStudentByUserCodes = async (userCodes) => {
        const { data } = await (0, PHXGrpcClientV6_1.PHXClientQueryV6)({
            schemaGroup: constant_1.ECoreSchemaGroup.CORE_STUDENT,
            variables: {
                userCodes,
                schoolId,
            },
            query: query_1.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 (0, PHXGrpcClientV6_1.PHXClientQueryV6)({
            schemaGroup: constant_1.ECoreSchemaGroup.CORE_ADMIN,
            variables: {
                userCodes,
                schoolId,
            },
            query: query_1.FIND_STAFF_BY_USER_CODE,
        });
        const result = data.allCoreAdminPublicUsers.nodes;
        return formatStaffData(result);
    };
    const findTeacherByUserCodes = async (userCodes) => {
        const { data } = await (0, PHXGrpcClientV6_1.PHXClientQueryV6)({
            schemaGroup: constant_1.ECoreSchemaGroup.CORE_ADMIN,
            variables: {
                userCodes,
                schoolId,
            },
            query: query_1.FIND_TEACHER_BY_USER_CODE,
        });
        const result = data.allCoreAdminPublicProfileTeachers.nodes;
        return formatTeacherData(result);
    };
    const findUserByUserCodes = async (target, userCodes) => {
        switch (target) {
            case constant_1.ERoleCode.STUDENT:
                return findStudentByUserCodes(userCodes);
            case constant_1.ERoleCode.STAFF:
                return findStaffByUserCodes(userCodes);
            case constant_1.ERoleCode.TEACHER:
                return findTeacherByUserCodes(userCodes);
            default:
                return [];
        }
    };
    return {
        findUserByUserCodes,
    };
};
exports.useImportUser = useImportUser;
//# sourceMappingURL=use-import-user.js.map