"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.IsStrongPasswordWithSettingsConstraint = void 0;
exports.IsStrongPasswordWithSettings = IsStrongPasswordWithSettings;
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 IsStrongPasswordWithSettingsConstraint = class IsStrongPasswordWithSettingsConstraint {
    constructor() {
        this.failedRule = null;
        this.settings = null;
    }
    async validate(password, args) {
        this.failedRule = null;
        this.settings = null;
        if (!password) {
            this.failedRule = 'password.required';
            return false;
        }
        try {
            const settingService = service_locator_1.ValidatorServiceLocator.getSettingService();
            this.settings = await settingService.getSystemSettings();
            const minLength = this.settings.setting['password-min-length'] || 6;
            const minLowercase = this.settings.setting['password-min-lowercase'] || 0;
            const minUppercase = this.settings.setting['password-min-uppercase'] || 0;
            const minNumbers = this.settings.setting['password-min-numbers'] || 0;
            const minSymbols = this.settings.setting['password-min-symbols'] || 0;
            // Validate length
            if (password.length < minLength) {
                this.failedRule = 'password.minLength';
                return false;
            }
            // Count character types
            const lowercase = (password.match(/[a-z]/g) || []).length;
            const uppercase = (password.match(/[A-Z]/g) || []).length;
            const numbers = (password.match(/[0-9]/g) || []).length;
            const symbols = (password.match(/[^a-zA-Z0-9]/g) || []).length;
            if (lowercase < minLowercase) {
                this.failedRule = 'password.minLowercase';
                return false;
            }
            if (uppercase < minUppercase) {
                this.failedRule = 'password.minUppercase';
                return false;
            }
            if (numbers < minNumbers) {
                this.failedRule = 'password.minNumbers';
                return false;
            }
            if (symbols < minSymbols) {
                this.failedRule = 'password.minSymbols';
                return false;
            }
            return true;
        }
        catch (error) {
            console.warn('[IsStrongPasswordWithSettingsConstraint] Error getting SettingService:', error);
            return true; // Allow if service is not available
        }
    }
    defaultMessage() {
        const locale = (0, locale_context_1.getLocaleFromContext)();
        if (this.failedRule && this.settings) {
            const minLength = this.settings.setting['password-min-length'] || 6;
            const minLowercase = this.settings.setting['password-min-lowercase'] || 0;
            const minUppercase = this.settings.setting['password-min-uppercase'] || 0;
            const minNumbers = this.settings.setting['password-min-numbers'] || 0;
            const minSymbols = this.settings.setting['password-min-symbols'] || 0;
            const replacements = {
                '{minLength}': String(minLength),
                '{minLowercase}': String(minLowercase),
                '{minUppercase}': String(minUppercase),
                '{minNumbers}': String(minNumbers),
                '{minSymbols}': String(minSymbols),
            };
            let message = (0, api_locale_1.getLocaleText)(this.failedRule, locale, 'Password does not meet the security requirements.');
            // Replace placeholders
            Object.entries(replacements).forEach(([placeholder, value]) => {
                message = message.replace(new RegExp(placeholder, 'g'), value);
            });
            return message;
        }
        return (0, api_locale_1.getLocaleText)('password.weakPassword', locale, 'Password does not meet the security requirements defined in system settings.');
    }
};
exports.IsStrongPasswordWithSettingsConstraint = IsStrongPasswordWithSettingsConstraint;
exports.IsStrongPasswordWithSettingsConstraint = IsStrongPasswordWithSettingsConstraint = __decorate([
    (0, class_validator_1.ValidatorConstraint)({ async: true }),
    (0, common_1.Injectable)()
], IsStrongPasswordWithSettingsConstraint);
function IsStrongPasswordWithSettings(validationOptions) {
    return function (object, propertyName) {
        (0, class_validator_1.registerDecorator)({
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            constraints: [],
            validator: IsStrongPasswordWithSettingsConstraint,
        });
    };
}
//# sourceMappingURL=is-strong-password-with-settings.validator.js.map