import { logger } from '@pellux/goodvibes-sdk/platform/utils/logger';
import { executeGateCommand, getSkippedGateReason, loadPackageScripts, } from '@pellux/goodvibes-sdk/platform/agents/wrfc-gates';
import { getEnabledWrfcGates } from './wrfc-config.js';
import { emitWrfcGateResult } from './wrfc-runtime-events.js';
export async function runWrfcGateChecks(options) {
    const gates = getEnabledWrfcGates(options.configManager);
    if (gates.length === 0) {
        logger.debug('Wrfc gate runner: no gates configured', { chainId: options.chainId });
        return [];
    }
    logger.debug('Wrfc gate runner: executing gates', {
        chainId: options.chainId,
        gateCount: gates.length,
    });
    const pkgScripts = await loadPackageScripts(options.projectRoot);
    const results = [];
    for (const gate of gates) {
        const skipReason = getSkippedGateReason(gate.name, options.projectRoot, pkgScripts);
        if (skipReason !== null) {
            const result = {
                gate: gate.name,
                passed: true,
                output: skipReason,
                durationMs: 0,
            };
            results.push(result);
            emitWrfcGateResult(options.runtimeBus, options.sessionId, options.chainId, gate.name, true);
            options.onResult?.(results.slice(), result);
            logger.debug('Wrfc gate runner: gate skipped', {
                chainId: options.chainId,
                gate: gate.name,
                reason: skipReason,
            });
            continue;
        }
        const startedAt = Date.now();
        const { passed, output } = await executeGateCommand(gate.command);
        const result = {
            gate: gate.name,
            passed,
            output,
            durationMs: Date.now() - startedAt,
        };
        results.push(result);
        emitWrfcGateResult(options.runtimeBus, options.sessionId, options.chainId, gate.name, passed);
        options.onResult?.(results.slice(), result);
        logger.debug('Wrfc gate runner: gate result', {
            chainId: options.chainId,
            gate: gate.name,
            passed,
            durationMs: result.durationMs,
        });
    }
    return results;
}
