// 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 { toFlatCommunicationIdentifier } from "../../../../../acs-ui-common/src";
import memoizeOne from 'memoize-one';
import { useAdapter } from '../adapter/CallAdapterProvider';
import { isCameraOn } from '../utils';
import { _toCommunicationIdentifier } from "../../../../../acs-ui-common/src";
import { useSelector } from './useSelector';
import { getCapabilites } from '../selectors/baseSelectors';
/**
 * Private implementation of the hook
 * @private
 */
export const useHandlers = (_component) => {
    const adapter = useAdapter();
    const capabilities = useSelector(getCapabilites);
    return createCompositeHandlers(adapter, capabilities);
};
const createCompositeHandlers = memoizeOne((adapter, capabilities) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    return {
        onCreateLocalStreamView: (options) => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.createStreamView(undefined, options);
        }),
        onCreateRemoteStreamView: (userId, options) => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.createStreamView(userId, options);
        }),
        onHangUp: (forEveryone) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.leaveCall(forEveryone);
        }),
        onToggleHold: () => __awaiter(void 0, void 0, void 0, function* () {
            var _j;
            return ((_j = adapter.getState().call) === null || _j === void 0 ? void 0 : _j.state) === 'LocalHold' ? yield adapter.resumeCall() : yield adapter.holdCall();
        }),
        onAddParticipant: (participant, options) => __awaiter(void 0, void 0, void 0, function* () {
            if ('communicationUserId' in participant) {
                return yield adapter.addParticipant(participant);
            }
            else if ('phoneNumber' in participant) {
                return yield adapter.addParticipant(participant, options);
            }
        }),
        onSendDtmfTone: (dtmfTone) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.sendDtmfTone(dtmfTone);
        }),
        onRemoveParticipant: (userId) => __awaiter(void 0, void 0, void 0, function* () {
            if (typeof userId === 'string') {
                yield adapter.removeParticipant(userId);
            }
            else {
                yield adapter.removeParticipant(_toCommunicationIdentifier(userId));
            }
        }),
        onRaiseHand: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.raiseHand();
        }),
        onLowerHand: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.lowerHand();
        }),
        onToggleRaiseHand: () => __awaiter(void 0, void 0, void 0, function* () {
            var _k;
            if ((_k = adapter.getState().call) === null || _k === void 0 ? void 0 : _k.raiseHand.localParticipantRaisedHand) {
                yield adapter.lowerHand();
            }
            else {
                yield adapter.raiseHand();
            }
        }),
        onReactionClick: (reaction) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.onReactionClick(reaction);
        }),
        onSelectCamera: (deviceInfo, options) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.setCamera(deviceInfo, options);
        }),
        onSelectMicrophone: (deviceInfo) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.setMicrophone(deviceInfo);
        }),
        onSelectSpeaker: (deviceInfo) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.setSpeaker(deviceInfo);
        }),
        onStartCall: (participants, options) => {
            const rawIds = participants.map(participant => toFlatCommunicationIdentifier(participant));
            return adapter.startCall(rawIds, options);
        },
        onStartScreenShare: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.startScreenShare();
        }),
        onStopScreenShare: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.stopScreenShare();
        }),
        onToggleCamera: (options) => __awaiter(void 0, void 0, void 0, function* () {
            isCameraOn(adapter.getState()) ? yield adapter.stopCamera() : yield adapter.startCamera(options);
        }),
        onToggleMicrophone: () => __awaiter(void 0, void 0, void 0, function* () {
            var _l;
            return ((_l = adapter.getState().call) === null || _l === void 0 ? void 0 : _l.isMuted) ? yield adapter.unmute() : yield adapter.mute();
        }),
        onToggleScreenShare: () => __awaiter(void 0, void 0, void 0, function* () {
            var _m;
            return ((_m = adapter.getState().call) === null || _m === void 0 ? void 0 : _m.isScreenSharingOn) ? yield adapter.stopScreenShare() : yield adapter.startScreenShare();
        }),
        onStartLocalVideo: () => __awaiter(void 0, void 0, void 0, function* () {
            if (adapter.getState().call) {
                return adapter.startCamera();
            }
        }),
        onDisposeLocalStreamView: () => __awaiter(void 0, void 0, void 0, function* () {
            return adapter.disposeLocalVideoStreamView();
        }),
        onDisposeRemoteStreamView: (userId) => __awaiter(void 0, void 0, void 0, function* () {
            return adapter.disposeStreamView(userId);
        }),
        onDisposeRemoteScreenShareStreamView: (userId) => __awaiter(void 0, void 0, void 0, function* () {
            return adapter.disposeScreenShareStreamView(userId);
        }),
        onDisposeLocalScreenShareStreamView: () => __awaiter(void 0, void 0, void 0, function* () {
            return adapter.disposeScreenShareStreamView('');
        }),
        onDisposeRemoteVideoStreamView: (userId) => __awaiter(void 0, void 0, void 0, function* () {
            return adapter.disposeRemoteVideoStreamView(userId);
        }),
        onRemoveVideoBackgroundEffects: () => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.stopVideoBackgroundEffects();
        }),
        onBlurVideoBackground: (backgroundBlurConfig) => __awaiter(void 0, void 0, void 0, function* () {
            const blurConfig = Object.assign({ effectName: 'blur' }, backgroundBlurConfig);
            return yield adapter.startVideoBackgroundEffect(blurConfig);
        }),
        onReplaceVideoBackground: (backgroundReplacementConfig) => __awaiter(void 0, void 0, void 0, function* () {
            const replacementConfig = Object.assign({ effectName: 'replacement' }, backgroundReplacementConfig);
            return yield adapter.startVideoBackgroundEffect(replacementConfig);
        }),
        onStartNoiseSuppressionEffect: () => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.startNoiseSuppressionEffect();
        }),
        onStopNoiseSuppressionEffect: () => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.stopNoiseSuppressionEffect();
        }),
        onStartCaptions: (options) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.startCaptions(options);
        }),
        onStopCaptions: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.stopCaptions();
        }),
        onSetSpokenLanguage: (language) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.setSpokenLanguage(language);
        }),
        onSetCaptionLanguage: (language) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.setCaptionLanguage(language);
        }),
        onSubmitSurvey: (survey) => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.submitSurvey(survey);
        }),
        onStartSpotlight: (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.startSpotlight(userIds);
        }),
        onStopSpotlight: (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.stopSpotlight(userIds);
        }),
        onStopAllSpotlight: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.stopAllSpotlight();
        }),
        onStartLocalSpotlight: (capabilities === null || capabilities === void 0 ? void 0 : capabilities.spotlightParticipant.isPresent) ? () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.startSpotlight();
        }) : undefined,
        onStopLocalSpotlight: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.stopSpotlight();
        }),
        onStartRemoteSpotlight: (capabilities === null || capabilities === void 0 ? void 0 : capabilities.spotlightParticipant.isPresent) ? (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.startSpotlight(userIds);
        }) : undefined,
        onStopRemoteSpotlight: (capabilities === null || capabilities === void 0 ? void 0 : capabilities.removeParticipantsSpotlight.isPresent) ? (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.stopSpotlight(userIds);
        }) : undefined,
        onMuteParticipant: (userId) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.muteParticipant(userId);
        }),
        onMuteAllRemoteParticipants: () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.muteAllRemoteParticipants();
        }),
        onForbidAudio: ((_a = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersAudio) === null || _a === void 0 ? void 0 : _a.isPresent) ? (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.forbidAudio(userIds);
        }) : undefined,
        onPermitAudio: ((_b = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersAudio) === null || _b === void 0 ? void 0 : _b.isPresent) ? (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.permitAudio(userIds);
        }) : undefined,
        onForbidOthersAudio: ((_c = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersAudio) === null || _c === void 0 ? void 0 : _c.isPresent) ? () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.forbidOthersAudio();
        }) : undefined,
        onPermitOthersAudio: ((_d = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersAudio) === null || _d === void 0 ? void 0 : _d.isPresent) ? () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.permitOthersAudio();
        }) : undefined,
        onForbidVideo: ((_e = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersVideo) === null || _e === void 0 ? void 0 : _e.isPresent) ? (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.forbidVideo(userIds);
        }) : undefined,
        onPermitVideo: ((_f = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersVideo) === null || _f === void 0 ? void 0 : _f.isPresent) ? (userIds) => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.permitVideo(userIds);
        }) : undefined,
        onForbidOthersVideo: ((_g = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersVideo) === null || _g === void 0 ? void 0 : _g.isPresent) ? () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.forbidOthersVideo();
        }) : undefined,
        onPermitOthersVideo: ((_h = capabilities === null || capabilities === void 0 ? void 0 : capabilities.forbidOthersVideo) === null || _h === void 0 ? void 0 : _h.isPresent) ? () => __awaiter(void 0, void 0, void 0, function* () {
            yield adapter.permitOthersVideo();
        }) : undefined,
        onCreateTogetherModeStreamView: (options) => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.createTogetherModeStreamView(options);
        }),
        onStartTogetherMode: () => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.startTogetherMode();
        }),
        onSetTogetherModeSceneSize: (width, height) => {
            return adapter.setTogetherModeSceneSize(width, height);
        },
        onDisposeTogetherModeStreamView: () => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.disposeTogetherModeStreamView();
        }),
        onSendRealTimeText: (text, isFinalized) => __awaiter(void 0, void 0, void 0, function* () {
            return yield adapter.sendRealTimeText(text, isFinalized);
        })
    };
});
//# sourceMappingURL=useHandlers.js.map