"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BigIntFieldCrudManager = void 0;
const class_validator_1 = require("class-validator");
class BigIntFieldCrudManager {
    constructor(options) {
        this.options = options;
    }
    validate(createDto) {
        let fieldValue = createDto[this.options.fieldName];
        if (fieldValue !== undefined && fieldValue !== null) {
            try {
                if (typeof fieldValue === 'string' || typeof fieldValue === 'number') {
                    fieldValue = BigInt(fieldValue);
                }
            }
            catch (err) {
                return [{ field: this.options.fieldName, error: 'Invalid numeric value' }];
            }
        }
        return this.applyValidations(fieldValue);
    }
    applyValidations(fieldValue) {
        const errors = [];
        this.isApplyRequiredValidation() && (0, class_validator_1.isEmpty)(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : "no errors";
        if ((0, class_validator_1.isNotEmpty)(fieldValue)) {
            errors.push(...this.applyFormatValidations(fieldValue));
        }
        return errors;
    }
    applyFormatValidations(fieldValue) {
        const errors = [];
        !this.isBigInt(fieldValue) ? errors.push({ field: this.options.fieldName, error: 'Field is not a bigint' }) : "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";
        return errors;
    }
    transformForCreate(createDto) {
        return createDto;
    }
    isApplyMinValidation() {
        return this.options.min > 0;
    }
    isApplyMaxValidation() {
        return this.options.max > 0;
    }
    isApplyRequiredValidation() {
        return this.options.required;
    }
    isBigInt(value) {
        const valueType = typeof value;
        return valueType === 'bigint' || (valueType === 'number' && Number.isFinite(value));
    }
}
exports.BigIntFieldCrudManager = BigIntFieldCrudManager;
//# sourceMappingURL=BigIntFieldCrudManager.js.map