const require_rolldown_runtime = require('../../../../../../../_virtual/rolldown_runtime.js');
const require_index = require('../utils/index.js');
const require_MetadataStorage$1 = require('../metadata/MetadataStorage.js');
const require_ValidationError$1 = require('./ValidationError.js');
const require_ValidationTypes$1 = require('./ValidationTypes.js');
const require_ValidationUtils$1 = require('./ValidationUtils.js');

//#region ../../node_modules/.pnpm/class-validator@0.14.1/node_modules/class-validator/cjs/validation/ValidationExecutor.js
var require_ValidationExecutor = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/.pnpm/class-validator@0.14.1/node_modules/class-validator/cjs/validation/ValidationExecutor.js": ((exports) => {
	Object.defineProperty(exports, "__esModule", { value: true });
	const ValidationError_1 = require_ValidationError$1.require_ValidationError();
	const ValidationTypes_1 = require_ValidationTypes$1.require_ValidationTypes();
	const ValidationUtils_1 = require_ValidationUtils$1.require_ValidationUtils();
	const utils_1 = require_index.require_utils();
	const MetadataStorage_1 = require_MetadataStorage$1.require_MetadataStorage();
	/**
	* Executes validation over given object.
	*/
	var ValidationExecutor = class {
		constructor(validator, validatorOptions) {
			this.validator = validator;
			this.validatorOptions = validatorOptions;
			this.awaitingPromises = [];
			this.ignoreAsyncValidations = false;
			this.metadataStorage = (0, MetadataStorage_1.getMetadataStorage)();
		}
		execute(object, targetSchema, validationErrors) {
			var _a, _b;
			/**
			* If there is no metadata registered it means possibly the dependencies are not flatterned and
			* more than one instance is used.
			*
			* TODO: This needs proper handling, forcing to use the same container or some other proper solution.
			*/
			if (!this.metadataStorage.hasValidationMetaData && ((_a = this.validatorOptions) === null || _a === void 0 ? void 0 : _a.enableDebugMessages) === true) console.warn("No validation metadata found. No validation will be  performed. There are multiple possible reasons:\n  - There may be multiple class-validator versions installed. You will need to flatten your dependencies to fix the issue.\n  - This validation runs before any file with validation decorator was parsed by NodeJS.");
			const groups = this.validatorOptions ? this.validatorOptions.groups : void 0;
			const strictGroups = this.validatorOptions && this.validatorOptions.strictGroups || false;
			const always = this.validatorOptions && this.validatorOptions.always || false;
			/** Forbid unknown values are turned on by default and any other value than false will enable it. */
			const forbidUnknownValues = ((_b = this.validatorOptions) === null || _b === void 0 ? void 0 : _b.forbidUnknownValues) === void 0 || this.validatorOptions.forbidUnknownValues !== false;
			const targetMetadatas = this.metadataStorage.getTargetValidationMetadatas(object.constructor, targetSchema, always, strictGroups, groups);
			const groupedMetadatas = this.metadataStorage.groupByPropertyName(targetMetadatas);
			if (this.validatorOptions && forbidUnknownValues && !targetMetadatas.length) {
				const validationError = new ValidationError_1.ValidationError();
				if (!this.validatorOptions || !this.validatorOptions.validationError || this.validatorOptions.validationError.target === void 0 || this.validatorOptions.validationError.target === true) validationError.target = object;
				validationError.value = void 0;
				validationError.property = void 0;
				validationError.children = [];
				validationError.constraints = { unknownValue: "an unknown value was passed to the validate function" };
				validationErrors.push(validationError);
				return;
			}
			if (this.validatorOptions && this.validatorOptions.whitelist) this.whitelist(object, groupedMetadatas, validationErrors);
			Object.keys(groupedMetadatas).forEach((propertyName) => {
				const value = object[propertyName];
				const definedMetadatas = groupedMetadatas[propertyName].filter((metadata) => metadata.type === ValidationTypes_1.ValidationTypes.IS_DEFINED);
				const metadatas = groupedMetadatas[propertyName].filter((metadata) => metadata.type !== ValidationTypes_1.ValidationTypes.IS_DEFINED && metadata.type !== ValidationTypes_1.ValidationTypes.WHITELIST);
				if (value instanceof Promise && metadatas.find((metadata) => metadata.type === ValidationTypes_1.ValidationTypes.PROMISE_VALIDATION)) this.awaitingPromises.push(value.then((resolvedValue) => {
					this.performValidations(object, resolvedValue, propertyName, definedMetadatas, metadatas, validationErrors);
				}));
				else this.performValidations(object, value, propertyName, definedMetadatas, metadatas, validationErrors);
			});
		}
		whitelist(object, groupedMetadatas, validationErrors) {
			const notAllowedProperties = [];
			Object.keys(object).forEach((propertyName) => {
				if (!groupedMetadatas[propertyName] || groupedMetadatas[propertyName].length === 0) notAllowedProperties.push(propertyName);
			});
			if (notAllowedProperties.length > 0) if (this.validatorOptions && this.validatorOptions.forbidNonWhitelisted) notAllowedProperties.forEach((property) => {
				const validationError = this.generateValidationError(object, object[property], property);
				validationError.constraints = { [ValidationTypes_1.ValidationTypes.WHITELIST]: `property ${property} should not exist` };
				validationError.children = void 0;
				validationErrors.push(validationError);
			});
			else notAllowedProperties.forEach((property) => delete object[property]);
		}
		stripEmptyErrors(errors) {
			return errors.filter((error) => {
				if (error.children) error.children = this.stripEmptyErrors(error.children);
				if (Object.keys(error.constraints).length === 0) if (error.children.length === 0) return false;
				else delete error.constraints;
				return true;
			});
		}
		performValidations(object, value, propertyName, definedMetadatas, metadatas, validationErrors) {
			const customValidationMetadatas = metadatas.filter((metadata) => metadata.type === ValidationTypes_1.ValidationTypes.CUSTOM_VALIDATION);
			const nestedValidationMetadatas = metadatas.filter((metadata) => metadata.type === ValidationTypes_1.ValidationTypes.NESTED_VALIDATION);
			const conditionalValidationMetadatas = metadatas.filter((metadata) => metadata.type === ValidationTypes_1.ValidationTypes.CONDITIONAL_VALIDATION);
			const validationError = this.generateValidationError(object, value, propertyName);
			validationErrors.push(validationError);
			if (!this.conditionalValidations(object, value, conditionalValidationMetadatas)) return;
			this.customValidations(object, value, definedMetadatas, validationError);
			this.mapContexts(object, value, definedMetadatas, validationError);
			if (value === void 0 && this.validatorOptions && this.validatorOptions.skipUndefinedProperties === true) return;
			if (value === null && this.validatorOptions && this.validatorOptions.skipNullProperties === true) return;
			if ((value === null || value === void 0) && this.validatorOptions && this.validatorOptions.skipMissingProperties === true) return;
			this.customValidations(object, value, customValidationMetadatas, validationError);
			this.nestedValidations(value, nestedValidationMetadatas, validationError);
			this.mapContexts(object, value, metadatas, validationError);
			this.mapContexts(object, value, customValidationMetadatas, validationError);
		}
		generateValidationError(object, value, propertyName) {
			const validationError = new ValidationError_1.ValidationError();
			if (!this.validatorOptions || !this.validatorOptions.validationError || this.validatorOptions.validationError.target === void 0 || this.validatorOptions.validationError.target === true) validationError.target = object;
			if (!this.validatorOptions || !this.validatorOptions.validationError || this.validatorOptions.validationError.value === void 0 || this.validatorOptions.validationError.value === true) validationError.value = value;
			validationError.property = propertyName;
			validationError.children = [];
			validationError.constraints = {};
			return validationError;
		}
		conditionalValidations(object, value, metadatas) {
			return metadatas.map((metadata) => metadata.constraints[0](object, value)).reduce((resultA, resultB) => resultA && resultB, true);
		}
		customValidations(object, value, metadatas, error) {
			metadatas.forEach((metadata) => {
				this.metadataStorage.getTargetValidatorConstraints(metadata.constraintCls).forEach((customConstraintMetadata) => {
					if (customConstraintMetadata.async && this.ignoreAsyncValidations) return;
					if (this.validatorOptions && this.validatorOptions.stopAtFirstError && Object.keys(error.constraints || {}).length > 0) return;
					const validationArguments = {
						targetName: object.constructor ? object.constructor.name : void 0,
						property: metadata.propertyName,
						object,
						value,
						constraints: metadata.constraints
					};
					if (!metadata.each || !(Array.isArray(value) || value instanceof Set || value instanceof Map)) {
						const validatedValue = customConstraintMetadata.instance.validate(value, validationArguments);
						if ((0, utils_1.isPromise)(validatedValue)) {
							const promise = validatedValue.then((isValid) => {
								if (!isValid) {
									const [type, message] = this.createValidationError(object, value, metadata, customConstraintMetadata);
									error.constraints[type] = message;
									if (metadata.context) {
										if (!error.contexts) error.contexts = {};
										error.contexts[type] = Object.assign(error.contexts[type] || {}, metadata.context);
									}
								}
							});
							this.awaitingPromises.push(promise);
						} else if (!validatedValue) {
							const [type, message] = this.createValidationError(object, value, metadata, customConstraintMetadata);
							error.constraints[type] = message;
						}
						return;
					}
					const validatedSubValues = (0, utils_1.convertToArray)(value).map((subValue) => customConstraintMetadata.instance.validate(subValue, validationArguments));
					if (validatedSubValues.some((validatedSubValue) => (0, utils_1.isPromise)(validatedSubValue))) {
						const asyncValidatedSubValues = validatedSubValues.map((validatedSubValue) => (0, utils_1.isPromise)(validatedSubValue) ? validatedSubValue : Promise.resolve(validatedSubValue));
						const asyncValidationIsFinishedPromise = Promise.all(asyncValidatedSubValues).then((flatValidatedValues) => {
							if (!flatValidatedValues.every((isValid) => isValid)) {
								const [type, message] = this.createValidationError(object, value, metadata, customConstraintMetadata);
								error.constraints[type] = message;
								if (metadata.context) {
									if (!error.contexts) error.contexts = {};
									error.contexts[type] = Object.assign(error.contexts[type] || {}, metadata.context);
								}
							}
						});
						this.awaitingPromises.push(asyncValidationIsFinishedPromise);
						return;
					}
					if (!validatedSubValues.every((isValid) => isValid)) {
						const [type, message] = this.createValidationError(object, value, metadata, customConstraintMetadata);
						error.constraints[type] = message;
					}
				});
			});
		}
		nestedValidations(value, metadatas, error) {
			if (value === void 0) return;
			metadatas.forEach((metadata) => {
				if (metadata.type !== ValidationTypes_1.ValidationTypes.NESTED_VALIDATION && metadata.type !== ValidationTypes_1.ValidationTypes.PROMISE_VALIDATION) return;
				else if (this.validatorOptions && this.validatorOptions.stopAtFirstError && Object.keys(error.constraints || {}).length > 0) return;
				if (Array.isArray(value) || value instanceof Set || value instanceof Map) (value instanceof Set ? Array.from(value) : value).forEach((subValue, index) => {
					this.performValidations(value, subValue, index.toString(), [], metadatas, error.children);
				});
				else if (value instanceof Object) {
					const targetSchema = typeof metadata.target === "string" ? metadata.target : metadata.target.name;
					this.execute(value, targetSchema, error.children);
				} else {
					const [type, message] = this.createValidationError(metadata.target, value, metadata);
					error.constraints[type] = message;
				}
			});
		}
		mapContexts(object, value, metadatas, error) {
			return metadatas.forEach((metadata) => {
				if (metadata.context) {
					let customConstraint;
					if (metadata.type === ValidationTypes_1.ValidationTypes.CUSTOM_VALIDATION) customConstraint = this.metadataStorage.getTargetValidatorConstraints(metadata.constraintCls)[0];
					const type = this.getConstraintType(metadata, customConstraint);
					if (error.constraints[type]) {
						if (!error.contexts) error.contexts = {};
						error.contexts[type] = Object.assign(error.contexts[type] || {}, metadata.context);
					}
				}
			});
		}
		createValidationError(object, value, metadata, customValidatorMetadata) {
			const targetName = object.constructor ? object.constructor.name : void 0;
			const type = this.getConstraintType(metadata, customValidatorMetadata);
			const validationArguments = {
				targetName,
				property: metadata.propertyName,
				object,
				value,
				constraints: metadata.constraints
			};
			let message = metadata.message || "";
			if (!metadata.message && (!this.validatorOptions || this.validatorOptions && !this.validatorOptions.dismissDefaultMessages)) {
				if (customValidatorMetadata && customValidatorMetadata.instance.defaultMessage instanceof Function) message = customValidatorMetadata.instance.defaultMessage(validationArguments);
			}
			return [type, ValidationUtils_1.ValidationUtils.replaceMessageSpecialTokens(message, validationArguments)];
		}
		getConstraintType(metadata, customValidatorMetadata) {
			return customValidatorMetadata && customValidatorMetadata.name ? customValidatorMetadata.name : metadata.type;
		}
	};
	exports.ValidationExecutor = ValidationExecutor;
}) });

//#endregion
Object.defineProperty(exports, 'default', {
  enumerable: true,
  get: function () {
    return require_ValidationExecutor();
  }
});