// engine/code-quality-thresholds-loader.ts — Configurable code quality thresholds (SPEC-210)
//
// Provides per-language code quality thresholds via the 3-layer ConfigLoader pattern.
// Source: src/config/code-quality-thresholds.json (system defaults)
// Override: data/global/code-quality-thresholds-custom.json (global)
//           data/projects/{hash}/config/code-quality-thresholds-overrides.json (project)
//
// USAGE:
//   const thresholds = getThresholds('typescript');
//   if (lines.length > thresholds.maxFileLines) { ... }
import { z } from 'zod';
import { ConfigLoader } from './config-loader.js';
// ---------------------------------------------------------------------------
// Schema
// ---------------------------------------------------------------------------
export const CodeQualityThresholdSchema = z.object({
    id: z.string().min(1).describe('Language identifier (e.g. "typescript", "python", "default")'),
    language: z.string().min(1).describe('Human-readable language name'),
    maxFileLines: z.number().int().positive().describe('Maximum lines per file'),
    maxFunctionLines: z.number().int().positive().describe('Maximum lines per function'),
    maxParams: z.number().int().positive().describe('Maximum function parameters'),
    maxNestingDepth: z.number().int().positive().describe('Maximum nesting depth'),
    maxCyclomaticComplexity: z.number().int().positive().describe('Maximum cyclomatic complexity'),
    minCoverageStatements: z.number().min(0).max(100).describe('Minimum statement coverage %'),
    minCoverageBranches: z.number().min(0).max(100).describe('Minimum branch coverage %'),
    minCoverageFunctions: z.number().min(0).max(100).describe('Minimum function coverage %'),
    minCoverageLines: z.number().min(0).max(100).describe('Minimum line coverage %'),
    duplicateLineThreshold: z
        .number()
        .int()
        .positive()
        .describe('Number of identical lines to flag as duplication'),
});
const thresholdsArraySchema = z.array(CodeQualityThresholdSchema);
// ---------------------------------------------------------------------------
// Loader singleton
// ---------------------------------------------------------------------------
let _loader = null;
function getLoader() {
    _loader ??= new ConfigLoader('code-quality-thresholds', thresholdsArraySchema);
    return _loader;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Load all code quality thresholds, merging system defaults with global/project overrides.
 */
export function loadThresholds(projectHash) {
    return getLoader().load(projectHash);
}
/**
 * Get thresholds for a specific language. Falls back to 'default' if no exact match.
 * Language comparison is case-insensitive.
 */
export function getThresholds(language, projectHash) {
    const all = loadThresholds(projectHash);
    const normalized = language.toLowerCase().trim();
    const exact = all.find((t) => t.language.toLowerCase() === normalized);
    if (exact !== undefined) {
        return exact;
    }
    // Try matching by id as well (e.g. "ts" → no match, "typescript" → match)
    const byId = all.find((t) => t.id.toLowerCase() === normalized);
    if (byId !== undefined) {
        return byId;
    }
    const fallback = all.find((t) => t.id === 'default');
    if (fallback !== undefined) {
        return fallback;
    }
    // Should never happen if the config JSON is intact, but be defensive
    return {
        id: 'default',
        language: 'default',
        maxFileLines: 500,
        maxFunctionLines: 60,
        maxParams: 5,
        maxNestingDepth: 5,
        maxCyclomaticComplexity: 15,
        minCoverageStatements: 80,
        minCoverageBranches: 70,
        minCoverageFunctions: 80,
        minCoverageLines: 80,
        duplicateLineThreshold: 4,
    };
}
//# sourceMappingURL=code-quality-thresholds-loader.js.map