"use strict";
// =============================================================================
// Pure Agent - Gateway Configuration
// Ported from Hermes gateway/config.py
// =============================================================================
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Platform = void 0;
exports.defaultGatewayConfig = defaultGatewayConfig;
exports.loadGatewayConfig = loadGatewayConfig;
exports.getConnectedPlatforms = getConnectedPlatforms;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const constants_1 = require("../constants");
const logger_1 = require("../logger");
// ---------------------------------------------------------------------------
// Platform enum
// ---------------------------------------------------------------------------
var Platform;
(function (Platform) {
    Platform["LOCAL"] = "local";
    Platform["TELEGRAM"] = "telegram";
    Platform["DISCORD"] = "discord";
    Platform["WHATSAPP"] = "whatsapp";
    Platform["SLACK"] = "slack";
    Platform["SIGNAL"] = "signal";
    Platform["MATTERMOST"] = "mattermost";
    Platform["MATRIX"] = "matrix";
    Platform["EMAIL"] = "email";
    Platform["WEBHOOK"] = "webhook";
    Platform["DINGTALK"] = "dingtalk";
    Platform["FEISHU"] = "feishu";
    Platform["WECOM"] = "wecom";
    Platform["WEIXIN"] = "weixin";
    Platform["QQBOT"] = "qqbot";
    Platform["YUANBAO"] = "yuanbao";
    Platform["API_SERVER"] = "api_server";
})(Platform || (exports.Platform = Platform = {}));
// ---------------------------------------------------------------------------
// Default config
// ---------------------------------------------------------------------------
function defaultGatewayConfig() {
    return {
        platforms: {},
        defaultResetPolicy: {
            mode: 'both',
            atHour: 4,
            idleMinutes: 1440,
            notify: true,
        },
        streaming: {
            enabled: false,
            transport: 'edit',
            editInterval: 0.8,
            bufferThreshold: 24,
            cursor: ' ▉',
        },
        sessionsDir: path.join((0, constants_1.getPureAgentHome)(), 'sessions'),
        groupSessionsPerUser: true,
        threadSessionsPerUser: false,
        sessionStoreMaxAgeDays: 90,
    };
}
// ---------------------------------------------------------------------------
// Load config
// ---------------------------------------------------------------------------
function loadGatewayConfig() {
    const configPath = path.join((0, constants_1.getPureAgentHome)(), 'gateway.json');
    const config = defaultGatewayConfig();
    try {
        if (fs.existsSync(configPath)) {
            const raw = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
            // Merge with defaults
            if (raw.platforms) {
                for (const [key, val] of Object.entries(raw.platforms)) {
                    const platform = key;
                    config.platforms[platform] = {
                        enabled: val.enabled ?? false,
                        token: val.token,
                        apiKey: val.api_key,
                        replyToMode: val.reply_to_mode || 'first',
                        extra: val.extra || {},
                    };
                }
            }
            if (raw.streaming)
                Object.assign(config.streaming, raw.streaming);
            if (raw.sessions_dir)
                config.sessionsDir = raw.sessions_dir;
        }
    }
    catch (err) {
        (0, logger_1.getLogger)().warn(`Failed to load gateway config: ${err.message}`);
    }
    // Apply env var overrides
    applyEnvOverrides(config);
    return config;
}
function makePlatformConfig(overrides) {
    return {
        enabled: overrides.enabled ?? false,
        token: overrides.token,
        apiKey: overrides.apiKey,
        homeChannel: overrides.homeChannel,
        replyToMode: overrides.replyToMode ?? 'first',
        extra: overrides.extra ?? {},
    };
}
function applyEnvOverrides(config) {
    // Telegram
    if (process.env.TELEGRAM_BOT_TOKEN) {
        config.platforms[Platform.TELEGRAM] = makePlatformConfig({
            ...config.platforms[Platform.TELEGRAM],
            enabled: true,
            token: process.env.TELEGRAM_BOT_TOKEN,
        });
    }
    // Discord
    if (process.env.DISCORD_BOT_TOKEN) {
        config.platforms[Platform.DISCORD] = makePlatformConfig({
            ...config.platforms[Platform.DISCORD],
            enabled: true,
            token: process.env.DISCORD_BOT_TOKEN,
        });
    }
    // Slack
    if (process.env.SLACK_BOT_TOKEN) {
        config.platforms[Platform.SLACK] = makePlatformConfig({
            ...config.platforms[Platform.SLACK],
            enabled: true,
            token: process.env.SLACK_BOT_TOKEN,
        });
    }
    // Webhook
    if (process.env.WEBHOOK_PORT || process.env.WEBHOOK_ENABLED) {
        config.platforms[Platform.WEBHOOK] = makePlatformConfig({
            ...config.platforms[Platform.WEBHOOK],
            enabled: true,
        });
    }
}
function getConnectedPlatforms(config) {
    return Object.entries(config.platforms)
        .filter(([, cfg]) => cfg?.enabled)
        .map(([platform]) => platform);
}
//# sourceMappingURL=config.js.map