// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
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());
    });
};
/**
 * @private
 */
export const createProfileStateModifier = (onFetchProfile, notifyUpdate) => {
    const cachedDisplayName = {};
    return (state) => {
        var _a;
        const originalParticipants = (_a = state.thread) === null || _a === void 0 ? void 0 : _a.participants;
        (() => __awaiter(void 0, void 0, void 0, function* () {
            let shouldNotifyUpdates = false;
            if (!originalParticipants) {
                return;
            }
            for (const [key, participant] of Object.entries(originalParticipants)) {
                if (cachedDisplayName[key]) {
                    continue;
                }
                const profile = yield onFetchProfile(key, {
                    displayName: participant.displayName
                });
                if ((profile === null || profile === void 0 ? void 0 : profile.displayName) && participant.displayName !== (profile === null || profile === void 0 ? void 0 : profile.displayName)) {
                    cachedDisplayName[key] = profile === null || profile === void 0 ? void 0 : profile.displayName;
                    shouldNotifyUpdates = true;
                }
            }
            // notify update only when there is a change, which most likely will trigger modifier and setState again
            if (shouldNotifyUpdates) {
                notifyUpdate();
            }
        }))();
        const participantsModifier = createParticipantModifier((id, participant) => {
            if (cachedDisplayName[id]) {
                return Object.assign(Object.assign({}, participant), { displayName: cachedDisplayName[id] });
            }
            return undefined;
        });
        const modifiedParticipantState = participantsModifier(state);
        const chatMessagesModifier = createChatMessageModifier((id, chatMessage) => {
            var _a;
            const originalChatMessage = Object.assign({}, chatMessage);
            if ((_a = originalChatMessage.content) === null || _a === void 0 ? void 0 : _a.participants) {
                const newParticipants = originalChatMessage.content.participants.map((participant) => {
                    if (participant.id) {
                        if ('communicationUserId' in participant.id && cachedDisplayName[participant.id.communicationUserId]) {
                            return Object.assign(Object.assign({}, participant), { displayName: cachedDisplayName[participant.id.communicationUserId] });
                        }
                        else if ('microsoftTeamsUserId' in participant.id && 'rawId' in participant.id && participant.id.rawId && cachedDisplayName[participant.id.rawId]) {
                            return Object.assign(Object.assign({}, participant), { displayName: cachedDisplayName[participant.id.rawId] });
                        }
                        else if ('teamsAppId' in participant.id && 'rawId' in participant.id && participant.id.rawId && cachedDisplayName[participant.id.rawId]) {
                            return Object.assign(Object.assign({}, participant), { displayName: cachedDisplayName[participant.id.rawId] });
                        }
                        else if ('phoneNumber' in participant.id && 'rawId' in participant.id && participant.id.rawId && cachedDisplayName[participant.id.rawId]) {
                            return Object.assign(Object.assign({}, participant), { displayName: cachedDisplayName[participant.id.rawId] });
                        }
                        else if ('id' in participant.id && cachedDisplayName[participant.id.id]) {
                            return Object.assign(Object.assign({}, participant), { displayName: cachedDisplayName[participant.id.id] });
                        }
                        else {
                            return participant;
                        }
                    }
                    return participant;
                });
                originalChatMessage.content = Object.assign(Object.assign({}, originalChatMessage.content), { participants: newParticipants });
            }
            if (originalChatMessage.sender && originalChatMessage.senderDisplayName) {
                if (originalChatMessage.sender.kind === 'communicationUser' && originalChatMessage.sender.communicationUserId && cachedDisplayName[originalChatMessage.sender.communicationUserId]) {
                    originalChatMessage.senderDisplayName = cachedDisplayName[originalChatMessage.sender.communicationUserId];
                }
                else if (originalChatMessage.sender.kind === 'microsoftTeamsUser' && originalChatMessage.sender.rawId && cachedDisplayName[originalChatMessage.sender.rawId]) {
                    originalChatMessage.senderDisplayName = cachedDisplayName[originalChatMessage.sender.rawId];
                }
                else if (originalChatMessage.sender.kind === 'phoneNumber' && originalChatMessage.sender.phoneNumber && cachedDisplayName[originalChatMessage.sender.phoneNumber]) {
                    originalChatMessage.senderDisplayName = cachedDisplayName[originalChatMessage.sender.phoneNumber];
                }
                else if (originalChatMessage.sender.kind === 'unknown' && originalChatMessage.sender.id && cachedDisplayName[originalChatMessage.sender.id]) {
                    originalChatMessage.senderDisplayName = cachedDisplayName[originalChatMessage.sender.id];
                }
                else if (originalChatMessage.sender.kind === 'microsoftTeamsApp' && originalChatMessage.sender.rawId && cachedDisplayName[originalChatMessage.sender.rawId]) {
                    originalChatMessage.senderDisplayName = cachedDisplayName[originalChatMessage.sender.rawId];
                }
            }
            return Object.assign({}, originalChatMessage);
        });
        return chatMessagesModifier(modifiedParticipantState);
    };
};
/**
 * @private
 * This is the util function to create a participant modifier for remote participantList
 * It memoize previous original participant items and only update the changed participant
 * It takes in one modifier function to generate one single participant object, it returns undefined if the object keeps unmodified
 */
export const createParticipantModifier = (createModifiedParticipant) => {
    let previousParticipantState = undefined;
    let modifiedParticipants = {};
    const memoizedParticipants = {};
    return (state) => {
        var _a, _b, _c, _d;
        // if root state is the same, we don't need to update the participants
        if (((_a = state.thread) === null || _a === void 0 ? void 0 : _a.participants) !== previousParticipantState) {
            modifiedParticipants = {};
            const originalParticipants = Object.entries(((_b = state.thread) === null || _b === void 0 ? void 0 : _b.participants) || {});
            for (const [key, originalParticipant] of originalParticipants) {
                const modifiedParticipant = createModifiedParticipant(key, originalParticipant);
                if (modifiedParticipant === undefined) {
                    modifiedParticipants[key] = originalParticipant;
                    continue;
                }
                // Generate the new item if original cached item has been changed
                if (((_c = memoizedParticipants[key]) === null || _c === void 0 ? void 0 : _c.originalRef) !== originalParticipant) {
                    memoizedParticipants[key] = {
                        newParticipant: modifiedParticipant,
                        originalRef: originalParticipant
                    };
                }
                // the modified participant is always coming from the memoized cache, whether is was refreshed
                // from the previous closure or not
                const memoizedParticipant = memoizedParticipants[key];
                if (!memoizedParticipant) {
                    throw new Error('Participant modifier encountered an unhandled exception.');
                }
                modifiedParticipants[key] = memoizedParticipant.newParticipant;
            }
            previousParticipantState = (_d = state.thread) === null || _d === void 0 ? void 0 : _d.participants;
        }
        return Object.assign(Object.assign({}, state), { thread: Object.assign(Object.assign({}, state.thread), { participants: modifiedParticipants }) });
    };
};
/**
 * @private
 * This is the util function to create a chat message modifier for remote participantList
 * It memoize previous original messages and only update the changed sender display name
 * It takes in one modifier function to generate one single participant object, it returns undefined if the object keeps unmodified
 */
export const createChatMessageModifier = (createModifiedChatMessage) => {
    let previousChatMessages;
    let modifiedChatMessages;
    const memoizedChatMessages = {};
    return (state) => {
        var _a, _b, _c, _d;
        if (((_a = state.thread) === null || _a === void 0 ? void 0 : _a.chatMessages) !== previousChatMessages) {
            modifiedChatMessages = {};
            const originalChatMessages = Object.entries(((_b = state.thread) === null || _b === void 0 ? void 0 : _b.chatMessages) || {});
            for (const [key, originalChatMessage] of originalChatMessages) {
                const modifiedChatMessage = createModifiedChatMessage(key, originalChatMessage);
                if (modifiedChatMessage === undefined) {
                    modifiedChatMessages[key] = originalChatMessage;
                    continue;
                }
                // Generate the new item if original cached item has been changed
                if (((_c = memoizedChatMessages[key]) === null || _c === void 0 ? void 0 : _c.originalRef) !== originalChatMessage) {
                    memoizedChatMessages[key] = {
                        newChatMessage: modifiedChatMessage,
                        originalRef: originalChatMessage
                    };
                }
                // the modified chat message is always coming from the memoized cache, whether is was refreshed
                // from the previous closure or not
                const memoizedChatMessage = memoizedChatMessages[key];
                if (!memoizedChatMessage) {
                    throw new Error('Participant modifier encountered an unhandled exception.');
                }
                modifiedChatMessages[key] = memoizedChatMessage.newChatMessage;
            }
            previousChatMessages = (_d = state.thread) === null || _d === void 0 ? void 0 : _d.chatMessages;
        }
        return Object.assign(Object.assign({}, state), { thread: Object.assign(Object.assign({}, state.thread), { chatMessages: modifiedChatMessages }) });
    };
};
//# sourceMappingURL=OnFetchProfileCallback.js.map