import { selectParticipantAgent } from "./participant.js";
function resolveDefaultParticipant(participants) {
    const defaultParticipant = participants.default;
    if (defaultParticipant !== undefined) {
        return defaultParticipant;
    }
    const participantIds = Object.keys(participants);
    if (participantIds.length === 1) {
        return participants[participantIds[0]];
    }
    throw new Error("Hook is missing a participant and no default participant is defined.");
}
function resolveHookParticipant(hook, participants) {
    if (hook.participant === undefined) {
        return resolveDefaultParticipant(participants);
    }
    if (!Object.hasOwn(participants, hook.participant)) {
        throw new Error(`Unknown participant: ${hook.participant}`);
    }
    return participants[hook.participant];
}
export async function runWorkflowHook(hook, context) {
    const participant = resolveHookParticipant(hook, context.participants);
    const mode = hook.mode ?? participant.mode;
    if (mode === undefined) {
        throw new Error(`Hook is missing mode for participant "${participant.id}".`);
    }
    await context.runAgent({
        agent: selectParticipantAgent(participant, 0),
        prompt: hook.prompt,
        mode,
        cwd: context.cwd,
        ...(participant.model ? { model: participant.model } : {}),
        ...(context.signal ? { signal: context.signal } : {})
    });
}
