// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// engine/role-template-loader.ts — SPEC-690
import { readFile } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const ROLES_DIR = join(dirname(fileURLToPath(import.meta.url)), '../../config/agent-roles');
const BUILTIN_ROLES = ['planner', 'coder', 'tester', 'security-reviewer', 'qa'];
export async function loadRoleTemplate(role) {
    const filePath = `${ROLES_DIR}/${role}.json`;
    const raw = await readFile(filePath, 'utf-8');
    return JSON.parse(raw);
}
export async function loadAllRoleTemplates() {
    const results = [];
    for (const role of BUILTIN_ROLES) {
        try {
            const template = await loadRoleTemplate(role);
            results.push(template);
        }
        catch {
            // skip missing files
        }
    }
    return results;
}
function matchesTrigger(trigger, ctx) {
    if (trigger === 'always') {
        return true;
    }
    if (trigger === 'isPublicApi=true') {
        return ctx.isPublicApi;
    }
    if (trigger.startsWith('difficulty>=')) {
        const n = parseInt(trigger.slice('difficulty>='.length), 10);
        return ctx.difficulty >= n;
    }
    if (trigger.startsWith('scope=')) {
        const scopeValue = trigger.slice('scope='.length);
        return ctx.scope === scopeValue;
    }
    return false;
}
export async function selectRolesForSpec(ctx) {
    const allTemplates = await loadAllRoleTemplates();
    const selected = [];
    const rationale = [];
    for (const template of allTemplates) {
        const matched = template.triggers.some((trigger) => matchesTrigger(trigger, ctx));
        if (matched) {
            selected.push(template);
            rationale.push(`${template.role}: trigger matched for context`);
        }
    }
    // always include coder as fallback
    const hasCoder = selected.some((r) => r.role === 'coder');
    if (!hasCoder) {
        const coderTemplate = allTemplates.find((r) => r.role === 'coder');
        if (coderTemplate !== undefined) {
            selected.push(coderTemplate);
            rationale.push('coder: always included as fallback');
        }
    }
    return { roles: selected, rationale };
}
//# sourceMappingURL=role-template-loader.js.map