import type { ListenerConfig, ResourceRegistry, SwiftListenerEvent } from '../types';
import { forwardToOmnl } from './omnlForwardAdapter';
import { submitToIntakeGateway } from './intakeGatewayAdapter';

export async function dispatchMatchedOutbounds(
  projectRoot: string,
  config: ListenerConfig,
  registry: ResourceRegistry,
  event: SwiftListenerEvent
): Promise<SwiftListenerEvent['outbound']> {
  if (!event.matched || !event.canonical) return undefined;

  const outbound: NonNullable<SwiftListenerEvent['outbound']> = {};

  const omnlEnabled = config.outbound?.omnlForward?.enabled || config.omnlForward?.enabled;
  if (omnlEnabled) {
    outbound.omnl = await forwardToOmnl(projectRoot, config, event);
  }

  if (config.outbound?.intakeGateway?.enabled) {
    outbound.intakeGateway = await submitToIntakeGateway(config, registry, event.canonical);
  }

  return outbound;
}
