"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectionStaticFieldCrudManager = void 0;
const class_validator_1 = require("class-validator");
const create_field_metadata_dto_1 = require("../../dtos/create-field-metadata.dto");
class SelectionStaticFieldCrudManager {
    constructor(options) {
        this.options = options;
    }
    async validate(dto) {
        const fieldValue = dto[this.options.fieldName];
        const isMultiSelect = this.options?.isMultiSelect;
        if (isMultiSelect && fieldValue) {
            const arrayCheck = this.parseAndValidateArray(fieldValue);
            if (!arrayCheck.isValid) {
                return [
                    {
                        field: this.options.fieldName,
                        error: `Field: ${this.options.fieldName} must be a valid array`,
                    },
                ];
            }
            const values = arrayCheck.values;
            if (this.isApplyRequiredValidation() && values.length === 0) {
                return [
                    {
                        field: this.options.fieldName,
                        error: `Field: ${this.options.fieldName} is required`,
                    },
                ];
            }
            const allErrors = await Promise.all(values.map((val) => this.applyValidations(val)));
            return allErrors.flat();
        }
        else {
            return this.applyValidations(fieldValue);
        }
    }
    parseAndValidateArray(fieldValue) {
        if (Array.isArray(fieldValue)) {
            return { isValid: true, values: fieldValue };
        }
        try {
            const parsed = typeof fieldValue === 'string' ? JSON.parse(fieldValue) : null;
            if (Array.isArray(parsed)) {
                return { isValid: true, values: parsed };
            }
        }
        catch {
        }
        return { isValid: false, values: [] };
    }
    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.isValidSelectionValueType(fieldValue, this.options.selectionValueType) ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : "no errors";
        !this.isValidSelectionStaticValue(fieldValue, this.options.selectionValueType, this.options.selectionStaticValues) ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : "no errors";
        return errors;
    }
    transformForCreate(dto) {
        return dto;
    }
    isValidSelectionValueType(fieldValue, selectionValueType) {
        switch (selectionValueType) {
            case create_field_metadata_dto_1.SelectionValueType.string:
                return (0, class_validator_1.isString)(fieldValue);
            case create_field_metadata_dto_1.SelectionValueType.int:
                return (0, class_validator_1.isInt)(fieldValue);
            default:
                throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);
        }
    }
    isValidSelectionStaticValue(fieldValue, selectionValueType, selectionStaticValues) {
        switch (selectionValueType) {
            case create_field_metadata_dto_1.SelectionValueType.string:
                return selectionStaticValues.map(v => v.split(":")[0]).includes(fieldValue);
            case create_field_metadata_dto_1.SelectionValueType.int:
                return selectionStaticValues.map(v => Number(v.split(":")[0])).includes(fieldValue);
            default:
                throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);
        }
    }
    isApplyRequiredValidation() {
        return this.options.required;
    }
}
exports.SelectionStaticFieldCrudManager = SelectionStaticFieldCrudManager;
//# sourceMappingURL=SelectionStaticFieldCrudManager.js.map