"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");
const logger_1 = require("../logger");
/**
 * Fire-and-forget publish helper. Swallows errors (best-effort).
 */
function fireAndForget(event) {
    if (!index_1.eventRegistry.hasAdapters)
        return Promise.resolve();
    logger_1.loggerRegistry.info(`[lt-pub] ${event.type} ${event.workflowId || ''} ${event.escalationId || event.taskId || ''}`);
    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: `system.milestone.${params.workflowId}`,
        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.
 * Subject: system.task.{taskId}.{action}
 */
function publishTaskEvent(params) {
    const action = params.type.split('.')[1]; // created, started, completed, escalated, failed
    return fireAndForget({
        type: `system.task.${params.taskId}.${action}`,
        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.
 * Subject: system.escalation.{escalationId}.{action}
 */
function publishEscalationEvent(params) {
    const action = params.type.split('.')[1];
    return fireAndForget({
        type: `system.escalation.${params.escalationId}.${action}`,
        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.
 * Subject: system.activity.{workflowId}.{activityName}.{action}
 */
function publishActivityEvent(params) {
    const action = params.type.split('.')[1];
    return fireAndForget({
        type: `system.activity.${params.workflowId}.${params.activityName}.${action}`,
        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.
 * Subject: system.knowledge.{domain}.{action}
 */
function publishKnowledgeEvent(params) {
    const action = params.type.split('.')[1];
    return fireAndForget({
        type: `system.knowledge.${params.domain}.${action}`,
        source: 'knowledge',
        workflowId: '',
        workflowName: '',
        taskQueue: '',
        data: { domain: params.domain, key: params.key },
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish a file storage event.
 * Subject: system.file.{action}
 */
function publishFileEvent(params) {
    const action = params.type.split('.')[1];
    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: `system.file.${action}`,
        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.
 * Subject: system.agent.{agentName}.{action}
 */
function publishAgentEvent(params) {
    const action = params.type.replace('agent.', '');
    return fireAndForget({
        type: `system.agent.${params.agentName}.${action}`,
        source: 'agent',
        workflowId: params.agentId,
        workflowName: params.agentName,
        taskQueue: '',
        status: params.status,
        data: params.data,
        timestamp: new Date().toISOString(),
    });
}
/**
 * Publish a workflow lifecycle event.
 * Subject: system.workflow.{workflowId}.{action}
 */
function publishWorkflowEvent(params) {
    const action = params.type.split('.')[1];
    return fireAndForget({
        type: `system.workflow.${params.workflowId}.${action}`,
        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(),
    });
}
