/**
 * Control Flow Analysis Utilities
 *
 * Backward-compatible wrappers around shared code-analysis utilities.
 * Existing callers pass (content: string, lineNumber: number) — these
 * wrappers create a temporary ParsedFile to delegate to the shared implementation.
 */

import { ParsedFile } from '../../../../shared/parsed-file'
import * as codeAnalysis from '../../../../shared/code-analysis'

/**
 * Check if a line is inside a try-catch block
 * Looks for enclosing try { ... } catch pattern
 */
export function isInsideTryCatch(content: string, lineNumber: number): boolean {
  return codeAnalysis.isInsideTryCatch(new ParsedFile(content, ''), lineNumber)
}

/**
 * Simpler heuristic: check if there's a try-catch in the same function scope
 * Looks for try { before the line and } catch after, within reasonable bounds
 */
export function hasTryCatchNearby(content: string, lineNumber: number, windowSize: number = 20): boolean {
  return codeAnalysis.hasTryCatchNearby(new ParsedFile(content, ''), lineNumber, windowSize)
}

/**
 * Extract function context where a call is being made
 * Looks backwards from the current line to find enclosing function name
 * Returns lowercase function name or null if not found
 */
export function extractFunctionContext(content: string, lineNumber: number): string | null {
  return codeAnalysis.extractFunctionContext(new ParsedFile(content, ''), lineNumber)
}
