/** Refactor the below to code  to handle the liveagent events and integrate with the chat system ai12z-bot.tsx */
import { LiveAgentProvider } from "../liveagents/liveagent-factory-services";
import { Logger, LogLevel } from "../logger";
export class LiveAgentEventHandler {
    constructor() {
        this.logger = new Logger("LiveAgentEventHandler", {
            logLevel: LogLevel.Warn, // Set log level based on environment or configuration
        });
        /**
         * Called when the `messageReceived` event is emitted.
         * Adds the new message to our state, triggering a UI update.
         */
        this.handleAgentMessage = (message) => {
            let combinesMessage = "";
            if ((message === null || message === void 0 ? void 0 : message.length) > 0) {
                for (const msg of message) {
                    combinesMessage += (msg === null || msg === void 0 ? void 0 : msg.text) + "\n\n";
                }
            }
            return combinesMessage;
        };
        this.sendMessage = (sendMessage) => {
            if (this.liveAgentProvider) {
                this.logger.debug("Sending message to Live Agent:", sendMessage.message);
                this.liveAgentProvider.sendMessage(sendMessage);
            }
            else {
                this.logger.warn("Live Agent provider is not initialized. Cannot send message.");
            }
        };
        this.disconnectLiveAgent = () => {
            if (this.liveAgentProvider) {
                this.logger.debug("Disconnecting from Live Agent");
                this.liveAgentProvider.endChat("");
            }
            else {
                this.logger.warn("Live Agent provider is not initialized. Cannot disconnect.");
            }
        };
    }
    addAgentEventMessage(eventMessages) {
        // I have to return a div element with the agent events as a list
        const entryParent = document.createElement("div");
        eventMessages.forEach((eventMsg) => {
            //  this.addAgentDivEvent(eventMsg, currentDomId)
            const entry = document.createElement("div");
            entry.className = "py-2 border-b border-gray-200 text-center";
            const label = document.createElement("span");
            label.className = "text-[10px] font-medium text-gray-500 tracking-wide"; // "text-xs font-semibold text-gray-600"
            label.textContent = (eventMsg === null || eventMsg === void 0 ? void 0 : eventMsg.text) || "Agent event";
            entry.appendChild(label);
            entryParent.appendChild(entry);
        });
        return entryParent;
    }
    /**
     * Connects to the specified Live Agent provider and sets up event listeners.
     * @param name - The name of the Live Agent provider (e.g., "salesforce", "custom", "ldesk").
     * @param chatOptions - Options for starting the chat session.
     */
    async connectToLiveAgent(name = "salesforce", chatOptions) {
        try {
            this.logger.debug("Connected to Live Agent", name, " --- ", chatOptions);
            switch (name) {
                case "salesforce":
                    this.logger.debug("Using Salesforce Live Agent Provider");
                    this.liveAgentProvider = LiveAgentProvider.getProvider(name);
                    await this.liveAgentProvider.startChat(chatOptions);
                    break;
                case "custom":
                    this.logger.debug("Using Custom Live Agent Provider");
                    break;
                case "liveagent":
                    this.logger.debug("Using LiveAgent Live Agent Provider");
                    break;
                default:
                    throw new Error(`Unsupported Live Agent provider: ${name}`);
            }
        }
        catch (error) {
            this.logger.error("Error connecting to Live Agent:", error);
            throw error;
        }
    }
    /**
     * Resumes an existing live agent session (e.g. after a page reload) using stored
     * session credentials.  Authentication and conversation creation are skipped;
     * only the SSE listener is reconnected.
     */
    async resumeLiveAgentSession(name = "salesforce", chatOptions) {
        try {
            this.logger.debug("Resuming Live Agent session", name, " --- ", chatOptions);
            this.liveAgentProvider = LiveAgentProvider.getProvider(name);
            if (this.liveAgentProvider.resumeChat) {
                await this.liveAgentProvider.resumeChat(chatOptions);
            }
            else {
                throw new Error(`Provider '${name}' does not support session resume`);
            }
        }
        catch (error) {
            this.logger.error("Error resuming Live Agent session:", error);
            throw error;
        }
    }
}
