"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PasswordFieldCrudManager = void 0;
const class_validator_1 = require("class-validator");
class PasswordFieldCrudManager {
    constructor(options) {
        this.options = options;
    }
    validate(dto) {
        const fieldValue = dto[this.options.fieldName];
        return this.applyValidations(fieldValue, dto);
    }
    applyValidations(fieldValue, dto) {
        const errors = [];
        if (!this.options?.isUpdate && this.isApplyRequiredValidation() && (0, class_validator_1.isEmpty)(fieldValue)) {
            errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` });
        }
        if ((0, class_validator_1.isNotEmpty)(fieldValue)) {
            errors.push(...this.applyFormatValidations(fieldValue, dto));
        }
        return errors;
    }
    applyFormatValidations(fieldValue, dto) {
        const errors = [];
        !(0, class_validator_1.isString)(fieldValue) ? errors.push({ field: this.options.fieldName, error: 'Field is not a string' }) : "no errors";
        !this.isPasswordValid(fieldValue) ? errors.push({ field: this.options.fieldName, error: 'Password regex pattern is invalid' }) : "no errors";
        this.isApplyMinValidation() && !(0, class_validator_1.min)(fieldValue, this.options.min) ? errors.push({ field: this.options.fieldName, error: 'Field value is lesser than minimum required' }) : "no errors";
        this.isApplyMaxValidation() && !(0, class_validator_1.max)(fieldValue, this.options.max) ? errors.push({ field: this.options.fieldName, error: 'Field value is greater than maximum required' }) : "no errors";
        !this.isConfirmPasswordValid(dto) ? errors.push({ field: this.options.fieldName, error: 'Password and confirm password do not match' }) : "no errors";
        return errors;
    }
    async transformForCreate(dto) {
        if (dto[this.options.fieldName]) {
            dto[this.options.fieldName] = await this.options.hashingService.hash(dto[this.options.fieldName]);
            dto['passwordScheme'] = this.options.hashingService.name();
            dto['passwordHashVersion'] = this.options.hashingService.currentVersion();
        }
        return dto;
    }
    isApplyMinValidation() {
        return this.options.min > 0;
    }
    isApplyMaxValidation() {
        return this.options.max > 0;
    }
    isPasswordValid(password) {
        return (0, class_validator_1.matches)(password, new RegExp(this.options.regexPattern ?? String.raw `^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).*$`));
    }
    isConfirmPasswordValid(dto) {
        const passwordFieldName = this.options.fieldName;
        const confirmPasswordFieldName = `${passwordFieldName}Confirm`;
        return dto[passwordFieldName] === dto[confirmPasswordFieldName];
    }
    isApplyRequiredValidation() {
        return this.options.required;
    }
}
exports.PasswordFieldCrudManager = PasswordFieldCrudManager;
//# sourceMappingURL=PasswordFieldCrudManager.js.map