// 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());
    });
};
import { Features } from '@azure/communication-calling';
import { isCommunicationUserIdentifier, isMicrosoftTeamsAppIdentifier } from '@azure/communication-common';
import { _toCommunicationIdentifier } from "../../../acs-ui-common/src";
import memoizeOne from 'memoize-one';
import { isTeamsCallParticipants } from '../utils/callUtils';
import { createLocalVideoStream } from '../utils/callUtils';
import { createDefaultCommonCallingHandlers } from './createCommonHandlers';
/**
 * Create the default implementation of {@link TeamsCallingHandlers} for teams call.
 *
 * Useful when implementing a custom component that utilizes the providers
 * exported from this library.
 *
 * @public
 */
export const createDefaultTeamsCallingHandlers = memoizeOne((callClient, callAgent, deviceManager, call, options) => {
    return Object.assign(Object.assign({}, createDefaultCommonCallingHandlers(callClient, deviceManager, call, options)), { onStartCall: (participants, options) => {
            if (!isTeamsCallParticipants(participants)) {
                throw new Error('CommunicationIdentifier in Teams call is not supported!');
            }
            if (callAgent) {
                // Remove when teams identity in stable support multiple participants
                return teamsSingleParticipantTrampoline(callAgent, participants, options);
            }
            return undefined;
        }, onRemoveParticipant: (userId) => __awaiter(void 0, void 0, void 0, function* () {
            const participant = _toCommunicationIdentifier(userId);
            if (isCommunicationUserIdentifier(participant)) {
                throw new Error('CommunicationIdentifier in Teams call is not supported!');
            }
            if (isMicrosoftTeamsAppIdentifier(participant)) {
                throw new Error('Removing Microsoft Teams app identifier is not supported!');
            }
            yield (call === null || call === void 0 ? void 0 : call.removeParticipant(participant));
        }), onAcceptCall: (incomingCallId, useVideo) => __awaiter(void 0, void 0, void 0, function* () {
            const localVideoStream = useVideo ? yield createLocalVideoStream(callClient) : undefined;
            const incomingCall = callAgent === null || callAgent === void 0 ? void 0 : callAgent.incomingCalls.find((incomingCall) => incomingCall.id === incomingCallId);
            if (incomingCall) {
                yield incomingCall.accept(localVideoStream ? {
                    videoOptions: {
                        localVideoStreams: [localVideoStream]
                    }
                } : undefined);
            }
        }), onRejectCall: (incomingCallId) => __awaiter(void 0, void 0, void 0, function* () {
            const incomingCall = callAgent === null || callAgent === void 0 ? void 0 : callAgent.incomingCalls.find((incomingCall) => incomingCall.id === incomingCallId);
            if (incomingCall) {
                yield incomingCall.reject();
            }
        }), onStartTogetherMode: () => __awaiter(void 0, void 0, void 0, function* () {
            if (!call) {
                return;
            }
            const callState = callClient.getState().calls[call.id];
            if (!callState) {
                return;
            }
            const togetherModeFeature = call === null || call === void 0 ? void 0 : call.feature(Features.TogetherMode);
            yield (togetherModeFeature === null || togetherModeFeature === void 0 ? void 0 : togetherModeFeature.start());
        }) });
});
/**
 * Create a set of default handlers for given component. Memoization is applied to the result. Multiple invocations with
 * the same arguments will return the same handler instances. DeclarativeCallAgent, DeclarativeDeviceManager, and
 * DeclarativeCall may be undefined. If undefined, their associated handlers will not be created and returned.
 *
 * @param callClient - StatefulCallClient returned from
 *   {@link @azure/communication-react#createStatefulCallClient}.
 * @param callAgent - Instance of {@link @azure/communication-calling#TeamsCallClient}.
 * @param deviceManager - Instance of {@link @azure/communication-calling#DeviceManager}.
 * @param call - Instance of {@link @azure/communication-calling#TeamsCall}.
 * @param _ - React component that you want to generate handlers for.
 *
 * @public
 */
export const createTeamsCallingHandlersForComponent = (callClient, callAgent, deviceManager, call, _Component) => {
    return createDefaultTeamsCallingHandlers(callClient, callAgent, deviceManager, call);
};
const teamsSingleParticipantTrampoline = (callAgent, participants, options) => {
    if (participants.length !== 1) {
        throw new Error('Only one participant is supported in Teams call!');
    }
    else {
        return callAgent.startCall(participants[0], options);
    }
};
//# sourceMappingURL=createTeamsCallHandlers.js.map