/**
 * Increment Limits
 *
 * Type-based limits for concurrent active increments.
 * Part of increment 0007: Smart Status Management
 */
import { MetadataManager } from './metadata-manager.js';
import { IncrementType, IncrementStatus, TYPE_LIMITS } from '../types/increment-metadata.js';
/**
 * Check if creating a new increment would exceed type-based limits
 */
export function checkIncrementLimits(type) {
    // Get limit for this type
    const limit = TYPE_LIMITS[type];
    // Get active increments of this type (not paused, not completed, not abandoned)
    const allIncrements = MetadataManager.getAll();
    const activeOfType = allIncrements.filter(m => m.type === type && m.status === IncrementStatus.ACTIVE);
    const current = activeOfType.length;
    // If unlimited, no limit enforcement but still report current count
    if (limit === null) {
        return {
            exceeded: false,
            current,
            limit: null,
            severity: 'info'
        };
    }
    const exceeded = current >= limit;
    // Build result
    const result = {
        exceeded,
        current,
        limit,
        severity: exceeded ? 'warning' : 'info',
        activeIncrements: activeOfType
    };
    // Add warning message if exceeded
    if (exceeded) {
        result.warning = buildLimitWarning(type, current, limit, activeOfType);
        result.suggestions = buildSuggestions(type);
    }
    return result;
}
/**
 * Check all increment types and return warnings
 */
export function checkAllLimits() {
    const results = {};
    for (const type of Object.values(IncrementType)) {
        results[type] = checkIncrementLimits(type);
    }
    return results;
}
/**
 * Get context switching warning for starting a new increment
 */
export function getContextSwitchWarning(newType) {
    const activeIncrements = MetadataManager.getActive();
    const activeCount = activeIncrements.length;
    // No warning if no active increments
    if (activeCount === 0) {
        return {
            show: false,
            productivityCost: '0%',
            message: '',
            options: []
        };
    }
    // No warning for hotfixes (emergency work)
    if (newType === IncrementType.HOTFIX || newType === IncrementType.BUG) {
        return {
            show: false,
            productivityCost: '0%',
            message: '',
            options: []
        };
    }
    // Calculate productivity cost
    const productivityCost = calculateProductivityCost(activeCount);
    // Build warning message
    const message = buildContextSwitchMessage(activeCount, productivityCost, activeIncrements);
    // Build options
    const options = [
        {
            label: 'Continue current work',
            value: 'continue',
            description: 'Finish active increment(s) before starting new work (recommended)'
        },
        {
            label: 'Pause current work',
            value: 'pause',
            description: 'Pause active increment(s) to focus on new work'
        },
        {
            label: 'Work in parallel',
            value: 'parallel',
            description: `Start new increment (${productivityCost} productivity cost)`
        }
    ];
    return {
        show: true,
        productivityCost,
        message,
        options
    };
}
/**
 * Build warning message for exceeded limit
 */
function buildLimitWarning(type, current, limit, activeIncrements) {
    const typeLabel = formatTypeLabel(type);
    const incrementList = activeIncrements
        .map(m => `  • ${m.id}`)
        .join('\n');
    return `
⚠️  You have ${current} active ${typeLabel} increment(s) (limit: ${limit})

Active ${typeLabel} increments:
${incrementList}

Recommendation: Complete or pause one before starting another.
Context switching reduces productivity by 20-40%.
`.trim();
}
/**
 * Type-specific suggestions for exceeding limits
 */
const TYPE_SUGGESTIONS = {
    [IncrementType.FEATURE]: 'Reduce scope: Break large feature into smaller increments',
    [IncrementType.REFACTOR]: 'Refactors need focus: Complete current refactor before starting new one'
};
/**
 * Build suggestions for resolving limit exceeded
 */
function buildSuggestions(type) {
    const suggestions = [
        'Complete active increment: /done <id>',
        'Pause active increment: /pause <id>',
        'Check status: /status'
    ];
    const typeSuggestion = TYPE_SUGGESTIONS[type];
    if (typeSuggestion) {
        suggestions.push(typeSuggestion);
    }
    return suggestions;
}
/**
 * Build context switching warning message
 */
function buildContextSwitchMessage(activeCount, productivityCost, activeIncrements) {
    const incrementList = activeIncrements
        .map(m => `  • ${m.id} [${m.type}]`)
        .join('\n');
    return `
⚠️  You have ${activeCount} active increment(s)

Active increments:
${incrementList}

Starting new work will reduce productivity by ${productivityCost} due to context switching.

Research shows:
• 2 concurrent tasks = 20% slower
• 3+ concurrent tasks = 40% slower
• Frequent switches = more bugs

Recommended: Complete or pause active work first.
`.trim();
}
/**
 * Productivity cost lookup table based on research
 */
const PRODUCTIVITY_COST = ['0%', '20%', '30%', '40%'];
/**
 * Calculate productivity cost based on active increment count
 */
function calculateProductivityCost(activeCount) {
    return PRODUCTIVITY_COST[Math.min(activeCount, PRODUCTIVITY_COST.length - 1)];
}
/**
 * Type label mapping for display
 */
const TYPE_LABELS = {
    [IncrementType.HOTFIX]: 'hotfix',
    [IncrementType.FEATURE]: 'feature',
    [IncrementType.BUG]: 'bug',
    [IncrementType.CHANGE_REQUEST]: 'change request',
    [IncrementType.REFACTOR]: 'refactor',
    [IncrementType.EXPERIMENT]: 'experiment'
};
/**
 * Format type label for display
 */
function formatTypeLabel(type) {
    return TYPE_LABELS[type] ?? type;
}
/**
 * Get summary of all limits
 */
export function getLimitsSummary() {
    const results = checkAllLimits();
    const lines = [];
    lines.push('Increment Limits:');
    lines.push('');
    for (const [type, result] of Object.entries(results)) {
        const typeLabel = formatTypeLabel(type);
        const limitStr = result.limit === null ? 'unlimited' : result.limit.toString();
        const status = result.exceeded ? '⚠️ ' : '✅';
        lines.push(`${status} ${typeLabel}: ${result.current}/${limitStr} active`);
    }
    return lines.join('\n');
}
/**
 * Check if starting a new increment of this type would be allowed
 * (considering limits)
 */
export function canStartIncrement(type) {
    const check = checkIncrementLimits(type);
    if (!check.exceeded) {
        return { allowed: true };
    }
    return {
        allowed: false,
        reason: check.warning
    };
}
//# sourceMappingURL=limits.js.map