import React from "react";
import { CometChat } from "@cometchat/chat-sdk-react-native";
import AIConversationSummaryView from "./AIConversationSummaryView";
import { CometChatTheme, CometChatUIEventHandler, CometChatUIEvents, DataSourceDecorator, MessageEvents, localize } from "../../shared";
import { MessageStatusConstants, ReceiverTypeConstants, ViewAlignment } from "../../shared/constants/UIKitConstants";
export class AIConversationSummaryDecorator extends DataSourceDecorator {
    configuration;
    newDataSource;
    currentMessage = null;
    unreadMessageCount = 0;
    loggedInUser;
    user;
    group;
    theme = new CometChatTheme({});
    LISTENER_ID = "aiconversationsummary__listener";
    constructor(dataSource, configuration) {
        super(dataSource);
        this.newDataSource = dataSource;
        this.configuration = configuration;
        setTimeout(() => {
            this.addMessageListener();
        }, 1000);
    }
    getId() {
        return "aiconversationsummary";
    }
    editReply(reply) {
        CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.ccComposeMessage, { text: reply });
        this.closePanel();
    }
    closePanel = () => {
        CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.hidePanel, {
            alignment: ViewAlignment.composerTop,
            child: () => (null),
        });
    };
    closeIfMessageReceived(message) {
        if (message?.getReceiverId() === this.loggedInUser?.getUid()) {
            this.closePanel();
        }
    }
    getConversationSummary = (theme) => {
        this.theme = theme ?? new CometChatTheme({});
        return new Promise(async (resolve, reject) => {
            try {
                let receiverId = this.user
                    ? this.user?.getUid()
                    : this.group?.getGuid();
                let receiverType = this.user
                    ? ReceiverTypeConstants.user
                    : ReceiverTypeConstants.group;
                let configuration;
                if (this.configuration?.apiConfiguration) {
                    configuration = await this.configuration?.apiConfiguration(this.user, this.group);
                }
                const response = await CometChat.getConversationSummary(receiverId, receiverType, configuration ? configuration : {});
                return resolve(response);
            }
            catch (e) {
                reject(e);
            }
        });
    };
    loadConversationSummary() {
        this.onClose();
        CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.showPanel, {
            alignment: ViewAlignment.messageListBottom,
            child: () => (<AIConversationSummaryView configuration={this.configuration} getConversationSummaryCallback={this.getConversationSummary} theme={this.theme} editReplyCallback={this.editReply} onPanelClose={this.onClose}/>),
        });
    }
    onClose = () => {
        CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.hidePanel, {
            alignment: ViewAlignment.messageListBottom,
            child: () => (null),
        });
    };
    getAIOptions(user, group, theme, id, AIOptionsStyle) {
        this.user = user;
        this.group = group;
        if (!id?.parentMessageId) {
            const messageComposerActions = super.getAIOptions(user, group, theme, id, AIOptionsStyle);
            let newAction = {
                title: localize("GENERATE_SUMMARY"),
                onPress: () => { this.loadConversationSummary(); },
                id: "ai-conversation-summary",
                iconURL: '',
                iconTint: '',
                titleColor: this.configuration?.conversationSummaryStyle?.buttonTextColor || AIOptionsStyle.listItemTitleColor,
                titleFont: this.configuration?.conversationSummaryStyle?.buttonTextFont || AIOptionsStyle.listItemTitleFont,
                background: this.configuration?.conversationSummaryStyle?.backgroundColor || AIOptionsStyle.listItemBackground,
                cornerRadius: this.configuration?.conversationSummaryStyle?.buttonBorderRadius || AIOptionsStyle.listItemBorderRadius,
            };
            messageComposerActions.push(newAction);
            return messageComposerActions;
        }
        else {
            return super.getAIOptions(user, group, theme, id, AIOptionsStyle);
        }
    }
    addMessageListener() {
        CometChat.getLoggedinUser().then((user) => {
            if (user) {
                this.loggedInUser = user;
            }
        });
        CometChatUIEventHandler.addMessageListener(this.LISTENER_ID, {
            onTextMessageReceived: (message) => {
                this.closeIfMessageReceived(message);
            },
            onCustomMessageReceived: (message) => {
                this.closeIfMessageReceived(message);
            },
            onMediaMessageReceived: (message) => {
                this.closeIfMessageReceived(message);
            },
            onFormMessageReceived: (formMessage) => {
                this.closeIfMessageReceived(formMessage);
            },
            onCardMessageReceived: (cardMessage) => {
                this.closeIfMessageReceived(cardMessage);
            },
            onSchedulerMessageReceived: (schedulerMessage) => {
                this.closeIfMessageReceived(schedulerMessage);
            },
            onCustomInteractiveMessageReceived: (customInteractiveMessage) => {
                this.closeIfMessageReceived(customInteractiveMessage);
            }
        });
        CometChatUIEventHandler.addMessageListener(MessageEvents.ccActiveChatChanged, {
            ccActiveChatChanged: (data) => {
                this.currentMessage = data.message;
                this.user = data.user;
                this.group = data.group;
                this.unreadMessageCount = data.unreadMessageCount ?? 0;
                if (this.unreadMessageCount >= (this.configuration?.unreadMessageThreshold ?? 30)) {
                    this.loadConversationSummary();
                }
            },
            ccMessageSent: ({ message, status }) => {
                if (status == MessageStatusConstants.success && message?.sender?.uid == this.loggedInUser?.uid) {
                    this.closePanel();
                    this.currentMessage = null;
                    this.unreadMessageCount = 0;
                }
            }
        });
    }
}
//# sourceMappingURL=AIConversationSummaryDecorator.js.map