"use strict";
/**
 * Request Builders for AI Validation
 *
 * Functions for building validation requests with full or scoped file context.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildHighContextValidationRequest = buildHighContextValidationRequest;
exports.buildMultiFileValidationRequest = buildMultiFileValidationRequest;
const project_context_builder_1 = require("../../utils/project-context-builder");
const path_helpers_1 = require("./utils/path-helpers");
const context_extractor_1 = require("./utils/context-extractor");
// ============================================================================
// Single-File Request Builder
// ============================================================================
/**
 * Build a high-context validation request with full or scoped file content
 */
function buildHighContextValidationRequest(file, findings, projectContext, options) {
    const mode = options?.contextMode ?? 'scoped';
    // Get file content (full or scoped)
    const { numberedContent, contextNote } = getFileContent(file, findings, mode, options?.extractionConfig);
    // Build candidate findings list
    const candidatesText = findings.map((f, idx) => {
        return `### Candidate ${idx}
- **Rule**: ${f.title}
- **Category**: ${f.category}
- **Original Severity**: ${f.severity}
- **Line**: ${f.lineNumber}
- **Detection Layer**: ${f.layer}
- **Description**: ${f.description}
- **Flagged Code**: \`${f.lineContent.trim()}\``;
    }).join('\n\n');
    // Get file-specific context
    const fileContext = (0, project_context_builder_1.getFileValidationContext)(file, projectContext);
    return `## Project Context
${projectContext.summary}

${fileContext}

## Full File Content${contextNote}
\`\`\`${file.language || (0, path_helpers_1.getLanguageFromPath)(file.path)}
${numberedContent}
\`\`\`

## Candidate Findings to Validate (${findings.length} total)

${candidatesText}

---

Please validate each candidate finding. Return a JSON array with your decision for each.
Remember: Be AGGRESSIVE in rejecting false positives. Use the full file context and project architecture to make informed decisions.`;
}
// ============================================================================
// Multi-File Request Builder (Phase 2 Optimization)
// ============================================================================
/**
 * Build a multi-file validation request (Phase 2 optimization)
 * Batches multiple files into a single API call to reduce overhead
 */
function buildMultiFileValidationRequest(fileDataList, projectContext, options) {
    const mode = options?.contextMode ?? 'scoped';
    const filesContent = fileDataList.map(({ file, findings }, fileIndex) => {
        // Get file content (full or scoped)
        const { numberedContent, contextNote } = getFileContent(file, findings, mode, options?.extractionConfig);
        // Build candidate findings list with file-specific indices
        const candidatesText = findings.map((f, idx) => {
            return `### Candidate ${idx}
- **Rule**: ${f.title}
- **Category**: ${f.category}
- **Original Severity**: ${f.severity}
- **Line**: ${f.lineNumber}
- **Detection Layer**: ${f.layer}
- **Description**: ${f.description}
- **Flagged Code**: \`${f.lineContent.trim()}\``;
        }).join('\n\n');
        // Get file-specific context
        const fileContext = (0, project_context_builder_1.getFileValidationContext)(file, projectContext);
        return `
================================================================================
FILE ${fileIndex + 1}: ${file.path}
================================================================================

${fileContext}

### Full File Content${contextNote}
\`\`\`${file.language || (0, path_helpers_1.getLanguageFromPath)(file.path)}
${numberedContent}
\`\`\`

### Candidate Findings to Validate (${findings.length} total)

${candidatesText}`;
    }).join('\n\n');
    return `## Project Context
${projectContext.summary}

${filesContent}

---

## Response Format

For EACH file, provide a JSON object with the file path and validation results.
Return a JSON array where each element has:
- "file": the file path (e.g., "${fileDataList[0]?.file.path || 'path/to/file.ts'}")
- "validations": array of validation results for that file's candidates

Example response format:
\`\`\`json
[
  {
    "file": "src/auth.ts",
    "validations": [
      { "index": 0, "keep": true, "adjustedSeverity": "medium", "notes": "Protected by middleware" },
      { "index": 1, "keep": false }
    ]
  },
  {
    "file": "src/api.ts",
    "validations": [
      { "index": 0, "keep": true, "notes": "User input flows to SQL query" }
    ]
  }
]
\`\`\`

Remember: Be AGGRESSIVE in rejecting false positives. Use the full file context and project architecture to make informed decisions.`;
}
// ============================================================================
// Internal Helpers
// ============================================================================
function getFileContent(file, findings, mode, extractionConfig) {
    if (mode === 'full') {
        const numberedContent = file.content
            .split('\n')
            .map((line, i) => `${String(i + 1).padStart(4, ' ')} | ${line}`)
            .join('\n');
        return { numberedContent, contextNote: '' };
    }
    // Scoped mode: extract relevant regions
    const findingLineNumbers = findings.map(f => f.lineNumber);
    const extracted = (0, context_extractor_1.extractRelevantContext)(file.content, findingLineNumbers, extractionConfig);
    const contextNote = extracted.isFullFile
        ? ''
        : ` (Showing ${extracted.linesSent} of ${extracted.totalLines} lines — relevant regions around findings)`;
    return { numberedContent: extracted.content, contextNote };
}
//# sourceMappingURL=request-builder.js.map