"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishMilestoneEvent = publishMilestoneEvent;
exports.publishTaskEvent = publishTaskEvent;
exports.publishEscalationEvent = publishEscalationEvent;
exports.publishActivityEvent = publishActivityEvent;
exports.publishKnowledgeEvent = publishKnowledgeEvent;
exports.publishFileEvent = publishFileEvent;
exports.publishAgentEvent = publishAgentEvent;
exports.publishWorkflowEvent = publishWorkflowEvent;
const index_1 = require("./index");
/**
 * Fire-and-forget publish helper. Swallows errors (best-effort).
 */
function fireAndForget(event) {
    if (!index_1.eventRegistry.hasAdapters)
        return Promise.resolve();
    return index_1.eventRegistry.publish(event).catch(() => { });
}
/**
 * Publish a milestone event. Called from handleCompletion (workflow
 * interceptor), ltCompleteTask (orchestrator activity), and the
 * activity interceptor's after phase.
 */
function publishMilestoneEvent(params) {
    if (!params.milestones?.length)
        return Promise.resolve();
    return fireAndForget({
        type: 'milestone',
        source: params.source,
        workflowId: params.workflowId,
        workflowName: params.workflowName,
        taskQueue: params.taskQueue,
        taskId: params.taskId,
        activityName: params.activityName,
        milestones: params.milestones,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish a task lifecycle event (created, started, completed, escalated, failed).
 */
function publishTaskEvent(params) {
    return fireAndForget({
        type: params.type,
        source: params.source,
        workflowId: params.workflowId,
        workflowName: params.workflowName,
        taskQueue: params.taskQueue,
        taskId: params.taskId,
        originId: params.originId,
        status: params.status,
        milestones: params.milestones,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish an escalation lifecycle event (created, resolved).
 */
function publishEscalationEvent(params) {
    return fireAndForget({
        type: params.type,
        source: params.source,
        workflowId: params.workflowId,
        workflowName: params.workflowName,
        taskQueue: params.taskQueue,
        taskId: params.taskId,
        escalationId: params.escalationId,
        originId: params.originId,
        status: params.status,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish an activity lifecycle event for YAML workflow steps.
 * Lightweight — no payload data, just progress signals.
 */
function publishActivityEvent(params) {
    return fireAndForget({
        type: params.type,
        source: 'yaml-worker',
        workflowId: params.workflowId,
        workflowName: params.workflowName,
        taskQueue: params.taskQueue,
        activityName: params.activityName,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish a knowledge lifecycle event (stored, deleted).
 * Lightweight — no workflow context required since knowledge writes
 * happen both inside and outside workflows.
 */
function publishKnowledgeEvent(params) {
    return fireAndForget({
        type: params.type,
        source: 'knowledge',
        workflowId: '',
        workflowName: '',
        taskQueue: '',
        data: { domain: params.domain, key: params.key },
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish a file storage event (stored, deleted).
 * Includes rich metadata: path, name, extension, mime type, size.
 */
function publishFileEvent(params) {
    const parsed = params.path.split('/');
    const filename = parsed[parsed.length - 1] || '';
    const dotIdx = filename.lastIndexOf('.');
    const name = dotIdx > 0 ? filename.slice(0, dotIdx) : filename;
    const extension = dotIdx > 0 ? filename.slice(dotIdx + 1) : '';
    return fireAndForget({
        type: params.type,
        source: 'file-storage',
        workflowId: '',
        workflowName: '',
        taskQueue: '',
        data: {
            path: params.path,
            name,
            extension,
            filename,
            mime: params.mime,
            size: params.size,
        },
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish an agent lifecycle event.
 */
function publishAgentEvent(params) {
    return fireAndForget({
        type: params.type,
        source: 'agent',
        workflowId: params.agentId,
        workflowName: params.agentName,
        taskQueue: '',
        status: params.status,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish a workflow lifecycle event (started, completed, failed).
 */
function publishWorkflowEvent(params) {
    return fireAndForget({
        type: params.type,
        source: params.source,
        workflowId: params.workflowId,
        workflowName: params.workflowName,
        taskQueue: params.taskQueue,
        taskId: params.taskId,
        originId: params.originId,
        status: params.status,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
