import { DataSourceDecorator } from '../../shared/framework';
// @ts-ignore
import { CometChat } from '@cometchat-pro/react-native-chat';
import { ReceiverTypeConstants } from '../../shared/constants/UIKitConstants';
import { ViewAlignment } from '../../shared/constants/UIKitConstants';
import { ExtensionConstants } from '../ExtensionConstants';
import { getExtentionData } from '../ExtensionModerator';
// @ts-ignore
import React from 'react';
// @ts-ignore
import { CometChatUIEvents, MessageEvents } from '../../shared/events';
import { SmartRepliesView } from './SmartRepliesView';
import { getUnixTimestamp, } from '../../shared/utils/CometChatMessageHelper';
import { CometChatUIEventHandler } from '../../shared/events/CometChatUIEventHandler/CometChatUIEventHandler';
import { CometChatUIKit } from '../../shared/CometChatUiKit/CometChatUIKit';
export class SmartRepliesDecorator extends DataSourceDecorator {
    smartRepliesConfiguration;
    messageListenerId = 'message_' + new Date().getTime();
    loggedInUser;
    constructor(dataSource, smartRepliesConfiguration) {
        super(dataSource);
        if (smartRepliesConfiguration != undefined) {
            this.smartRepliesConfiguration = smartRepliesConfiguration;
        }
        CometChat.getLoggedinUser()
            .then((u) => {
            this.loggedInUser = u;
        })
            .catch((err) => console.log(err));
        CometChat.addMessageListener(this.messageListenerId, new CometChat.MessageListener({
            onTextMessageReceived: (textMessage) => {
                this.getReplies(textMessage);
            },
        }));
        CometChatUIEventHandler.addMessageListener(MessageEvents.ccActiveChatChanged, {
            ccActiveChatChanged: ({ message }) => {
                if (message['sender']?.['uid'] != this.loggedInUser.getUid())
                    this.getReplies(message);
            },
        });
    }
    isDeletedMessage(message) {
        return message.getDeletedBy() != null;
    }
    getId() {
        return 'SmartReply';
    }
    getReplies(message) {
        const smartReplyData = getExtentionData(message, ExtensionConstants.smartReply);
        let options = [];
        if (smartReplyData &&
            Object.keys(smartReplyData).length &&
            !smartReplyData.hasOwnProperty('error')) {
            options.push(smartReplyData['reply_positive']);
            options.push(smartReplyData['reply_neutral']);
            options.push(smartReplyData['reply_negative']);
        }
        CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.showPanel, {
            alignment: ViewAlignment.messageListBottom,
            child: () => (<SmartRepliesView replies={options} onClose={this.onCloseRepliesPannel} onClick={(smartReply) => {
                    this.handleSendMessage(message, smartReply);
                }} {...this.smartRepliesConfiguration}/>),
        });
    }
    handleSendMessage = (message, smartReply) => {
        let chatWithId = '';
        let chatWith;
        if (!smartReply.trim().length) {
            return;
        }
        if (typeof message !== 'object')
            return;
        if (message?.receiverType === ReceiverTypeConstants.user) {
            chatWithId = message?.sender?.uid;
            chatWith = ReceiverTypeConstants.user;
        }
        else {
            chatWithId = message?.receiverId;
            chatWith = ReceiverTypeConstants.group;
        }
        let textMessage = new CometChat.TextMessage(chatWithId, smartReply, chatWith);
        textMessage.setParentMessageId(message.getParentMessageId());
        textMessage.setSender(this.loggedInUser);
        textMessage.setText(smartReply);
        textMessage.setSentAt(getUnixTimestamp());
        textMessage.setMuid(String(getUnixTimestamp()));
        CometChatUIKit.sendTextMessage(textMessage, () => { }, () => { });
    };
    onCloseRepliesPannel = () => {
        CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.hidePanel, {
            alignment: ViewAlignment.messageListBottom,
        });
    };
}
//# sourceMappingURL=SmartRepliesDecorator.js.map