"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGroupProfile = exports.getGroupMemberList = exports.generateStatusChangeText = exports.generateText = exports.getRemoteUserProfile = exports.getMyProfile = exports.setDefaultUserInfo = void 0;
const index_1 = require("../const/index");
const index_2 = require("../locales/index");
// 设置默认的 UserInfo 信息
function setDefaultUserInfo(userId, domId) {
    const userInfo = {
        userId,
        nick: '',
        avatar: '',
        remark: '',
        displayUserInfo: '',
        isAudioAvailable: false,
        isVideoAvailable: false,
        isEnter: false,
        domId: domId || userId,
    };
    return domId ? userInfo : Object.assign(Object.assign({}, userInfo), { isEnter: false }); // localUserInfo 没有 isEnter, remoteUserInfoList 有 isEnter
}
exports.setDefaultUserInfo = setDefaultUserInfo;
// 获取个人用户信息
function getMyProfile(myselfUserId, tim, TUIStore) {
    var _a, _b, _c, _d, _e;
    return __awaiter(this, void 0, void 0, function* () {
        let localUserInfo = setDefaultUserInfo(myselfUserId, index_1.NAME.LOCAL_VIDEO);
        try {
            if (!tim)
                return localUserInfo;
            const res = yield tim.getMyProfile();
            const currentLocalUserInfo = TUIStore === null || TUIStore === void 0 ? void 0 : TUIStore.getData(index_1.StoreName.CALL, index_1.NAME.LOCAL_USER_INFO); // localUserInfo may have been updated
            if ((res === null || res === void 0 ? void 0 : res.code) === 0) {
                localUserInfo = Object.assign(Object.assign(Object.assign({}, localUserInfo), currentLocalUserInfo), { userId: (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.userID, nick: (_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.nick, avatar: (_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.avatar, displayUserInfo: ((_d = res === null || res === void 0 ? void 0 : res.data) === null || _d === void 0 ? void 0 : _d.nick) || ((_e = res === null || res === void 0 ? void 0 : res.data) === null || _e === void 0 ? void 0 : _e.userID) });
            }
            return localUserInfo;
        }
        catch (error) {
            console.error(`${index_1.NAME.PREFIX}getMyProfile failed, error: ${error}.`);
            return localUserInfo;
        }
    });
}
exports.getMyProfile = getMyProfile;
// 获取远端用户列表信息
function getRemoteUserProfile(userIdList, tim, TUIStore) {
    return __awaiter(this, void 0, void 0, function* () {
        let remoteUserInfoList = userIdList.map((userId) => setDefaultUserInfo(userId));
        try {
            if (!tim)
                return remoteUserInfoList;
            const res = yield tim.getFriendProfile({ userIDList: userIdList });
            if (res.code === 0) {
                const { friendList = [], failureUserIDList = [] } = res.data;
                let unFriendList = failureUserIDList.map((obj) => obj.userID);
                if (failureUserIDList.length > 0) {
                    const res = yield tim.getUserProfile({ userIDList: failureUserIDList.map((obj) => obj.userID) });
                    if ((res === null || res === void 0 ? void 0 : res.code) === 0) {
                        unFriendList = (res === null || res === void 0 ? void 0 : res.data) || [];
                    }
                }
                const currentRemoteUserInfoList = TUIStore === null || TUIStore === void 0 ? void 0 : TUIStore.getData(index_1.StoreName.CALL, index_1.NAME.REMOTE_USER_INFO_LIST); // remoteUserInfoList may have been updated
                const tempFriendIdList = friendList.map((obj) => obj.userID);
                const tempUnFriendIdList = unFriendList.map((obj) => obj.userID);
                remoteUserInfoList = userIdList.map((userId) => {
                    var _a, _b, _c, _d, _e, _f, _g;
                    const defaultUserInfo = setDefaultUserInfo(userId);
                    const friendListIndex = tempFriendIdList.indexOf(userId);
                    const unFriendListIndex = tempUnFriendIdList.indexOf(userId);
                    let remark = '';
                    let nick = '';
                    let displayUserInfo = '';
                    let avatar = '';
                    if (friendListIndex !== -1) {
                        remark = ((_a = friendList[friendListIndex]) === null || _a === void 0 ? void 0 : _a.remark) || '';
                        nick = ((_c = (_b = friendList[friendListIndex]) === null || _b === void 0 ? void 0 : _b.profile) === null || _c === void 0 ? void 0 : _c.nick) || '';
                        displayUserInfo = remark || nick || defaultUserInfo.userId || '';
                        avatar = ((_e = (_d = friendList[friendListIndex]) === null || _d === void 0 ? void 0 : _d.profile) === null || _e === void 0 ? void 0 : _e.avatar) || '';
                    }
                    if (unFriendListIndex !== -1) {
                        nick = ((_f = unFriendList[unFriendListIndex]) === null || _f === void 0 ? void 0 : _f.nick) || '';
                        displayUserInfo = nick || defaultUserInfo.userId || '';
                        avatar = ((_g = unFriendList[unFriendListIndex]) === null || _g === void 0 ? void 0 : _g.avatar) || '';
                    }
                    const userInfo = currentRemoteUserInfoList.find(subObj => subObj.userId === userId) || {};
                    return Object.assign(Object.assign(Object.assign({}, defaultUserInfo), userInfo), { remark, nick, displayUserInfo, avatar });
                });
            }
            return remoteUserInfoList;
        }
        catch (error) {
            console.error(`${index_1.NAME.PREFIX}getRemoteUserProfile failed, error: ${error}.`);
            return remoteUserInfoList;
        }
    });
}
exports.getRemoteUserProfile = getRemoteUserProfile;
// 生成弹框提示文案
function generateText(TUIStore, key, prefix, suffix) {
    const isGroup = TUIStore.getData(index_1.StoreName.CALL, index_1.NAME.IS_GROUP);
    let callTips = `${(0, index_2.t)(key)}`;
    if (isGroup) {
        callTips = prefix ? `${prefix} ${callTips}` : callTips;
        callTips = suffix ? `${callTips} ${suffix}` : callTips;
    }
    return callTips;
}
exports.generateText = generateText;
// 生成 statusChange 抛出的字符串
function generateStatusChangeText(TUIStore) {
    const callStatus = TUIStore.getData(index_1.StoreName.CALL, index_1.NAME.CALL_STATUS);
    if (callStatus === index_1.CallStatus.IDLE) {
        return index_1.StatusChange.IDLE;
    }
    const isGroup = TUIStore.getData(index_1.StoreName.CALL, index_1.NAME.IS_GROUP);
    if (callStatus === index_1.CallStatus.CALLING) {
        return isGroup ? index_1.StatusChange.DIALING_GROUP : index_1.StatusChange.DIALING_C2C;
    }
    const callMediaType = TUIStore.getData(index_1.StoreName.CALL, index_1.NAME.CALL_MEDIA_TYPE);
    if (isGroup) {
        return callMediaType === index_1.CallMediaType.AUDIO ? index_1.StatusChange.CALLING_GROUP_AUDIO : index_1.StatusChange.CALLING_GROUP_VIDEO;
    }
    return callMediaType === index_1.CallMediaType.AUDIO ? index_1.StatusChange.CALLING_C2C_AUDIO : index_1.StatusChange.CALLING_C2C_VIDEO;
}
exports.generateStatusChangeText = generateStatusChangeText;
// 获取群组[offset, count + offset]区间成员
function getGroupMemberList(groupID, tim, count, offset) {
    return __awaiter(this, void 0, void 0, function* () {
        let groupMemberList = [];
        try {
            const res = yield tim.getGroupMemberList({ groupID, count, offset });
            if (res.code === 0) {
                return res.data.memberList || groupMemberList;
            }
        }
        catch (error) {
            console.error(`${index_1.NAME.PREFIX}getGroupMember failed, error: ${error}.`);
            return groupMemberList;
        }
    });
}
exports.getGroupMemberList = getGroupMemberList;
// 获取 IM 群信息
function getGroupProfile(groupID, tim) {
    return __awaiter(this, void 0, void 0, function* () {
        let groupProfile = {};
        try {
            const res = yield tim.getGroupProfile({ groupID });
            if (res.code === 0) {
                return res.data.group || groupProfile;
            }
        }
        catch (error) {
            console.error(`${index_1.NAME.PREFIX}getGroupProfile failed, error: ${error}.`);
            return groupProfile;
        }
    });
}
exports.getGroupProfile = getGroupProfile;
