"use strict";
/**
 * Certificate verification source that handles both CRL and OCSP methods
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CertificateVerificationSource = void 0;
const Resource_ts_1 = require("../../resources/Resource.js");
// Import verification functions
let performCRLCheck;
let performOCSPCheck;
// Lazy load to avoid circular dependencies
async function loadVerificationFunctions() {
    if (!performCRLCheck) {
        const crlModule = await import('./crlVerification.js');
        performCRLCheck = crlModule.performCRLCheck;
    }
    if (!performOCSPCheck) {
        const ocspModule = await import('./ocspVerification.js');
        performOCSPCheck = ocspModule.performOCSPCheck;
    }
}
/**
 * Certificate Verification Source that can handle both CRL and OCSP
 */
class CertificateVerificationSource extends Resource_ts_1.Resource {
    async get(query) {
        const id = query.id;
        // Get the certificate data from requestContext
        const context = this.getContext();
        const requestContext = context?.requestContext;
        if (!requestContext || !requestContext.certPem || !requestContext.issuerPem) {
            // Likely a source request for an expired entry - we can't verify without cert and issuer data
            return null;
        }
        const { certPem: certPemStr, issuerPem: issuerPemStr, ocspUrls, config } = requestContext;
        // Determine method from cache key
        let method;
        if (id.startsWith('crl:')) {
            method = 'crl';
        }
        else if (id.startsWith('ocsp:')) {
            method = 'ocsp';
        }
        else {
            method = 'unknown';
        }
        // Load verification functions
        await loadVerificationFunctions();
        // Perform verification based on method
        let result;
        let methodConfig;
        if (method === 'crl') {
            methodConfig = config.crl;
            // Pass distributionPoint as an array if available (for CRL fetch)
            const crlUrls = requestContext.distributionPoint ? [requestContext.distributionPoint] : undefined;
            result = await performCRLCheck(certPemStr, issuerPemStr, methodConfig, crlUrls);
        }
        else if (method === 'ocsp') {
            methodConfig = config.ocsp;
            result = await performOCSPCheck(certPemStr, issuerPemStr, methodConfig, ocspUrls);
        }
        else {
            throw new Error(`Unsupported verification method: ${method} for ID: ${id}`);
        }
        // Handle result consistently
        const expiresAt = Date.now() + methodConfig.cacheTtl;
        return {
            certificate_id: id,
            status: result.status,
            reason: result.reason,
            checked_at: Date.now(),
            expiresAt,
            method,
        };
    }
}
exports.CertificateVerificationSource = CertificateVerificationSource;
//# sourceMappingURL=certificateVerificationSource.js.map