// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
/**
 * Tokenize a text string into lowercase words for keyword matching.
 */
function tokenize(text) {
    return text
        .toLowerCase()
        .replace(/[^a-z0-9\s]/g, ' ')
        .split(/\s+/)
        .filter((w) => w.length > 2);
}
/**
 * Count how many tokens from `keywords` appear in `text`.
 */
function countMatches(text, keywords) {
    const tokens = tokenize(text);
    return keywords.reduce((count, kw) => (tokens.includes(kw) ? count + 1 : count), 0);
}
/**
 * Build a combined keyword set from the scope definition.
 */
function buildScopeKeywords(scope) {
    const platformTokens = scope.platforms.flatMap(tokenize);
    const userTokens = scope.targetUsers.flatMap(tokenize);
    const problemTokens = tokenize(scope.primaryProblem);
    return [...new Set([...platformTokens, ...userTokens, ...problemTokens])];
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Evaluate whether a requirement belongs to the project's defined scope.
 *
 * Classification logic:
 * - in_scope: requirement matches >= 2 scope keywords or directly references a platform/user
 * - backlog: requirement matches 1 keyword (adjacent but not currently in scope)
 * - out_of_project: no keyword match (completely unrelated)
 */
export function evaluateScope(requirement, scope) {
    if (!requirement.trim()) {
        return {
            classification: 'out_of_project',
            reason: 'Empty requirement cannot be evaluated.',
            suggestion: 'Provide a concrete description of the requirement.',
        };
    }
    if (scope.platforms.length === 0 &&
        scope.targetUsers.length === 0 &&
        !scope.primaryProblem.trim()) {
        return {
            classification: 'in_scope',
            reason: 'No scope definition found — all requirements are tentatively in scope.',
            suggestion: 'Define the project scope with init_constitution to enable accurate filtering.',
        };
    }
    const keywords = buildScopeKeywords(scope);
    if (keywords.length === 0) {
        return {
            classification: 'in_scope',
            reason: 'Scope definition is too sparse to evaluate — requirement accepted.',
        };
    }
    const matchCount = countMatches(requirement, keywords);
    // Direct platform or user reference (higher weight)
    const reqLower = requirement.toLowerCase();
    const directPlatformMatch = scope.platforms.some((p) => reqLower.includes(p.toLowerCase()));
    const directUserMatch = scope.targetUsers.some((u) => reqLower.includes(u.toLowerCase()));
    if (directPlatformMatch || directUserMatch || matchCount >= 2) {
        const classification = 'in_scope';
        const matchedPlatform = scope.platforms.find((p) => reqLower.includes(p.toLowerCase()));
        const matchedUser = scope.targetUsers.find((u) => reqLower.includes(u.toLowerCase()));
        const reason = matchedPlatform
            ? `Requirement explicitly targets "${matchedPlatform}", which is in scope.`
            : matchedUser
                ? `Requirement targets "${matchedUser}" users, who are in scope.`
                : `Requirement matches ${matchCount} scope keywords — considered in scope.`;
        return { classification, reason };
    }
    if (matchCount === 1) {
        return {
            classification: 'backlog',
            reason: 'Requirement is adjacent to the project scope but not directly related to the current focus.',
            suggestion: 'Add to backlog with manage_scope (add_to_backlog) or open a new project for this idea.',
        };
    }
    return {
        classification: 'out_of_project',
        reason: 'Requirement does not relate to the defined project scope (platforms, users, or problem).',
        suggestion: 'Discard, add to backlog for future consideration, or reconsider whether scope should expand.',
    };
}
/**
 * Build a human-readable summary of a scope evaluation result.
 */
export function formatScopeEvaluation(evaluation, requirement) {
    const label = {
        in_scope: 'IN SCOPE',
        backlog: 'BACKLOG',
        out_of_project: 'OUT OF PROJECT',
    };
    const lines = [
        `## Scope Evaluation`,
        ``,
        `**Requirement:** ${requirement}`,
        `**Classification:** ${label[evaluation.classification]}`,
        `**Reason:** ${evaluation.reason}`,
    ];
    if (evaluation.suggestion) {
        lines.push(`**Suggestion:** ${evaluation.suggestion}`);
    }
    return lines.join('\n');
}
//# sourceMappingURL=scope-guardian.js.map