"use strict";
// =============================================================================
// Pure Agent - Base Platform Adapter
// Ported from Hermes gateway/platforms/base.py
// =============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePlatformAdapter = exports.MessageType = void 0;
const events_1 = require("events");
// ---------------------------------------------------------------------------
// Message types
// ---------------------------------------------------------------------------
var MessageType;
(function (MessageType) {
    MessageType["TEXT"] = "text";
    MessageType["PHOTO"] = "photo";
    MessageType["VIDEO"] = "video";
    MessageType["AUDIO"] = "audio";
    MessageType["VOICE"] = "voice";
    MessageType["DOCUMENT"] = "document";
    MessageType["COMMAND"] = "command";
})(MessageType || (exports.MessageType = MessageType = {}));
// ---------------------------------------------------------------------------
// BasePlatformAdapter - abstract base class
// ---------------------------------------------------------------------------
class BasePlatformAdapter extends events_1.EventEmitter {
    platform;
    _config;
    _messageHandler = null;
    _connected = false;
    constructor(platform, config) {
        super();
        this.platform = platform;
        this._config = config;
    }
    get connected() { return this._connected; }
    setMessageHandler(handler) {
        this._messageHandler = handler;
    }
    // -------------------------------------------------------------------------
    // Optional overridable methods
    // -------------------------------------------------------------------------
    async editMessage(chatId, messageId, content) {
        return { success: false, error: 'editMessage not supported' };
    }
    async deleteMessage(chatId, messageId) {
        return false;
    }
    async sendTyping(chatId) {
        // No-op by default
    }
    async sendImage(chatId, url, caption) {
        // Fallback: send URL as text
        return this.send(chatId, `[Image] ${url}${caption ? ': ' + caption : ''}`);
    }
    async sendDocument(chatId, url, filename) {
        return this.send(chatId, `[Document] ${filename || url}: ${url}`);
    }
    // -------------------------------------------------------------------------
    // Inbound message handling
    // -------------------------------------------------------------------------
    async handleMessage(event) {
        if (!this._messageHandler) {
            return;
        }
        this.emit('message', event);
        try {
            const response = await this._messageHandler(event);
            if (response) {
                await this.send(event.chatId, response, event.messageId);
            }
        }
        catch (err) {
            // Log but don't crash
            this.emit('error', err);
        }
    }
    // -------------------------------------------------------------------------
    // Message chunking
    // ---------------------------------------------------------------------------
    truncateMessage(content, maxLength = 4000) {
        if (content.length <= maxLength)
            return [content];
        const chunks = [];
        let remaining = content;
        while (remaining.length > 0) {
            if (remaining.length <= maxLength) {
                chunks.push(remaining);
                break;
            }
            // Try to split at a newline
            let splitIdx = remaining.lastIndexOf('\n', maxLength);
            if (splitIdx < maxLength * 0.5) {
                splitIdx = maxLength;
            }
            chunks.push(remaining.slice(0, splitIdx));
            remaining = remaining.slice(splitIdx).trimStart();
        }
        return chunks;
    }
}
exports.BasePlatformAdapter = BasePlatformAdapter;
//# sourceMappingURL=base.js.map