import { logger } from '../../logger.js';
/**
 * Registry for email domain configurations
 * Provides fast lookups and validation for domains
 */
export class DomainRegistry {
    domains = new Map();
    defaults;
    constructor(domainConfigs, defaults) {
        // Set defaults
        this.defaults = {
            dnsMode: defaults?.dnsMode || 'external-dns',
            ...this.getDefaultDkimConfig(),
            ...defaults?.dkim,
            rateLimits: defaults?.rateLimits
        };
        // Process and store domain configurations
        for (const config of domainConfigs) {
            const processedConfig = this.applyDefaults(config);
            this.domains.set(config.domain.toLowerCase(), processedConfig);
            logger.log('info', `Registered domain: ${config.domain} with DNS mode: ${processedConfig.dnsMode}`);
        }
    }
    /**
     * Get default DKIM configuration
     */
    getDefaultDkimConfig() {
        return {
            selector: 'default',
            keySize: 2048,
            rotateKeys: false,
            rotationInterval: 90
        };
    }
    /**
     * Apply defaults to a domain configuration
     */
    applyDefaults(config) {
        return {
            ...config,
            dnsMode: config.dnsMode || this.defaults.dnsMode,
            dkim: {
                ...this.getDefaultDkimConfig(),
                ...this.defaults,
                ...config.dkim
            },
            rateLimits: {
                ...this.defaults.rateLimits,
                ...config.rateLimits,
                outbound: {
                    ...this.defaults.rateLimits?.outbound,
                    ...config.rateLimits?.outbound
                },
                inbound: {
                    ...this.defaults.rateLimits?.inbound,
                    ...config.rateLimits?.inbound
                }
            }
        };
    }
    /**
     * Check if a domain is registered
     */
    isDomainRegistered(domain) {
        return this.domains.has(domain.toLowerCase());
    }
    /**
     * Check if an email address belongs to a registered domain
     */
    isEmailRegistered(email) {
        const domain = this.extractDomain(email);
        if (!domain)
            return false;
        return this.isDomainRegistered(domain);
    }
    /**
     * Get domain configuration
     */
    getDomainConfig(domain) {
        return this.domains.get(domain.toLowerCase());
    }
    /**
     * Get domain configuration for an email address
     */
    getEmailDomainConfig(email) {
        const domain = this.extractDomain(email);
        if (!domain)
            return undefined;
        return this.getDomainConfig(domain);
    }
    /**
     * Extract domain from email address
     */
    extractDomain(email) {
        const parts = email.toLowerCase().split('@');
        if (parts.length !== 2)
            return null;
        return parts[1];
    }
    /**
     * Get all registered domains
     */
    getAllDomains() {
        return Array.from(this.domains.keys());
    }
    /**
     * Get all domain configurations
     */
    getAllConfigs() {
        return Array.from(this.domains.values());
    }
    /**
     * Get domains by DNS mode
     */
    getDomainsByMode(mode) {
        return Array.from(this.domains.values()).filter(config => config.dnsMode === mode);
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xhc3Nlcy5kb21haW4ucmVnaXN0cnkuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi90cy9tYWlsL3JvdXRpbmcvY2xhc3Nlcy5kb21haW4ucmVnaXN0cnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLGlCQUFpQixDQUFDO0FBRXpDOzs7R0FHRztBQUNILE1BQU0sT0FBTyxjQUFjO0lBQ2pCLE9BQU8sR0FBb0MsSUFBSSxHQUFHLEVBQUUsQ0FBQztJQUNyRCxRQUFRLENBR2Q7SUFFRixZQUNFLGFBQW1DLEVBQ25DLFFBSUM7UUFFRCxlQUFlO1FBQ2YsSUFBSSxDQUFDLFFBQVEsR0FBRztZQUNkLE9BQU8sRUFBRSxRQUFRLEVBQUUsT0FBTyxJQUFJLGNBQWM7WUFDNUMsR0FBRyxJQUFJLENBQUMsb0JBQW9CLEVBQUU7WUFDOUIsR0FBRyxRQUFRLEVBQUUsSUFBSTtZQUNqQixVQUFVLEVBQUUsUUFBUSxFQUFFLFVBQVU7U0FDakMsQ0FBQztRQUVGLDBDQUEwQztRQUMxQyxLQUFLLE1BQU0sTUFBTSxJQUFJLGFBQWEsRUFBRSxDQUFDO1lBQ25DLE1BQU0sZUFBZSxHQUFHLElBQUksQ0FBQyxhQUFhLENBQUMsTUFBTSxDQUFDLENBQUM7WUFDbkQsSUFBSSxDQUFDLE9BQU8sQ0FBQyxHQUFHLENBQUMsTUFBTSxDQUF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