// engine/challenge-scenarios-loader.ts — Challenge scenario pattern library (SPEC-214)
//
// Loads challenge scenario patterns from the 3-layer config system.
// Source: src/config/challenge-scenarios.json (system defaults)
// Override: data/global/challenge-scenarios-custom.json
//           data/projects/{hash}/config/challenge-scenarios-overrides.json
//
// Scenarios can be extended via JSON without modifying TypeScript source files.
import { z } from 'zod';
import { ConfigLoader } from './config-loader.js';
// ---------------------------------------------------------------------------
// Schema
// ---------------------------------------------------------------------------
export const ChallengeScenarioPatternSchema = z.object({
    id: z.string().min(1).describe('Unique scenario pattern identifier'),
    category: z
        .string()
        .min(1)
        .describe('Scenario category: data-consistency, security, scale, failure, etc.'),
    triggerKeywords: z
        .array(z.string())
        .describe('Keywords in spec content that activate this scenario'),
    scenario: z.string().min(1).describe('Short description of the failure scenario'),
    probability: z.enum(['low', 'medium', 'high']).describe('Probability of this failure occurring'),
    impact: z.enum(['low', 'medium', 'high', 'critical']).describe('Impact if this failure occurs'),
    handlingKeywords: z
        .array(z.string())
        .describe('Keywords that indicate the spec already addresses this scenario'),
    handlingFound: z.string().describe('Message when handling is detected in spec'),
    handlingMissing: z.string().describe('Message when handling is absent from spec'),
    requiredHandling: z.string().describe('What the spec should implement to handle this scenario'),
    dataConsistency: z.string().describe('Data consistency requirements for this scenario'),
    userExperience: z.string().describe('Expected user experience for this scenario'),
});
const patternsArraySchema = z.array(ChallengeScenarioPatternSchema);
// ---------------------------------------------------------------------------
// Loader singleton
// ---------------------------------------------------------------------------
let _loader = null;
function getLoader() {
    _loader ??= new ConfigLoader('challenge-scenarios', patternsArraySchema);
    return _loader;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Load all challenge scenario patterns from the 3-layer config system.
 */
export function loadScenarioPatterns(projectHash) {
    return getLoader().load(projectHash);
}
/**
 * Filter patterns that are triggered by content of the spec.
 * A pattern is triggered if ANY of its triggerKeywords appear in specContent,
 * OR if it has no trigger keywords (always-on scenarios).
 */
export function getTriggeredPatterns(specContent, projectHash) {
    const lower = specContent.toLowerCase();
    const all = loadScenarioPatterns(projectHash);
    return all.filter((pattern) => {
        if (pattern.triggerKeywords.length === 0) {
            return true; // always-on pattern
        }
        return pattern.triggerKeywords.some((kw) => lower.includes(kw.toLowerCase()));
    });
}
/**
 * Get patterns filtered by category.
 */
export function getPatternsByCategory(category, projectHash) {
    return loadScenarioPatterns(projectHash).filter((p) => p.category === category);
}
/**
 * Determine currentHandling label based on whether the spec content
 * includes any of the pattern's handlingKeywords.
 */
export function resolveHandlingLabel(pattern, specContent) {
    const lower = specContent.toLowerCase();
    const addressed = pattern.handlingKeywords.some((kw) => lower.includes(kw.toLowerCase()));
    return addressed ? pattern.handlingFound : pattern.handlingMissing;
}
//# sourceMappingURL=challenge-scenarios-loader.js.map