import { parseExpression } from '../runtime/evaluator/expression/index.js';
import { DEFAULT_TRIGGERS } from '../runtime/event.js';
/**
 * Compile `guards` into a single synthetic skill `<pack>/guards`. When `guards`
 * is empty the result is `{ok: true, skill}` with a zero-rule skill — callers
 * SHOULD filter out empty-rule skills before appending (matches
 * `compileVerifyGates`). A `when` expression that fails to parse is collected
 * (with its guard name) and surfaced as `{ok: false, errors}` — no silent skip.
 */
export function compileGuards(packName, guards) {
    const errors = [];
    const rules = [];
    for (const guard of guards) {
        try {
            parseExpression(guard.when);
        }
        catch (e) {
            errors.push({
                guardName: guard.name,
                message: `when failed to parse: ${e instanceof Error ? e.message : String(e)}`,
            });
            continue;
        }
        const process = [];
        if (guard.detect !== undefined) {
            process.push({
                call: guard.detect.call,
                ...(guard.detect.args !== undefined ? { args: guard.detect.args } : {}),
                as: guard.as,
            });
        }
        process.push({
            call: 'verdict',
            if: guard.when,
            args: { level: guard.level, message: guard.message },
        });
        rules.push({ id: `guard:${guard.name}`, kind: 'track_check', requires: [], process });
    }
    if (errors.length > 0)
        return { ok: false, errors };
    // Triggers: one per guard's event kind, deduped (matches compileVerifyGates).
    const kinds = new Set(guards.map((g) => g.on));
    const triggers = guards.length === 0 ? [...DEFAULT_TRIGGERS] : [...kinds].map((kind) => ({ kind }));
    const skill = {
        name: `${packName}/guards`,
        load: 'lazy',
        when_to_load: [],
        requires: [],
        unloads_when: [],
        triggers,
        rules,
        tools: [],
    };
    return { ok: true, skill };
}
//# sourceMappingURL=guards_compiler.js.map