import { findAgentsFilesInScopeDetailed } from "../utils/agents_files.js";
import { buildProjectContextBlock, buildSkillsIndexBlock } from "../utils/context_builder.js";
export function resolvePersonaSkillsForPromptContext(args) {
    const personaSkills = args.persona.skills;
    if (personaSkills === "*") {
        const skills = [...args.discoveredSkills];
        return {
            skills,
            unknown: [],
            skillsBlock: buildSkillsIndexBlock(skills),
        };
    }
    if (personaSkills.length === 0) {
        return { skills: [], unknown: [], skillsBlock: undefined };
    }
    const skillsByName = new Map();
    for (const skill of args.discoveredSkills) {
        skillsByName.set(skill.name.toLowerCase(), skill);
    }
    const skills = [];
    const unknown = [];
    for (const name of personaSkills) {
        const trimmed = name.trim();
        if (!trimmed) {
            throw new Error(`persona '${args.persona.id}' has an empty skill name`);
        }
        const skill = skillsByName.get(trimmed.toLowerCase());
        if (skill) {
            skills.push(skill);
            continue;
        }
        unknown.push(trimmed);
    }
    return {
        skills,
        unknown,
        skillsBlock: buildSkillsIndexBlock(skills),
    };
}
export function resolveProjectContextForPromptContext(args) {
    if (!args.includeAgentContext) {
        return { agentsFiles: [], warnings: [], projectContextBlock: undefined };
    }
    const agentsContext = findAgentsFilesInScopeDetailed(args.cwd, args.home);
    return {
        agentsFiles: agentsContext.files,
        warnings: agentsContext.errors,
        projectContextBlock: buildProjectContextBlock({
            cwd: args.cwd,
            home: args.home,
            agentsFiles: agentsContext.files,
            readFile: args.readFile,
        }),
    };
}
export function resolveRuntimePromptBootstrap(args) {
    const projectContext = resolveProjectContextForPromptContext({
        cwd: args.cwd,
        home: args.home,
        includeAgentContext: args.includeAgentContext,
        readFile: args.readFile,
    });
    const resolvedSkills = resolvePersonaSkillsForPromptContext({
        persona: args.persona,
        discoveredSkills: args.discoveredSkills,
    });
    return {
        promptContext: {
            cwd: args.cwd,
            home: args.home,
            includeAgentContext: args.includeAgentContext,
            projectContextBlock: projectContext.projectContextBlock,
            skillsBlock: resolvedSkills.skillsBlock,
        },
        agentsFiles: projectContext.agentsFiles,
        warnings: projectContext.warnings,
        unknownSkills: resolvedSkills.unknown,
    };
}
//# sourceMappingURL=runtime_bootstrap.js.map