// src/engine/opencode/detect.ts — SPEC-966: Detect OpenCode workspace
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
export function detectOpenCodeWorkspace(projectPath) {
    const markers = [];
    let configPath;
    let version;
    // Marker 1: opencode.json in project root
    const opencodeJson = join(projectPath, 'opencode.json');
    if (existsSync(opencodeJson)) {
        markers.push('opencode.json');
        configPath = opencodeJson;
        try {
            const content = JSON.parse(readFileSync(opencodeJson, 'utf-8'));
            version = typeof content.version === 'string' ? content.version : undefined;
        }
        catch {
            // ignore parse errors
        }
    }
    // Marker 2: .opencode/ directory
    const opencodeDir = join(projectPath, '.opencode');
    if (existsSync(opencodeDir)) {
        markers.push('.opencode/');
        configPath ??= opencodeDir;
    }
    // Marker 3: ~/.opencode/ directory
    const homeOpencode = join(homedir(), '.opencode');
    if (existsSync(homeOpencode)) {
        markers.push('~/.opencode/');
    }
    // Marker 4: OPENCODE_HOME env var
    if (process.env.OPENCODE_HOME) {
        markers.push('OPENCODE_HOME');
    }
    return {
        detected: markers.length > 0,
        markers,
        configPath,
        version,
    };
}
//# sourceMappingURL=detect.js.map