const LEFT_BOUNDARY_CHARS = new Set(['(', '[', '{', '"', "'", ',', ':', ';', '!', '?']);
const RIGHT_BOUNDARY_CHARS = new Set([')', ']', '}', '"', "'", ',', '.', ':', ';', '!', '?']);
function isWhitespaceChar(char) {
    return char === ' ' || char === '\t' || char === '\n' || char === '\r';
}
function isSkillNameChar(char) {
    return Boolean(char && /[a-z0-9-]/.test(char));
}
function isInlineSkillLeftBoundary(char) {
    return char === undefined || isWhitespaceChar(char) || LEFT_BOUNDARY_CHARS.has(char);
}
function isInlineSkillRightBoundary(char) {
    return char === undefined || isWhitespaceChar(char) || RIGHT_BOUNDARY_CHARS.has(char);
}
function collapseMarkerGap(before, after) {
    const collapseWhitespaceBoundary = /\s$/.test(before) && /^\s/.test(after);
    const needsSpace = before.length > 0 &&
        after.length > 0 &&
        !/\s$/.test(before) &&
        !/^\s/.test(after) &&
        /[\w)\]}"']$/.test(before) &&
        /^[\w([{"]/.test(after);
    return collapseWhitespaceBoundary
        ? `${before.replace(/\s+$/, ' ')}${after.replace(/^\s+/, '')}`
        : `${before}${needsSpace ? ' ' : ''}${after}`;
}
export function buildSkillSelectionMarker(name) {
    return `[Skill:${name} selected]`;
}
export function findSkillSelectionMarker(text) {
    const markerStart = text.indexOf('[Skill:');
    if (markerStart === -1)
        return null;
    const nameStart = markerStart + '[Skill:'.length;
    let cursor = nameStart;
    while (isSkillNameChar(text[cursor])) {
        cursor += 1;
    }
    if (cursor === nameStart)
        return null;
    if (!text.startsWith(' selected]', cursor))
        return null;
    const end = cursor + ' selected]'.length;
    const marker = text.slice(markerStart, end);
    return {
        name: text.slice(nameStart, cursor),
        marker,
        start: markerStart,
        end,
        beforeText: text.slice(0, markerStart),
        afterText: text.slice(end)
    };
}
export function removeSkillSelectionMarker(text, match) {
    const combined = collapseMarkerGap(match.beforeText, match.afterText).trim();
    return combined.length > 0 ? combined : undefined;
}
export function findExplicitSkillInvocation(text) {
    for (let index = 0; index < text.length; index += 1) {
        if (!text.startsWith('/skill:', index))
            continue;
        if (!isInlineSkillLeftBoundary(text[index - 1]))
            continue;
        const nameStart = index + '/skill:'.length;
        let cursor = nameStart;
        while (isSkillNameChar(text[cursor])) {
            cursor += 1;
        }
        if (cursor === nameStart)
            continue;
        if (!isInlineSkillRightBoundary(text[cursor]))
            continue;
        return {
            skillName: text.slice(nameStart, cursor),
            invocation: text.slice(index, cursor),
            start: index,
            end: cursor
        };
    }
    return null;
}
//# sourceMappingURL=invocation.js.map