"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Volume13ComplianceDecisionEngine = void 0;
exports.createComplianceDecisionEngine = createComplianceDecisionEngine;
const ComplianceDecisionEngine_js_1 = require("./ComplianceDecisionEngine.js");
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
/**
 * Production compliance engine: Volume 13 rules + internal sanctions list.
 * Vendor adapter hook via COMPLIANCE_VENDOR env (worldcheck|chainalysis|internal).
 */
class Volume13ComplianceDecisionEngine {
    mockFallback;
    rules;
    sanctions;
    vendor;
    auditCounter = 0;
    constructor(options) {
        const root = process.env.PROXMOX_ROOT || node_path_1.default.resolve(process.cwd(), '../..');
        const rulesPath = options?.rulesPath ||
            node_path_1.default.join(root, 'config/hybx-omnl-dbis/volume13-aml-decision-rules.v1.json');
        const sanctionsPath = options?.sanctionsPath ||
            node_path_1.default.join(root, 'config/compliance/internal-sanctions-list.v1.json');
        this.mockFallback = new ComplianceDecisionEngine_js_1.MockComplianceDecisionEngine();
        this.vendor = process.env.COMPLIANCE_VENDOR || 'internal';
        this.rules = node_fs_1.default.existsSync(rulesPath)
            ? JSON.parse(node_fs_1.default.readFileSync(rulesPath, 'utf8')).rules ?? []
            : [];
        this.sanctions = node_fs_1.default.existsSync(sanctionsPath)
            ? JSON.parse(node_fs_1.default.readFileSync(sanctionsPath, 'utf8')).entries ?? []
            : [];
    }
    nextAuditId() {
        this.auditCounter += 1;
        return `audit-v13-${this.vendor}-${this.auditCounter}`;
    }
    matchSanctions(entityId) {
        const needle = entityId.toLowerCase();
        return this.sanctions.some((e) => e.active !== false &&
            (e.id.toLowerCase() === needle ||
                (e.names ?? []).some((n) => needle.includes(n.toLowerCase()))));
    }
    async evaluateTransaction(input) {
        const hints = input.riskHints ?? [];
        if (this.matchSanctions(input.entityId) || hints.includes('sanctions_hit')) {
            return {
                decision: 'block',
                risk_score: 100,
                reason_codes: ['SANCTIONS_HIT', `VENDOR_${this.vendor.toUpperCase()}`],
                evidence_refs: ['volume13-sanctions-screen'],
                manual_review_required: true,
                audit_event_id: this.nextAuditId(),
            };
        }
        for (const rule of this.rules) {
            const trigger = rule.trigger ?? {};
            if (trigger.riskHints?.some((h) => hints.includes(h))) {
                return {
                    decision: rule.decision ?? 'review',
                    risk_score: 80,
                    reason_codes: [rule.id],
                    evidence_refs: [`volume13-${rule.id}`],
                    manual_review_required: true,
                    audit_event_id: this.nextAuditId(),
                };
            }
            if (trigger.amountGte != null && Number(input.amount) >= trigger.amountGte) {
                return {
                    decision: rule.decision ?? 'hold',
                    risk_score: 65,
                    reason_codes: [rule.id, 'AMOUNT_THRESHOLD'],
                    evidence_refs: [`volume13-${rule.id}`],
                    manual_review_required: true,
                    audit_event_id: this.nextAuditId(),
                };
            }
        }
        return this.mockFallback.evaluateTransaction(input);
    }
    async evaluateEntity(entityId, tenantId) {
        if (this.matchSanctions(entityId)) {
            return {
                decision: 'reject',
                risk_score: 95,
                reason_codes: ['ENTITY_SANCTIONS_MATCH'],
                evidence_refs: [`sanctions-${entityId}`],
                manual_review_required: true,
                audit_event_id: this.nextAuditId(),
            };
        }
        return this.mockFallback.evaluateEntity(entityId, tenantId);
    }
    async evaluateCounterparty(counterpartyId, tenantId) {
        return this.evaluateEntity(counterpartyId, tenantId);
    }
}
exports.Volume13ComplianceDecisionEngine = Volume13ComplianceDecisionEngine;
function createComplianceDecisionEngine() {
    if (process.env.OMNL_COMPLIANCE_GATE !== '1') {
        return new ComplianceDecisionEngine_js_1.MockComplianceDecisionEngine();
    }
    return new Volume13ComplianceDecisionEngine();
}
