"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManyToOneRelationFieldCrudManager = void 0;
const string_helper_1 = require("../string.helper");
const common_1 = require("@nestjs/common");
const class_validator_1 = require("class-validator");
const is_parsable_int_1 = require("../../validators/is-parsable-int");
class ManyToOneRelationFieldCrudManager {
    constructor(options) {
        this.options = options;
        this.idFieldName = `${options.fieldName}Id`;
        this.userKeyFieldName = `${options.fieldName}UserKey`;
    }
    validate(dto) {
        const fieldId = dto[this.idFieldName];
        const fieldUserKey = dto[this.userKeyFieldName];
        return this.applyValidations(fieldId, fieldUserKey);
    }
    applyValidations(fieldId, fieldUserKey) {
        const errors = [];
        this.isApplyRequiredValidation() && (0, class_validator_1.isEmpty)(fieldId) && (0, class_validator_1.isEmpty)(fieldUserKey) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required. Either pass ${this.idFieldName} or ${this.userKeyFieldName}.` }) : "no errors";
        if ((0, class_validator_1.isNotEmpty)(fieldId)) {
            errors.push(...this.applyIdFormatValidations(fieldId));
        }
        if ((0, class_validator_1.isNotEmpty)(fieldUserKey)) {
            errors.push(...this.applyUserKeyFormatValidations(fieldUserKey));
        }
        return errors;
    }
    applyIdFormatValidations(fieldId) {
        const errors = [];
        !(0, is_parsable_int_1.IsParsableInt)(fieldId) ? errors.push({ field: this.options.fieldName, error: 'Field is not a integer' }) : "no errors";
        return errors;
    }
    applyUserKeyFormatValidations(fieldUserKey) {
        const errors = [];
        !(0, class_validator_1.isString)(fieldUserKey) ? errors.push({ field: this.options.fieldName, error: 'Field is not a string' }) : "no errors";
        if ((0, class_validator_1.isEmpty)(this.options.relationCoModelUserKeyFieldName)) {
            errors.push({ field: this.options.fieldName, error: `UserKey field name is not defined in the model ${this.options.relationCoModelSingularName}` });
        }
        return errors;
    }
    async transformForCreate(dto) {
        const fieldId = dto[this.idFieldName];
        const fieldUserKeyValue = dto[this.userKeyFieldName];
        if (((0, class_validator_1.isEmpty)(fieldId)) && (0, class_validator_1.isEmpty)(fieldUserKeyValue))
            return dto;
        if (!this.options.relationCoModelSingularName) {
            throw new common_1.BadRequestException(`ManyToOneRelationFieldCrudManager: relationCoModelSingularName is not defined in the field: ${this.options.fieldName}`);
        }
        const coModelEntityName = (0, string_helper_1.classify)(this.options.relationCoModelSingularName);
        if ((0, class_validator_1.isNotEmpty)(fieldId)) {
            dto[this.options.fieldName] = await this.options.entityManager.getRepository(coModelEntityName).findOneBy({ id: fieldId });
            if (this.options.required && (0, class_validator_1.isEmpty)(dto[this.options.fieldName])) {
                throw new Error(`Field ${this.options.fieldName} resolution failed. Record with id: ${fieldId} not found in ${this.options.relationCoModelSingularName}`);
            }
        }
        else {
            dto[this.options.fieldName] = await this.options.entityManager.getRepository(coModelEntityName).findOneBy({ [this.options.relationCoModelUserKeyFieldName]: fieldUserKeyValue });
            if (this.options.required && (0, class_validator_1.isEmpty)(dto[this.options.fieldName])) {
                throw new Error(`Field ${this.options.fieldName} resolution failed. Record with userKey: ${this.options.relationCoModelUserKeyFieldName}: ${fieldUserKeyValue} not found in ${this.options.relationCoModelSingularName}`);
            }
        }
        return dto;
    }
    isApplyRequiredValidation() {
        return this.options.required;
    }
}
exports.ManyToOneRelationFieldCrudManager = ManyToOneRelationFieldCrudManager;
//# sourceMappingURL=ManyToOneRelationFieldCrudManager.js.map