"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsEmailWithSettingsConstraint = void 0;
exports.IsEmailWithSettings = IsEmailWithSettings;
const api_locale_1 = require("@hed-hog/api-locale");
const common_1 = require("@nestjs/common");
const class_validator_1 = require("class-validator");
const locale_context_1 = require("../utils/locale-context");
const service_locator_1 = require("./service-locator");
let IsEmailWithSettingsConstraint = class IsEmailWithSettingsConstraint {
    async validate(email, args) {
        var _a;
        if (!email) {
            return false;
        }
        // Validate email format (basic regex)
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            return false;
        }
        // Extract domain from email
        const domain = (_a = email.split('@')[1]) === null || _a === void 0 ? void 0 : _a.toLowerCase();
        if (!domain) {
            return false;
        }
        try {
            const settingService = service_locator_1.ValidatorServiceLocator.getSettingService();
            const settings = await settingService.getSystemSettings();
            const blacklist = settings.setting['email-host-blacklist'] || [];
            const whitelist = settings.setting['email-host-whitelist'] || [];
            // blacklist e whitelist já são arrays
            const blacklistHosts = Array.isArray(blacklist)
                ? blacklist.map((host) => host.trim().toLowerCase()).filter((host) => host.length > 0)
                : [];
            const whitelistHosts = Array.isArray(whitelist)
                ? whitelist.map((host) => host.trim().toLowerCase()).filter((host) => host.length > 0)
                : [];
            // If whitelist is defined and not empty, domain must be in whitelist
            if (whitelistHosts.length > 0) {
                return whitelistHosts.includes(domain);
            }
            // If blacklist is defined, domain must not be in blacklist
            if (blacklistHosts.length > 0) {
                return !blacklistHosts.includes(domain);
            }
            // No restrictions, email is valid
            return true;
        }
        catch (error) {
            console.warn('[IsEmailWithSettingsConstraint] Error getting SettingService:', error);
            return true; // Allow if service is not available
        }
    }
    defaultMessage() {
        return (0, api_locale_1.getLocaleText)('email.invalidEmail', (0, locale_context_1.getLocaleFromContext)(), 'Email does not meet the security requirements defined in system settings.');
    }
};
exports.IsEmailWithSettingsConstraint = IsEmailWithSettingsConstraint;
exports.IsEmailWithSettingsConstraint = IsEmailWithSettingsConstraint = __decorate([
    (0, class_validator_1.ValidatorConstraint)({ async: true }),
    (0, common_1.Injectable)()
], IsEmailWithSettingsConstraint);
function IsEmailWithSettings(validationOptions) {
    return function (object, propertyName) {
        (0, class_validator_1.registerDecorator)({
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            constraints: [],
            validator: IsEmailWithSettingsConstraint,
        });
    };
}
//# sourceMappingURL=is-email-with-settings.validator.js.map