// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only
import { KERNEL_EVENT_KINDS } from '@lumenflow/control-plane-sdk';
import { buildKernelEventV2, buildTaskCreatedLegacyEvent, } from './emitters.js';
// ─── V2 builders (11 conductor kinds) ───
export function buildOrchestrationPlanWrittenEvent(input, options) {
    const payload = input.launch_attempt !== undefined
        ? {
            kind: KERNEL_EVENT_KINDS.ORCHESTRATION_PLAN_WRITTEN,
            initiative: input.initiative,
            launch_attempt: input.launch_attempt,
        }
        : {
            kind: KERNEL_EVENT_KINDS.ORCHESTRATION_PLAN_WRITTEN,
            initiative: input.initiative,
        };
    return buildKernelEventV2(payload, options);
}
export function buildOrchestrationLaunchEmittedEvent(input, options) {
    const payload = {
        kind: KERNEL_EVENT_KINDS.ORCHESTRATION_LAUNCH_EMITTED,
        initiative: input.initiative,
        wu_id: input.wu_id,
    };
    if (input.launch_attempt !== undefined) {
        payload.launch_attempt = input.launch_attempt;
    }
    if (input.worktree_path !== undefined) {
        payload.worktree_path = input.worktree_path;
    }
    return buildKernelEventV2(payload, options);
}
export function buildOrchestrationStatusUpdatedEvent(input, options) {
    const payload = {
        kind: KERNEL_EVENT_KINDS.ORCHESTRATION_STATUS_UPDATED,
        initiative: input.initiative,
        wu_id: input.wu_id,
        orchestration_state: input.orchestration_state,
    };
    return buildKernelEventV2(payload, options);
}
export function buildDelegationRecordedEvent(input, options) {
    const payload = input.lane !== undefined
        ? {
            kind: KERNEL_EVENT_KINDS.DELEGATION_RECORDED,
            delegation_id: input.delegation_id,
            parent_wu_id: input.parent_wu_id,
            target_wu_id: input.target_wu_id,
            lane: input.lane,
        }
        : {
            kind: KERNEL_EVENT_KINDS.DELEGATION_RECORDED,
            delegation_id: input.delegation_id,
            parent_wu_id: input.parent_wu_id,
            target_wu_id: input.target_wu_id,
        };
    return buildKernelEventV2(payload, options);
}
export function buildDelegationPickedUpEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.DELEGATION_PICKED_UP,
        delegation_id: input.delegation_id,
        picked_up_by: input.picked_up_by,
    }, options);
}
export function buildBriefEmittedEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.BRIEF_EMITTED,
        wu_id: input.wu_id,
        client_name: input.client_name,
        prompt_hash: input.prompt_hash,
    }, options);
}
export function buildWuBlockedEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.WU_BLOCKED,
        wu_id: input.wu_id,
        reason: input.reason,
    }, options);
}
export function buildWuUnblockedEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.WU_UNBLOCKED,
        wu_id: input.wu_id,
    }, options);
}
export function buildWuReleasedEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.WU_RELEASED,
        wu_id: input.wu_id,
        reason: input.reason,
    }, options);
}
export function buildLockAcquiredEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.LOCK_ACQUIRED,
        lock_id: input.lock_id,
        held_by: input.held_by,
    }, options);
}
export function buildLockReleasedEvent(input, options) {
    return buildKernelEventV2({
        kind: KERNEL_EVENT_KINDS.LOCK_RELEASED,
        lock_id: input.lock_id,
    }, options);
}
// ─── V1 legacy builder (task_created with stamped event_id) ───
export function buildLegacyTaskCreatedEvent(input, options) {
    return buildTaskCreatedLegacyEvent({
        task_id: input.task_id,
        spec_hash: input.spec_hash,
    }, options);
}
// ─── Fail-silent sink resolver (mirrors WU-2643 pattern) ───
/**
 * Read `control_plane.org_id` off a workspace config snapshot. Returns
 * `undefined` when the field is missing, non-string, or whitespace-only —
 * callers use that as the "skip all emissions" signal for local-only
 * workspaces.
 */
export function readOrgIdFromConfig(config) {
    if (!config || typeof config !== 'object') {
        return undefined;
    }
    const controlPlane = config.control_plane;
    if (!controlPlane || typeof controlPlane !== 'object') {
        return undefined;
    }
    const raw = controlPlane.org_id;
    if (typeof raw !== 'string') {
        return undefined;
    }
    const trimmed = raw.trim();
    return trimmed.length > 0 ? trimmed : undefined;
}
/**
 * Resolve the conductor sink for call-site emissions. The sink pushes through
 * the control-plane SDK when workspace.yaml declares one; otherwise returns a
 * fail-silent noop so local-only workspaces keep working. Any thrown error
 * during resolution collapses to a noop — callers never branch on network
 * conditions or transport availability.
 */
export async function resolveConductorEventSink() {
    try {
        const { resolveWuLifecycleEventSink } = await import('../wu-lifecycle-sync/control-plane-sink.js');
        const sink = await resolveWuLifecycleEventSink({});
        return (event) => {
            // KernelEvent and SdkKernelEvent overlap on the wire shapes that the
            // cloud accepts (ADR-011 §2); the sink typechecks against KernelEvent
            // for v1 compat while the SDK discriminant drives v2 routing.
            void sink.push([event]);
        };
    }
    catch {
        return () => {
            /* noop: local-only workspace, or transient sink failure */
        };
    }
}
//# sourceMappingURL=lifecycle-emitters.js.map