/**
 * Context Files - Automatic Project Context (Gemini CLI inspired)
 *
 * Automatically loads context from special files:
 * - .codebuddy/CONTEXT.md - Project-specific context
 * - CODEBUDDY.md - Alternative location (project root)
 * - ~/.codebuddy/CONTEXT.md - Global user context
 *
 * These files are read at startup and injected into the system prompt.
 */
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
/**
 * Context file locations in order of priority
 */
const CONTEXT_FILE_LOCATIONS = [
    { pattern: '.codebuddy/CONTEXT.md', source: 'project', priority: 1 },
    { pattern: 'CODEBUDDY.md', source: 'project', priority: 2 },
    { pattern: '.codebuddy/context.md', source: 'project', priority: 3 },
    { pattern: 'CLAUDE.md', source: 'project', priority: 4 }, // Compatibility
];
const GLOBAL_CONTEXT_FILE = {
    pattern: path.join(os.homedir(), '.codebuddy', 'CONTEXT.md'),
    source: 'global',
    priority: 10,
};
/**
 * Load context files from project directory
 */
export async function loadProjectContextFiles(projectDir = process.cwd()) {
    const files = [];
    // Load project-specific context files
    for (const location of CONTEXT_FILE_LOCATIONS) {
        const filePath = path.join(projectDir, location.pattern);
        try {
            if (await fs.pathExists(filePath)) {
                const content = await fs.readFile(filePath, 'utf-8');
                files.push({
                    path: filePath,
                    content,
                    source: location.source,
                    priority: location.priority,
                });
            }
        }
        catch {
            // Ignore read errors
        }
    }
    // Load global context file
    try {
        if (await fs.pathExists(GLOBAL_CONTEXT_FILE.pattern)) {
            const content = await fs.readFile(GLOBAL_CONTEXT_FILE.pattern, 'utf-8');
            files.push({
                path: GLOBAL_CONTEXT_FILE.pattern,
                content,
                source: GLOBAL_CONTEXT_FILE.source,
                priority: GLOBAL_CONTEXT_FILE.priority,
            });
        }
    }
    catch {
        // Ignore read errors
    }
    // Sort by priority (lower = higher priority)
    return files.sort((a, b) => a.priority - b.priority);
}
/**
 * Load and combine all context files
 */
export async function loadContext(projectDir = process.cwd()) {
    const files = await loadProjectContextFiles(projectDir);
    if (files.length === 0) {
        return {
            files: [],
            combinedContent: '',
            totalSize: 0,
        };
    }
    // Combine content with headers
    const sections = files.map(f => {
        const sourceLabel = f.source === 'global' ? '(global)' : '(project)';
        const fileName = path.basename(f.path);
        return `<!-- Context from ${fileName} ${sourceLabel} -->\n${f.content}`;
    });
    const combinedContent = sections.join('\n\n---\n\n');
    return {
        files,
        combinedContent,
        totalSize: combinedContent.length,
    };
}
/**
 * Format context for inclusion in system prompt
 */
export function formatContextForPrompt(context) {
    if (!context.combinedContent) {
        return '';
    }
    return `<project_context>
${context.combinedContent}
</project_context>`;
}
/**
 * Initialize context file in project
 */
export async function initContextFile(projectDir = process.cwd()) {
    const contextDir = path.join(projectDir, '.codebuddy');
    const contextFile = path.join(contextDir, 'CONTEXT.md');
    await fs.ensureDir(contextDir);
    if (await fs.pathExists(contextFile)) {
        return contextFile; // Already exists
    }
    const template = `# Project Context

This file is automatically loaded by Code Buddy to provide project context.

## Project Overview

<!-- Describe your project here -->

## Architecture

<!-- Key architectural decisions -->

## Conventions

<!-- Coding conventions and patterns -->

## Important Files

<!-- Key files and their purposes -->

## Common Tasks

<!-- How to build, test, deploy -->
`;
    await fs.writeFile(contextFile, template, 'utf-8');
    return contextFile;
}
/**
 * Check if context files exist
 */
export async function hasContextFiles(projectDir = process.cwd()) {
    const files = await loadProjectContextFiles(projectDir);
    return files.length > 0;
}
/**
 * Get context file summary for display
 */
export function formatContextSummary(context) {
    if (context.files.length === 0) {
        return 'No context files found';
    }
    const lines = ['Context files loaded:'];
    for (const file of context.files) {
        const size = Math.round(file.content.length / 1024);
        const sourceLabel = file.source === 'global' ? '(global)' : '';
        lines.push(`  ${path.basename(file.path)} ${sourceLabel} - ${size}KB`);
    }
    lines.push(`  Total: ${Math.round(context.totalSize / 1024)}KB`);
    return lines.join('\n');
}
//# sourceMappingURL=context-files.js.map