// Feishu Bot API Client using official SDK with WebSocket long polling
// https://open.feishu.cn/document/server-side-sdk/nodejs-sdk/preparation-before-development
import * as Lark from '@larksuiteoapi/node-sdk';
import { logger as rootLogger } from '../../../core/logger.js';
const log = rootLogger.child({ component: 'feishu-client' });
export class FeishuClient {
    client;
    wsClient;
    eventDispatcher;
    messageHandler;
    constructor(config) {
        // Create base config
        const baseConfig = {
            appId: config.appId,
            appSecret: config.appSecret,
            loggerLevel: Lark.LoggerLevel.info,
        };
        // Initialize API client
        this.client = new Lark.Client(baseConfig);
        // Initialize event dispatcher
        this.eventDispatcher = new Lark.EventDispatcher({});
        // Initialize WebSocket client for long polling
        this.wsClient = new Lark.WSClient(baseConfig);
    }
    // ============================================
    // Event Registration
    // ============================================
    onMessage(handler) {
        this.messageHandler = handler;
    }
    // ============================================
    // API Calls
    // ============================================
    async sendMessage(chatId, text) {
        const response = await this.client.im.message.create({
            params: {
                receive_id_type: 'chat_id',
            },
            data: {
                receive_id: chatId,
                msg_type: 'text',
                content: JSON.stringify({ text }),
            },
        });
        return (response.data ?? {});
    }
    async deleteMessage(messageId) {
        await this.client.im.message.delete({
            path: { message_id: messageId },
        });
    }
    async sendCard(chatId, card) {
        const response = await this.client.im.message.create({
            params: {
                receive_id_type: 'chat_id',
            },
            data: {
                receive_id: chatId,
                msg_type: 'interactive',
                content: JSON.stringify(card),
            },
        });
        return response.data;
    }
    async replyToMessage(messageId, text) {
        const response = await this.client.im.message.reply({
            path: {
                message_id: messageId,
            },
            data: {
                msg_type: 'text',
                content: JSON.stringify({ text }),
            },
        });
        return response.data;
    }
    async replyWithCard(messageId, card) {
        const response = await this.client.im.message.reply({
            path: {
                message_id: messageId,
            },
            data: {
                msg_type: 'interactive',
                content: JSON.stringify(card),
            },
        });
        return response.data;
    }
    // ============================================
    // WebSocket Long Polling
    // ============================================
    async start() {
        // Register event handlers
        this.eventDispatcher.register({
            'im.message.receive_v1': async (data) => {
                if (this.messageHandler) {
                    await this.messageHandler(data);
                }
            },
        });
        // Start WebSocket long polling with event dispatcher
        await this.wsClient.start({
            eventDispatcher: this.eventDispatcher,
        });
        log.info('WebSocket long polling started');
    }
    async stop() {
        this.wsClient.close();
        log.info('WebSocket connection stopped');
    }
    // ============================================
    // Getters
    // ============================================
    get larkClient() {
        return this.client;
    }
}
//# sourceMappingURL=feishu-client.js.map