import { subscribeDurableMemoryStatus } from '../../app/extensions/durable-memory/status.js';
import { formatToolCallStatus } from './tool-call-status.js';
const STATUS_KEY = 'memory';
const TRANSIENT_STATUS_MS = 12000;
const STATUS_MESSAGE_MAX_CHARS = 100;
/** Format a durable-memory status event into the one-line text shown in the footer. */
function formatStatus(status) {
    const summary = 'message' in status && typeof status.message === 'string' && status.message.trim().length > 0
        ? ` - ${truncateStatusMessage(status.message)}`
        : '';
    switch (status.phase) {
        case 'running':
            return formatToolCallStatus('memory', status.toolCall);
        case 'checked':
            return `memory: checked${summary}`;
        case 'updated':
            return `memory: updated${summary}`;
        case 'error':
            return `memory: error${summary}`;
    }
}
/** Collapse whitespace and truncate the durable-memory status message to fit the footer. */
function truncateStatusMessage(message) {
    const normalized = message.replace(/\s+/g, ' ').trim();
    if (normalized.length <= STATUS_MESSAGE_MAX_CHARS) {
        return normalized;
    }
    return `${normalized.slice(0, STATUS_MESSAGE_MAX_CHARS - 3).trimEnd()}...`;
}
/** Start subscribing to durable-memory status events; returns a teardown function. */
export function startDurableMemoryStatusUpdater(setStatus, requestRender) {
    let clearTimer = null;
    const clear = () => {
        if (clearTimer) {
            clearTimeout(clearTimer);
            clearTimer = null;
        }
        setStatus(STATUS_KEY, undefined);
        requestRender();
    };
    const unsubscribe = subscribeDurableMemoryStatus((status) => {
        if (clearTimer) {
            clearTimeout(clearTimer);
            clearTimer = null;
        }
        setStatus(STATUS_KEY, formatStatus(status));
        requestRender();
        if (status.phase !== 'running') {
            clearTimer = setTimeout(() => {
                clearTimer = null;
                setStatus(STATUS_KEY, undefined);
                requestRender();
            }, TRANSIENT_STATUS_MS);
            clearTimer.unref();
        }
    });
    return () => {
        unsubscribe();
        clear();
    };
}
//# sourceMappingURL=durable-memory-status.js.map