/**
 * Custom Status Line
 *
 * Configurable status bar with template rendering, script execution,
 * auto-refresh, and model/git/token display.
 */
import { logger } from '../utils/logger.js';
import { execSync } from 'child_process';
// ============================================================================
// Defaults
// ============================================================================
const DEFAULT_CONFIG = {
    enabled: false,
    refreshInterval: 5000,
    position: 'bottom',
};
const DEFAULT_TEMPLATE = '{{model}} | {{gitBranch}} | Tokens: {{tokenUsage}} | Changes: {{uncommittedChanges}}';
// ============================================================================
// Status Line Manager
// ============================================================================
export class StatusLineManager {
    config;
    currentData = {};
    refreshTimer = null;
    constructor(config) {
        this.config = { ...DEFAULT_CONFIG, ...config };
    }
    /**
     * Update status data
     */
    updateData(data) {
        this.currentData = { ...this.currentData, ...data };
    }
    /**
     * Render the status line
     */
    render() {
        if (!this.config.enabled) {
            return '';
        }
        // If there's custom content from a script, use it
        if (this.currentData.customContent) {
            return this.currentData.customContent;
        }
        const template = this.config.template || DEFAULT_TEMPLATE;
        return this.renderTemplate(template, this.currentData);
    }
    /**
     * Render template with placeholders
     */
    renderTemplate(template, data) {
        let result = template;
        result = result.replace(/\{\{model\}\}/g, data.model || 'unknown');
        result = result.replace(/\{\{gitBranch\}\}/g, this.formatGitBranch(data.gitBranch || ''));
        result = result.replace(/\{\{tokenUsage\}\}/g, data.tokenUsage ? this.formatTokenUsage(data.tokenUsage) : '0/0');
        result = result.replace(/\{\{uncommittedChanges\}\}/g, String(data.uncommittedChanges ?? 0));
        result = result.replace(/\{\{sessionId\}\}/g, data.sessionId || '');
        return result;
    }
    /**
     * Execute script for dynamic content
     */
    async executeScript() {
        if (!this.config.script) {
            return '';
        }
        try {
            const output = execSync(this.config.script, {
                timeout: 5000,
                encoding: 'utf-8',
                stdio: ['pipe', 'pipe', 'pipe'],
            });
            return output.trim();
        }
        catch (e) {
            logger.warn(`Status line script error: ${e.message}`);
            return '';
        }
    }
    /**
     * Start auto-refresh
     */
    startRefresh() {
        if (this.refreshTimer)
            return;
        this.refreshTimer = setInterval(() => {
            // In a real implementation, this would trigger a re-render
            logger.debug('Status line refresh tick');
        }, this.config.refreshInterval);
    }
    /**
     * Stop auto-refresh
     */
    stopRefresh() {
        if (this.refreshTimer) {
            clearInterval(this.refreshTimer);
            this.refreshTimer = null;
        }
    }
    /**
     * Check if auto-refreshing
     */
    isRefreshing() {
        return this.refreshTimer !== null;
    }
    /**
     * Format token usage
     */
    formatTokenUsage(usage) {
        const pct = usage.max > 0 ? Math.round((usage.used / usage.max) * 100) : 0;
        return `${usage.used}/${usage.max} (${pct}%)`;
    }
    /**
     * Format git branch
     */
    formatGitBranch(branch) {
        if (!branch)
            return 'no branch';
        return branch;
    }
    /**
     * Enable status line
     */
    enable() {
        this.config.enabled = true;
    }
    /**
     * Disable status line
     */
    disable() {
        this.config.enabled = false;
        this.stopRefresh();
    }
    /**
     * Check if enabled
     */
    isEnabled() {
        return this.config.enabled;
    }
    /**
     * Get current data
     */
    getData() {
        return { ...this.currentData };
    }
    /**
     * Get config
     */
    getConfig() {
        return { ...this.config };
    }
    /**
     * Dispose resources
     */
    dispose() {
        this.stopRefresh();
        this.currentData = {};
    }
}
//# sourceMappingURL=status-line.js.map