"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.IsPinCodeWithSettingConstraint = void 0;
exports.IsPinCodeWithSetting = IsPinCodeWithSetting;
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 IsPinCodeWithSettingConstraint = class IsPinCodeWithSettingConstraint {
    constructor() {
        this.settings = null;
    }
    async validate(pinCode, args) {
        this.settings = null;
        if (!pinCode) {
            return false;
        }
        // Validate that pinCode contains only digits
        const digitRegex = /^\d+$/;
        if (!digitRegex.test(pinCode)) {
            return false;
        }
        try {
            const settingService = service_locator_1.ValidatorServiceLocator.getSettingService();
            this.settings = await settingService.getSystemSettings();
            const codeLength = this.settings.setting['mfa-email-code-length'] || 6;
            // Validate pin code length matches the configured length
            return pinCode.length === Number(codeLength);
        }
        catch (error) {
            console.warn('[IsPinCodeWithSettingConstraint] Error getting SettingService:', error);
            return true; // Allow if service is not available
        }
    }
    defaultMessage() {
        const locale = (0, locale_context_1.getLocaleFromContext)();
        if (this.settings) {
            const codeLength = this.settings.setting['mfa-email-code-length'] || 6;
            const replacements = {
                '{length}': String(codeLength),
            };
            let message = (0, api_locale_1.getLocaleText)('pinCode.invalidLength', locale, 'PIN code does not meet the required length defined in system settings.');
            // Replace placeholders
            Object.entries(replacements).forEach(([placeholder, value]) => {
                message = message.replace(new RegExp(placeholder, 'g'), value);
            });
            return message;
        }
        return (0, api_locale_1.getLocaleText)('pinCode.invalidLength', locale, 'PIN code does not meet the required length defined in system settings.');
    }
};
exports.IsPinCodeWithSettingConstraint = IsPinCodeWithSettingConstraint;
exports.IsPinCodeWithSettingConstraint = IsPinCodeWithSettingConstraint = __decorate([
    (0, class_validator_1.ValidatorConstraint)({ async: true }),
    (0, common_1.Injectable)()
], IsPinCodeWithSettingConstraint);
function IsPinCodeWithSetting(validationOptions) {
    return function (object, propertyName) {
        (0, class_validator_1.registerDecorator)({
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            constraints: [],
            validator: IsPinCodeWithSettingConstraint,
        });
    };
}
//# sourceMappingURL=is-pin-code-with-setting.validator.js.map