import { createHash } from 'node:crypto';
export const COMPILED_RULES_SCHEMA_VERSION = 'v1';
function uniqueSorted(values) {
    return [...new Set([...values].map((v) => v.trim()).filter(Boolean))].sort();
}
export function compilePolicyToRules(config) {
    const blockedTools = new Set();
    const allowedTools = new Set();
    const blockedMethodSubstrings = new Set();
    for (const rule of config.policy.rules) {
        if (rule.tools?.deny?.length) {
            for (const item of rule.tools.deny)
                blockedTools.add(item);
        }
        if (rule.tools?.allow?.length) {
            for (const item of rule.tools.allow)
                allowedTools.add(item);
        }
        if (rule.toolCategories?.deny?.length) {
            for (const item of rule.toolCategories.deny)
                blockedMethodSubstrings.add(item);
        }
    }
    return {
        schemaVersion: COMPILED_RULES_SCHEMA_VERSION,
        generatedAt: new Date().toISOString(),
        sourcePolicyVersion: config.version,
        minProxyVersion: '0.1.0',
        blockedTools: uniqueSorted(blockedTools),
        allowedTools: uniqueSorted(allowedTools),
        blockedMethodSubstrings: uniqueSorted(blockedMethodSubstrings),
        policyMode: config.policy.mode,
        defaultAction: config.policy.default_action ?? 'pass',
    };
}
export function compiledRulesEtag(rules) {
    const hash = createHash('sha256')
        .update(JSON.stringify(rules))
        .digest('hex');
    return `W/"${hash}"`;
}
//# sourceMappingURL=compiled-rules.js.map