/**
 * CSharpStrategy - Warm-up strategy for C# projects
 *
 * Handles .sln and .csproj detection with upward directory search.
 * Supports multiple solution file selection with caching.
 *
 * @see spec.md US-001: Language-Aware Warm-up
 * @satisfies AC-US1-01, AC-US1-03
 */
/** Maximum files to return for warm-up */
const MAX_FILES = 10;
/**
 * Get directory from file path (cross-platform)
 */
function getDirectory(filePath) {
    const lastSlash = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
    return lastSlash > 0 ? filePath.substring(0, lastSlash) : filePath;
}
/**
 * Get filename from file path (cross-platform)
 */
function getFilename(filePath) {
    const lastSlash = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
    return lastSlash >= 0 ? filePath.substring(lastSlash + 1) : filePath;
}
/**
 * C# warm-up strategy with solution/project file detection
 */
export class CSharpStrategy {
    constructor(fs, prompt, cache) {
        this.language = 'csharp';
        this.fs = fs;
        this.prompt = prompt;
        this.cache = cache;
    }
    /**
     * Detect the C# project root by searching for .sln or .csproj files
     *
     * @param startDir - Directory to start searching from
     * @returns Project root directory, or null if no C# project found
     */
    async detectProjectRoot(startDir) {
        // Try to find .sln file first (preferred)
        const slnPath = await this.fs.findUp('*.sln', startDir);
        if (slnPath) {
            const dir = getDirectory(slnPath);
            const files = await this.fs.glob('*.sln', dir);
            if (files.length > 1) {
                // Multiple solutions - check cache or prompt
                const cached = await this.getCachedChoice(dir);
                if (cached && files.includes(cached)) {
                    this.solutionFile = cached;
                }
                else if (this.prompt) {
                    const choice = await this.prompt(files, 'Select a solution file:');
                    this.solutionFile = choice;
                    await this.setCachedChoice(dir, choice);
                }
                else {
                    // No prompt available, use first one
                    this.solutionFile = files[0];
                }
            }
            else {
                this.solutionFile = getFilename(slnPath);
            }
            this.projectRoot = dir;
            return dir;
        }
        // Fall back to .csproj
        const csprojPath = await this.fs.findUp('*.csproj', startDir);
        if (csprojPath) {
            const dir = getDirectory(csprojPath);
            this.projectFile = getFilename(csprojPath);
            this.projectRoot = dir;
            return dir;
        }
        return null;
    }
    /**
     * Get files to open for warm-up
     *
     * @param projectRoot - Root directory of the project
     * @returns Promise resolving to array of .cs file paths
     */
    async getFilesToOpen(projectRoot) {
        const files = await this.fs.glob('*.cs', projectRoot);
        return files.slice(0, MAX_FILES);
    }
    getCacheKey(dir) {
        return `csharp:${dir}:solution`;
    }
    async getCachedChoice(dir) {
        if (!this.cache)
            return null;
        return this.cache.get(this.getCacheKey(dir));
    }
    async setCachedChoice(dir, choice) {
        if (!this.cache)
            return;
        await this.cache.set(this.getCacheKey(dir), choice);
    }
}
//# sourceMappingURL=csharp.js.map