/**
 * Skill command expansion.
 *
 * Pure function to rewrite `/skill:name args` and bare `/name args` commands
 * into backend-style selected-skill markers.
 */
import { BARE_SKILL_NAMES } from '../../config.js';
import { buildSkillSelectionMarker, findExplicitSkillInvocation } from '../../shared/skills/invocation.js';
/**
 * Rewrite a skill command (`/skill:name args` or bare `/name args`) to a
 * selected-skill marker.
 *
 * Returns the rewritten text, or the original text if not a skill command or
 * the skill is not found.
 */
export function expandSkillCommand(text, resourceLoader) {
    const explicitMatch = findExplicitSkillInvocation(text);
    if (explicitMatch) {
        const skill = resourceLoader.getSkills().skills.find((s) => s.name === explicitMatch.skillName);
        if (!skill)
            return text;
        const selectedSkill = buildSkillSelectionMarker(skill.name);
        return `${text.slice(0, explicitMatch.start)}${selectedSkill}${text.slice(explicitMatch.end)}`;
    }
    if (!text.startsWith('/'))
        return text;
    const spaceIndex = text.indexOf(' ');
    let skillName;
    let args;
    if (text.startsWith('/skill:')) {
        // Explicit /skill:name — any skill
        skillName = spaceIndex === -1 ? text.slice(7) : text.slice(7, spaceIndex);
        args = spaceIndex === -1 ? '' : text.slice(spaceIndex + 1).trim();
    }
    else {
        // Bare /name — only allowed for BARE_SKILLS
        const candidate = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
        if (!BARE_SKILL_NAMES.has(candidate))
            return text;
        skillName = candidate;
        args = spaceIndex === -1 ? '' : text.slice(spaceIndex + 1).trim();
    }
    const skill = resourceLoader.getSkills().skills.find((s) => s.name === skillName);
    if (!skill)
        return text; // Not a skill, pass through
    const selectedSkill = buildSkillSelectionMarker(skill.name);
    return args ? `${selectedSkill} ${args}` : selectedSkill;
}
//# sourceMappingURL=skill-expansion.js.map