// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { getDisplayName, getIdentifier, getStartCaptionsInProgress, getSupportedCaptionLanguages } from './baseSelectors';
import { allRemoteParticipantsSelector } from './remoteParticipantsSelector';
import { getRealTimeTextStatus, getRealTimeText } from './baseSelectors';
import { getCaptions, getCaptionsStatus, getCurrentCaptionLanguage, getCurrentSpokenLanguage, getSupportedSpokenLanguages } from './baseSelectors';
import * as reselect from 'reselect';
import { toFlatCommunicationIdentifier } from "../../acs-ui-common/src";
import { getRemoteParticipantDisplayName } from './utils/callUtils';
/**
 * Selector for {@link StartCaptionsButton} component.
 *
 * @public
 */
export const startCaptionsButtonSelector = reselect.createSelector([getCaptionsStatus, getCurrentCaptionLanguage, getCurrentSpokenLanguage], (isCaptionsFeatureActive, currentCaptionLanguage, currentSpokenLanguage) => {
    return {
        checked: isCaptionsFeatureActive !== null && isCaptionsFeatureActive !== void 0 ? isCaptionsFeatureActive : false,
        currentCaptionLanguage: currentCaptionLanguage !== null && currentCaptionLanguage !== void 0 ? currentCaptionLanguage : '',
        currentSpokenLanguage: currentSpokenLanguage !== null && currentSpokenLanguage !== void 0 ? currentSpokenLanguage : 'en-us'
    };
});
/**
 * Selector for Changing caption language and spoken language
 *
 * @public
 */
export const captionSettingsSelector = reselect.createSelector([getSupportedCaptionLanguages, getCurrentCaptionLanguage, getSupportedSpokenLanguages, getCurrentSpokenLanguage, getCaptionsStatus], (supportedCaptionLanguages, currentCaptionLanguage, supportedSpokenLanguages, currentSpokenLanguage, isCaptionsFeatureActive) => {
    return {
        supportedCaptionLanguages: supportedCaptionLanguages !== null && supportedCaptionLanguages !== void 0 ? supportedCaptionLanguages : [],
        currentCaptionLanguage: currentCaptionLanguage !== null && currentCaptionLanguage !== void 0 ? currentCaptionLanguage : 'en',
        supportedSpokenLanguages: supportedSpokenLanguages !== null && supportedSpokenLanguages !== void 0 ? supportedSpokenLanguages : ['en-us'],
        currentSpokenLanguage: currentSpokenLanguage !== null && currentSpokenLanguage !== void 0 ? currentSpokenLanguage : 'en-us',
        isCaptionsFeatureActive: isCaptionsFeatureActive !== null && isCaptionsFeatureActive !== void 0 ? isCaptionsFeatureActive : false
    };
});
/**
 * Selector for {@link CaptionsBanner} component.
 *
 * @public
 */
export const captionsBannerSelector = reselect.createSelector([getCaptions, getRealTimeText, getCaptionsStatus, getRealTimeTextStatus, getStartCaptionsInProgress, allRemoteParticipantsSelector, getDisplayName, getIdentifier], (captions, realTimeTexts, isCaptionsFeatureActive, isRealTimeTextActive, startCaptionsInProgress, allRemoteParticipants, displayName, identifier) => {
    var _a, _b;
    const captionsInfo = captions === null || captions === void 0 ? void 0 : captions.map((c, index) => {
        const userId = getCaptionsSpeakerIdentifier(c);
        let finalDisplayName;
        if (userId === identifier) {
            finalDisplayName = displayName;
        }
        else {
            finalDisplayName = getRemoteParticipantDisplayName(userId, allRemoteParticipants);
        }
        return {
            id: (finalDisplayName !== null && finalDisplayName !== void 0 ? finalDisplayName : 'Unnamed Participant') + index,
            displayName: finalDisplayName !== null && finalDisplayName !== void 0 ? finalDisplayName : 'Unnamed Participant',
            captionText: c.captionText,
            userId,
            createdTimeStamp: c.timestamp,
            isFinalized: c.resultType === 'Final'
        };
    });
    const completedRealTimeTexts = (_a = realTimeTexts === null || realTimeTexts === void 0 ? void 0 : realTimeTexts.completedMessages) === null || _a === void 0 ? void 0 : _a.filter(rtt => rtt.message !== '').map(rtt => {
        const userId = getRealTimeTextSpeakerIdentifier(rtt);
        return {
            id: rtt.sequenceId,
            displayName: getRealTimeTextDisplayName(rtt, identifier, allRemoteParticipants, displayName, userId),
            message: rtt.message,
            userId,
            isTyping: rtt.resultType === 'Partial',
            isMe: rtt.isMe,
            finalizedTimeStamp: rtt.updatedTimestamp
        };
    });
    const inProgressRealTimeTexts = (_b = realTimeTexts === null || realTimeTexts === void 0 ? void 0 : realTimeTexts.currentInProgress) === null || _b === void 0 ? void 0 : _b.filter(rtt => rtt.message !== '').map(rtt => {
        const userId = getRealTimeTextSpeakerIdentifier(rtt);
        return {
            id: rtt.sequenceId,
            displayName: getRealTimeTextDisplayName(rtt, identifier, allRemoteParticipants, displayName, userId),
            message: rtt.message,
            userId,
            isTyping: rtt.resultType === 'Partial',
            isMe: rtt.isMe,
            finalizedTimeStamp: rtt.updatedTimestamp
        };
    });
    const myInProgress = (realTimeTexts === null || realTimeTexts === void 0 ? void 0 : realTimeTexts.myInProgress) && realTimeTexts.myInProgress.message !== '' ? {
        id: realTimeTexts.myInProgress.sequenceId,
        displayName: displayName,
        message: realTimeTexts.myInProgress.message,
        userId: identifier,
        isTyping: realTimeTexts.myInProgress.resultType === 'Partial',
        isMe: true,
        finalizedTimeStamp: realTimeTexts.myInProgress.updatedTimestamp
    } : undefined;
    // find the last final local real time text caption if myInProgress is not available
    let latestLocalRealTimeText;
    if (!myInProgress) {
        latestLocalRealTimeText = realTimeTexts && realTimeTexts.completedMessages && realTimeTexts.completedMessages.slice().reverse().find(rtt => rtt.isMe);
    }
    return {
        captions: captionsInfo !== null && captionsInfo !== void 0 ? captionsInfo : [],
        realTimeTexts: {
            completedMessages: completedRealTimeTexts,
            currentInProgress: inProgressRealTimeTexts,
            myInProgress: myInProgress
        },
        isCaptionsOn: isCaptionsFeatureActive !== null && isCaptionsFeatureActive !== void 0 ? isCaptionsFeatureActive : false,
        startCaptionsInProgress: startCaptionsInProgress !== null && startCaptionsInProgress !== void 0 ? startCaptionsInProgress : false,
        isRealTimeTextOn: isRealTimeTextActive !== null && isRealTimeTextActive !== void 0 ? isRealTimeTextActive : false,
        latestLocalRealTimeText: (myInProgress !== null && myInProgress !== void 0 ? myInProgress : latestLocalRealTimeText)
    };
});
const getCaptionsSpeakerIdentifier = (captions) => {
    return captions.speaker.identifier ? toFlatCommunicationIdentifier(captions.speaker.identifier) : '';
};
const getRealTimeTextSpeakerIdentifier = (realTimeText) => {
    return realTimeText.sender.identifier ? toFlatCommunicationIdentifier(realTimeText.sender.identifier) : '';
};
const getRealTimeTextDisplayName = (realTimeText, identifier, allRemoteParticipants, displayName, userId) => {
    let finalDisplayName;
    if (userId === identifier) {
        finalDisplayName = displayName;
    }
    else {
        finalDisplayName = getRemoteParticipantDisplayName(userId, allRemoteParticipants);
    }
    return finalDisplayName !== null && finalDisplayName !== void 0 ? finalDisplayName : 'Unnamed Participant';
};
//# sourceMappingURL=captionsSelector.js.map