"use strict";
/**
 * 🤖 Claude-Inspired Todo Engine - Championship Gamification System
 *
 * PORTABLE ARCHITECTURE:
 * - Pure TypeScript core (no CLI dependencies)
 * - Works in: Node.js CLI, Browser/Svelte, API endpoints
 * - Pluggable scoring algorithms
 * - Event-driven progress tracking
 *
 * DESIGN PHILOSOPHY:
 * Turn low AI context scores into exciting improvement games
 * with Claude-inspired todo lists and celebration systems.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TodoEngineUtils = exports.ClaudeTodoEngine = void 0;
exports.createClaudeTodoEngine = createClaudeTodoEngine;
// =====================================
// SCORE-BASED TODO GENERATION ENGINE
// =====================================
class ClaudeTodoEngine {
    todoDatabase = new Map();
    completionCallbacks = [];
    constructor() {
        this.initializeTodoDatabase();
    }
    /**
     * Generate contextual todo list based on current score and missing context
     */
    generateTodoList(context) {
        const targetScore = this.calculateTargetScore(context.currentScore);
        const availableTasks = this.getRelevantTasks(context);
        const optimizedTasks = this.optimizeTaskSelection(availableTasks, context, targetScore);
        return {
            id: this.generateId(),
            projectId: context.projectType || 'unknown',
            currentScore: context.currentScore,
            targetScore,
            items: optimizedTasks,
            createdAt: new Date(),
            updatedAt: new Date()
        };
    }
    /**
     * Complete a todo item and calculate score improvement
     */
    completeTask(todoList, taskId, newProjectScore) {
        const task = todoList.items.find(item => item.id === taskId);
        if (!task || task.completed) {
            throw new Error(`Task ${taskId} not found or already completed`);
        }
        const oldScore = todoList.currentScore;
        task.completed = true;
        task.completedAt = new Date();
        todoList.currentScore = newProjectScore;
        todoList.updatedAt = new Date();
        const improvement = {
            before: oldScore,
            after: newProjectScore,
            improvement: newProjectScore - oldScore,
            taskCompleted: task.title,
            celebrationLevel: this.calculateCelebrationLevel(oldScore, newProjectScore, todoList.targetScore)
        };
        // Check for championship completion
        if (newProjectScore >= todoList.targetScore && !todoList.completedAt) {
            todoList.completedAt = new Date();
            improvement.celebrationLevel = 'championship';
        }
        // Trigger callbacks
        this.completionCallbacks.forEach(callback => callback(improvement));
        return improvement;
    }
    /**
     * Subscribe to completion events (for UI updates, celebrations, etc.)
     */
    onTaskCompletion(callback) {
        this.completionCallbacks.push(callback);
    }
    /**
     * Get progress statistics
     */
    getProgress(todoList) {
        const completed = todoList.items.filter(item => item.completed).length;
        const total = todoList.items.length;
        const remainingTasks = todoList.items.filter(item => !item.completed);
        const remainingPoints = remainingTasks.reduce((sum, task) => sum + task.impact, 0);
        const estimatedMinutes = remainingTasks.reduce((sum, task) => sum + task.estimatedMinutes, 0);
        return {
            completed,
            total,
            percentage: total > 0 ? (completed / total) * 100 : 0,
            remainingPoints,
            estimatedMinutes
        };
    }
    // =====================================
    // PRIVATE IMPLEMENTATION
    // =====================================
    initializeTodoDatabase() {
        // High-impact documentation tasks (Claude's favorites)
        this.addTaskTemplate({
            id: 'readme-creation',
            title: 'Create comprehensive README.md',
            description: 'Add project overview, installation, and usage instructions',
            impact: 15,
            difficulty: 'medium',
            category: 'documentation',
            completed: false,
            estimatedMinutes: 20,
            aiPersonality: 'claude'
        });
        this.addTaskTemplate({
            id: 'project-goals',
            title: 'Define clear project goals',
            description: 'Add specific, measurable project objectives to .faf',
            impact: 12,
            difficulty: 'easy',
            category: 'context',
            completed: false,
            estimatedMinutes: 10,
            aiPersonality: 'universal'
        });
        this.addTaskTemplate({
            id: 'tech-stack-documentation',
            title: 'Document complete tech stack',
            description: 'List all frameworks, libraries, and tools used',
            impact: 10,
            difficulty: 'easy',
            category: 'structure',
            completed: false,
            estimatedMinutes: 8,
            aiPersonality: 'chatgpt'
        });
        this.addTaskTemplate({
            id: 'api-documentation',
            title: 'Add API documentation',
            description: 'Document all public APIs and endpoints',
            impact: 18,
            difficulty: 'hard',
            category: 'documentation',
            completed: false,
            estimatedMinutes: 45,
            aiPersonality: 'claude'
        });
        this.addTaskTemplate({
            id: 'examples-creation',
            title: 'Add usage examples',
            description: 'Create practical examples showing how to use the project',
            impact: 14,
            difficulty: 'medium',
            category: 'examples',
            completed: false,
            estimatedMinutes: 30,
            aiPersonality: 'chatgpt'
        });
        this.addTaskTemplate({
            id: 'architecture-overview',
            title: 'Add architecture overview',
            description: 'Explain system design and component relationships',
            impact: 16,
            difficulty: 'medium',
            category: 'documentation',
            completed: false,
            estimatedMinutes: 25,
            aiPersonality: 'gemini'
        });
        this.addTaskTemplate({
            id: 'package-description',
            title: 'Improve package.json description',
            description: 'Write clear, compelling project description',
            impact: 8,
            difficulty: 'easy',
            category: 'structure',
            completed: false,
            estimatedMinutes: 5,
            aiPersonality: 'universal'
        });
        this.addTaskTemplate({
            id: 'contributing-guide',
            title: 'Create CONTRIBUTING.md',
            description: 'Guide contributors on how to participate',
            impact: 11,
            difficulty: 'medium',
            category: 'documentation',
            completed: false,
            estimatedMinutes: 15,
            aiPersonality: 'claude'
        });
        this.addTaskTemplate({
            id: 'error-handling-docs',
            title: 'Document error handling',
            description: 'Explain how errors are handled and what users should expect',
            impact: 9,
            difficulty: 'easy',
            category: 'quality',
            completed: false,
            estimatedMinutes: 12,
            aiPersonality: 'gemini'
        });
        this.addTaskTemplate({
            id: 'deployment-instructions',
            title: 'Add deployment instructions',
            description: 'Step-by-step guide for deploying the project',
            impact: 13,
            difficulty: 'medium',
            category: 'documentation',
            completed: false,
            estimatedMinutes: 20,
            aiPersonality: 'chatgpt'
        });
    }
    addTaskTemplate(template) {
        const category = template.category;
        if (!this.todoDatabase.has(category)) {
            this.todoDatabase.set(category, []);
        }
        this.todoDatabase.get(category).push(template);
    }
    getRelevantTasks(context) {
        const allTasks = [];
        // Get tasks from all categories
        this.todoDatabase.forEach((categoryTasks) => {
            allTasks.push(...categoryTasks);
        });
        // Filter based on missing context and existing files
        return allTasks.filter(task => this.isTaskRelevant(task, context));
    }
    isTaskRelevant(task, context) {
        // Don't suggest README if it already exists
        if (task.id === 'readme-creation' && context.existingFiles.includes('README.md')) {
            return false;
        }
        // Don't suggest package.json improvements if it doesn't exist
        if (task.id === 'package-description' && !context.existingFiles.includes('package.json')) {
            return false;
        }
        // Prefer tasks matching AI personality
        if (context.aiPreference && task.aiPersonality !== 'universal' &&
            task.aiPersonality !== context.aiPreference) {
            return Math.random() > 0.7; // 30% chance to include non-preferred
        }
        return true;
    }
    optimizeTaskSelection(availableTasks, context, targetScore) {
        const pointsNeeded = Math.max(0, targetScore - context.currentScore);
        const selectedTasks = [];
        let currentPoints = 0;
        // Sort by impact/effort ratio (efficiency)
        const sortedTasks = availableTasks
            .map(task => ({
            ...task,
            efficiency: task.impact / task.estimatedMinutes
        }))
            .sort((a, b) => b.efficiency - a.efficiency);
        // Select tasks greedily until we hit target or reasonable limit
        for (const task of sortedTasks) {
            if (selectedTasks.length >= 8) {
                break;
            } // Max 8 tasks to avoid overwhelm
            if (currentPoints >= pointsNeeded * 1.2) {
                break;
            } // 20% buffer
            selectedTasks.push({
                ...task,
                id: this.generateId() // Give unique ID for this todo list
            });
            currentPoints += task.impact;
        }
        return selectedTasks;
    }
    calculateTargetScore(currentScore) {
        if (currentScore < 50) {
            return 75;
        }
        if (currentScore < 70) {
            return 85;
        }
        if (currentScore < 85) {
            return 95;
        }
        return 100;
    }
    calculateCelebrationLevel(oldScore, newScore, targetScore) {
        const improvement = newScore - oldScore;
        if (newScore >= targetScore) {
            return 'championship';
        }
        if (improvement >= 15 || newScore >= 85) {
            return 'major';
        }
        return 'minor';
    }
    generateId() {
        return Math.random().toString(36).substring(2) + Date.now().toString(36);
    }
}
exports.ClaudeTodoEngine = ClaudeTodoEngine;
// =====================================
// EXPORT FOR DIFFERENT PLATFORMS
// =====================================
/**
 * Factory function for creating Claude Todo Engine instances
 * Works in both Node.js and browser environments
 */
function createClaudeTodoEngine() {
    return new ClaudeTodoEngine();
}
/**
 * Utility functions for platform-specific integrations
 */
exports.TodoEngineUtils = {
    /**
     * Format progress for CLI output
     */
    formatProgressCLI(progress) {
        const percentage = Math.round(progress.percentage);
        return `${progress.completed}/${progress.total} tasks (${percentage}%) - ${progress.remainingPoints} points remaining`;
    },
    /**
     * Format progress for Svelte reactive stores
     */
    formatProgressSvelte(progress) {
        return {
            completed: progress.completed,
            total: progress.total,
            percentage: Math.round(progress.percentage),
            remainingPoints: progress.remainingPoints,
            estimatedMinutes: progress.estimatedMinutes,
            isComplete: progress.percentage === 100
        };
    },
    /**
     * Generate celebration message based on improvement
     */
    generateCelebration(improvement) {
        switch (improvement.celebrationLevel) {
            case 'championship':
                return `🏆 CHAMPIONSHIP ACHIEVED! Perfect score: ${improvement.after}%!`;
            case 'major':
                return `🎉 Major breakthrough! +${improvement.improvement} points (${improvement.after}%)`;
            case 'minor':
                return `☑️ Great progress! +${improvement.improvement} points (${improvement.after}%)`;
        }
    }
};
//# sourceMappingURL=claude-todo-engine.js.map