import { parseExpression } from '../runtime/evaluator/expression/index.js';
import { DEFAULT_TRIGGERS } from '../runtime/event.js';
/**
 * Compile `gates` into a single synthetic skill. When `gates` is empty
 * the result is `{ok: true, skill}` with a default-trigger skill that
 * carries zero rules — callers SHOULD filter out empty-rule skills before
 * appending to the pack to keep dispatcher noise low.
 */
export function compileVerifyGates(packName, gates) {
    const errors = [];
    const rules = [];
    for (const gate of gates) {
        try {
            parseExpression(gate.check);
        }
        catch (e) {
            errors.push({
                gateName: gate.name,
                message: `check failed to parse: ${e instanceof Error ? e.message : String(e)}`,
            });
            continue;
        }
        const step = {
            call: 'verdict',
            if: gate.check,
            args: {
                level: gate.on_fail.level,
                message: gate.on_fail.message,
            },
        };
        rules.push({
            id: `gate:${gate.name}`,
            kind: 'track_check',
            requires: [],
            process: [step],
        });
    }
    if (errors.length > 0)
        return { ok: false, errors };
    // Triggers: one per gate's event_kind, deduped. Tool-name filtering, if
    // wanted, belongs INSIDE the `check` expression (e.g. `match(tool,
    // '^Bash$') && contains(tool_args.command, 'rm -rf')`) — the `tool_call`
    // Trigger variant in event.ts intentionally carries no per-trigger
    // tool_match field (matches existing skill grammar).
    const kinds = new Set(gates.map((g) => g.when.event_kind));
    const triggers = gates.length === 0 ? [...DEFAULT_TRIGGERS] : [...kinds].map((kind) => ({ kind }));
    const skill = {
        name: `${packName}/verify`,
        load: 'lazy',
        when_to_load: [],
        requires: [],
        unloads_when: [],
        triggers,
        rules,
        tools: [],
    };
    return { ok: true, skill };
}
//# sourceMappingURL=verify_gates_compiler.js.map