function globToRegExp(pattern) {
    const escaped = pattern
        .replace(/[.+^${}()|[\]\\]/g, '\\$&')
        .replace(/\*\*/g, '::DOUBLE_STAR::')
        .replace(/\*/g, '[^/]*')
        .replace(/::DOUBLE_STAR::/g, '.*');
    return new RegExp(`^${escaped}$`);
}
function matchesAny(path, patterns) {
    return patterns.find((pattern) => globToRegExp(pattern).test(path));
}
function decision(kind, target, reason, key) {
    return {
        decision: kind,
        target,
        reason,
        evidence: [{ source: 'policy', key }],
        confidence: 'high',
    };
}
function hasSafetyOverride(action, policy) {
    return policy.context.safetyOverrideActions.includes(action);
}
function classifyCandidate(candidate, input) {
    if (hasSafetyOverride(input.action, input.policy)) {
        return decision('override', candidate.path, `Action ${input.action} requires exact context according to policy.`, 'context.safetyOverrideActions');
    }
    const generated = matchesAny(candidate.path, input.policy.context.generatedPatterns);
    if (generated !== undefined) {
        return decision('exclude', candidate.path, `Matches generated context pattern ${generated}.`, 'context.generatedPatterns');
    }
    const stale = matchesAny(candidate.path, input.policy.context.stalePatterns);
    if (stale !== undefined) {
        return decision('exclude', candidate.path, `Matches stale context pattern ${stale}.`, 'context.stalePatterns');
    }
    const summarize = matchesAny(candidate.path, input.policy.context.summarizePatterns);
    if (summarize !== undefined) {
        return decision('summarize', candidate.path, `Matches summarizable context pattern ${summarize}.`, 'context.summarizePatterns');
    }
    return {
        decision: 'include',
        target: candidate.path,
        reason: candidate.reason ?? 'Candidate context is relevant and not excluded by policy.',
        evidence: [{ source: 'runtime', key: 'candidate.path', value: candidate.path }],
        confidence: 'medium',
    };
}
export function analyzeContextPreflight(input) {
    const result = { include: [], summarize: [], exclude: [] };
    for (const candidate of input.candidates) {
        const item = classifyCandidate(candidate, input);
        if (item.decision === 'exclude') {
            result.exclude.push(item);
        }
        else if (item.decision === 'summarize') {
            result.summarize.push(item);
        }
        else {
            result.include.push(item);
        }
    }
    return result;
}
//# sourceMappingURL=context-preflight.js.map