/**
 * Lucid heartbeat presenter.
 *
 * Adapts heartbeat task, run, and scheduler projections into Lucid-style agent
 * notifications. This is an integration projection, not scheduler policy.
 */
import dayjs from 'dayjs';
export class HeartbeatLucidPresenter {
    static taskStatusToLucidStatus(status) {
        switch (status) {
            case 'running':
                return 'running';
            case 'waiting':
            case 'idle':
                return 'asleep';
            case 'complete':
                return 'terminated';
            case 'blocked':
                return 'blocked';
            case 'failed':
                return 'failed';
        }
    }
    static taskViewToMessages(task, options = {}) {
        const agentId = HeartbeatLucidPresenter.resolveAgentId(task.taskId, options);
        const timestamp = task.state.runAt ?? task.schedule.nextRunAt ?? dayjs().toISOString();
        const messages = [
            HeartbeatLucidPresenter.statusMessage(agentId, HeartbeatLucidPresenter.taskStatusToLucidStatus(task.state.status), timestamp),
        ];
        if (task.state.progress) {
            messages.push(HeartbeatLucidPresenter.progressMessage(agentId, task.state.progress, timestamp));
        }
        if (task.state.result?.summary) {
            messages.push(HeartbeatLucidPresenter.responseMessage(agentId, task.state.result.summary, timestamp));
        }
        return messages;
    }
    static runViewToMessages(run, options = {}) {
        const agentId = HeartbeatLucidPresenter.resolveAgentId(run.taskId, options);
        const timestamp = run.createdAt;
        const messages = [
            HeartbeatLucidPresenter.statusMessage(agentId, HeartbeatLucidPresenter.taskStatusToLucidStatus(run.task.state.status), timestamp),
        ];
        if (run.task.state.progress) {
            messages.push(HeartbeatLucidPresenter.progressMessage(agentId, run.task.state.progress, timestamp));
        }
        messages.push(HeartbeatLucidPresenter.responseMessage(agentId, run.result.summary, timestamp));
        return messages;
    }
    static schedulerEventToMessages(event, options = {}) {
        if (event.type === 'heartbeat.scheduler.started' || event.type === 'heartbeat.scheduler.stopped') {
            return [];
        }
        const agentId = HeartbeatLucidPresenter.resolveAgentId(event.taskId, options);
        switch (event.type) {
            case 'heartbeat.task.due':
                return [];
            case 'heartbeat.task.started':
                return [
                    HeartbeatLucidPresenter.statusMessage(agentId, HeartbeatLucidPresenter.taskStatusToLucidStatus(event.status), event.timestamp),
                    HeartbeatLucidPresenter.progressMessage(agentId, event.progress, event.timestamp),
                ];
            case 'heartbeat.task.agent_event':
                return [];
            case 'heartbeat.task.finished': {
                const { task, result } = event.record;
                return [
                    HeartbeatLucidPresenter.statusMessage(agentId, HeartbeatLucidPresenter.taskStatusToLucidStatus(task.state?.status ?? 'waiting'), event.timestamp),
                    HeartbeatLucidPresenter.progressMessage(agentId, task.state?.progress ?? '', event.timestamp),
                    HeartbeatLucidPresenter.responseMessage(agentId, result.summary, event.timestamp),
                ];
            }
            case 'heartbeat.task.failed':
                return [
                    HeartbeatLucidPresenter.statusMessage(agentId, HeartbeatLucidPresenter.taskStatusToLucidStatus(event.status), event.timestamp),
                    HeartbeatLucidPresenter.progressMessage(agentId, event.progress, event.timestamp),
                    HeartbeatLucidPresenter.responseMessage(agentId, event.error, event.timestamp),
                ];
        }
    }
    static statusMessage(agentId, status, timestamp) {
        return {
            event: 'agent_status',
            data: {
                status: {
                    agent_id: agentId,
                    status,
                    timestamp,
                },
            },
        };
    }
    static progressMessage(agentId, progress, timestamp) {
        return {
            event: 'agent_progress',
            data: {
                progress: {
                    agent_id: agentId,
                    progress,
                    timestamp,
                },
            },
        };
    }
    static responseMessage(agentId, response, timestamp) {
        return {
            event: 'agent_response',
            data: {
                response: {
                    agent_id: agentId,
                    response,
                    timestamp,
                },
            },
        };
    }
    static resolveAgentId(taskId, options) {
        return options.taskIdToAgentId?.(taskId) ?? taskId;
    }
}
//# sourceMappingURL=lucid-presenter.js.map