import { CometChat } from "@cometchat-pro/react-native-chat";
import { CometChatUIKitHelper } from "./CometChatUIKitHelper";
import { messageStatus } from "../utils/CometChatMessageHelper";
import { CallingExtension } from "../../calls/CallingExtension";
import { CallingPackage } from "../../calls/CallingPackage";
import { StickersExtension } from "../../extensions/Stickers";
import { CollaborativeWhiteboardExtension } from "../../extensions/CollaborativeWhiteboard/CollaborativeWhiteboardExtension";
import { LinkPreviewExtention } from "../../extensions/LinkPreview";
import { ReactionsExtension } from "../../extensions/Reactions";
import { CollaborativeDocumentExtension } from "../../extensions/CollaborativeDocument/CollaborativeDocumentExtension";
import { PollsExtension } from "../../extensions/Polls/PollsExtension";
import { SmartRepliesExtension } from "../../extensions/SmartReplies";
import { ChatConfigurator } from "../framework";
import { ImageModerationExtension } from "../../extensions/ImageModeration";
import { MessageTranslationExtension } from "../../extensions/MessageTranslation";
import { TextModerationExtension } from "../../extensions/TextModeration";
import { ThumbnailGenerationExtension } from "../../extensions/ThumbnailGeneration";
export class CometChatUIKit {
    static uiKitSettings;
    static init(uiKitSettings) {
        //perform sdk init taking values from uiKitSettings
        CometChatUIKit.uiKitSettings = {
            ...uiKitSettings
        };
        var appSetting = new CometChat.AppSettingsBuilder()
            .subscribePresenceForAllUsers()
            .autoEstablishSocketConnection(uiKitSettings.autoEstablishSocketConnection)
            .setRegion(uiKitSettings.region);
        appSetting.subscriptionType = uiKitSettings.subscriptionType;
        return CometChat.init(uiKitSettings.appId, appSetting.build());
    }
    static enableExtensions() {
        ChatConfigurator.init(); //re-initialize data source
        if (!CometChatUIKit.uiKitSettings.disableCalling) {
            if (CallingPackage.isCallingPackageInstalled)
                new CallingExtension().enable();
        }
        if (CometChatUIKit.uiKitSettings.extensions?.length > 0) {
            CometChatUIKit.uiKitSettings.extensions.forEach(extention => {
                extention.enable();
            });
        }
        else {
            new StickersExtension().enable();
            new LinkPreviewExtention().enable();
            new CollaborativeWhiteboardExtension().enable();
            new CollaborativeDocumentExtension().enable();
            new ReactionsExtension().enable();
            new PollsExtension().enable();
            new SmartRepliesExtension().enable();
            new ImageModerationExtension().enable();
            new MessageTranslationExtension().enable();
            new TextModerationExtension().enable();
            new ThumbnailGenerationExtension().enable();
        }
    }
    static async getLoggedInUser() {
        if (CometChatUIKit.checkAuthSettings(Promise.reject))
            null;
        let user = await CometChat.getLoggedinUser().catch((e) => Promise.reject(e));
        if (user == null) {
            Promise.reject(new CometChat.CometChatException({ code: "NOT_FOUND", message: "Login user not found" }));
        }
        else {
            this.enableExtensions();
        }
        return user;
    }
    static async login({ uid, authToken }) {
        if (CometChatUIKit.checkAuthSettings(Promise.reject))
            null;
        if (uid) {
            let user = await CometChat.login(uid, CometChatUIKit.uiKitSettings?.authKey).catch(e => Promise.reject(e));
            this.enableExtensions();
            return user;
        }
        if (authToken) {
            let user = await CometChat.login(authToken).catch(e => Promise.reject(e));
            this.enableExtensions();
            return user;
        }
        return Promise.reject(new CometChat.CometChatException({ code: "INVALID_LOGIN_ATTEMPT", message: "Provide uid or authToken" }));
    }
    static logout() {
        if (this.checkAuthSettings(Promise.reject)) { }
        return CometChat.logout();
    }
    static createUser(user) {
        if (this.checkAuthSettings(Promise.reject)) { }
        return CometChat.createUser(user, this.uiKitSettings.authKey);
    }
    static updateUser(user) {
        if (this.checkAuthSettings(Promise.reject)) { }
        return CometChat.updateUser(user, this.uiKitSettings.authKey);
    }
    //Error handling to give better logs
    static checkAuthSettings(onError) {
        if (this.uiKitSettings == null) {
            if (onError != null) {
                onError(new CometChat.CometChatException({
                    code: "ERR", name: "Authentication null",
                    message: "Populate authSettings before initializing"
                }));
            }
            return false;
        }
        if (!this.uiKitSettings?.appId) {
            if (onError != null) {
                onError(new CometChat.CometChatException({
                    code: "appIdErr", name: "APP ID null",
                    message: "Populate appId in authSettings before initializing"
                }));
            }
            return false;
        }
        return true;
    }
    //---------- Helper methods to send messages ----------
    ///[sendCustomMessage] used to send a custom message
    static sendCustomMessage(message, onSuccess, onError) {
        CometChatUIKitHelper.onMessageSent(message, messageStatus.inprogress);
        CometChat.sendCustomMessage(message)
            .then(customMessage => {
            CometChatUIKitHelper.onMessageSent(customMessage, messageStatus.success);
            onSuccess && onSuccess(customMessage);
        })
            .catch(err => {
            CometChatUIKitHelper.onMessageSent(message, messageStatus.error);
            onError && onError(err);
        });
    }
    ///[sendMediaMessage] used to send a media message
    static sendMediaMessage(message, onSuccess, onError) {
        CometChatUIKitHelper.onMessageSent(message, messageStatus.inprogress);
        CometChat.sendMediaMessage(message)
            .then(mediaMessage => {
            CometChatUIKitHelper.onMessageSent(mediaMessage, messageStatus.success);
            onSuccess && onSuccess(mediaMessage);
        })
            .catch(err => {
            CometChatUIKitHelper.onMessageSent(message, messageStatus.error);
            onError && onError(err);
        });
    }
    ///[sendTextMessage] used to send a text message
    static sendTextMessage(message, onSuccess, onError) {
        CometChatUIKitHelper.onMessageSent(message, messageStatus.inprogress);
        CometChat.sendMessage(message)
            .then(textMessage => {
            CometChatUIKitHelper.onMessageSent(textMessage, messageStatus.success);
            onSuccess && onSuccess(textMessage);
        })
            .catch(err => {
            CometChatUIKitHelper.onMessageSent(message, messageStatus.error);
            onError && onError(err);
        });
    }
}
//# sourceMappingURL=CometChatUIKit.js.map