const require_rolldown_runtime = require('../../../../../../_virtual/rolldown_runtime.js');
const require_index = require('./enums/index.js');
const require_storage$1 = require('./storage.js');
const require_index$1 = require('./utils/index.js');

//#region ../../node_modules/.pnpm/class-transformer@0.5.1/node_modules/class-transformer/cjs/TransformOperationExecutor.js
var require_TransformOperationExecutor = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/.pnpm/class-transformer@0.5.1/node_modules/class-transformer/cjs/TransformOperationExecutor.js": ((exports) => {
	Object.defineProperty(exports, "__esModule", { value: true });
	const storage_1 = require_storage$1.require_storage();
	const enums_1 = require_index.require_enums();
	const utils_1 = require_index$1.require_utils();
	function instantiateArrayType(arrayType) {
		const array = new arrayType();
		if (!(array instanceof Set) && !("push" in array)) return [];
		return array;
	}
	var TransformOperationExecutor = class {
		constructor(transformationType, options) {
			this.transformationType = transformationType;
			this.options = options;
			this.recursionStack = /* @__PURE__ */ new Set();
		}
		transform(source, value, targetType, arrayType, isMap, level = 0) {
			if (Array.isArray(value) || value instanceof Set) {
				const newValue = arrayType && this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS ? instantiateArrayType(arrayType) : [];
				value.forEach((subValue, index) => {
					const subSource = source ? source[index] : void 0;
					if (!this.options.enableCircularCheck || !this.isCircular(subValue)) {
						let realTargetType;
						if (typeof targetType !== "function" && targetType && targetType.options && targetType.options.discriminator && targetType.options.discriminator.property && targetType.options.discriminator.subTypes) {
							if (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS) {
								realTargetType = targetType.options.discriminator.subTypes.find((subType) => subType.name === subValue[targetType.options.discriminator.property]);
								const options = {
									newObject: newValue,
									object: subValue,
									property: void 0
								};
								const newType = targetType.typeFunction(options);
								realTargetType === void 0 ? realTargetType = newType : realTargetType = realTargetType.value;
								if (!targetType.options.keepDiscriminatorProperty) delete subValue[targetType.options.discriminator.property];
							}
							if (this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS) realTargetType = subValue.constructor;
							if (this.transformationType === enums_1.TransformationType.CLASS_TO_PLAIN) subValue[targetType.options.discriminator.property] = targetType.options.discriminator.subTypes.find((subType) => subType.value === subValue.constructor).name;
						} else realTargetType = targetType;
						const value$1 = this.transform(subSource, subValue, realTargetType, void 0, subValue instanceof Map, level + 1);
						if (newValue instanceof Set) newValue.add(value$1);
						else newValue.push(value$1);
					} else if (this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS) if (newValue instanceof Set) newValue.add(subValue);
					else newValue.push(subValue);
				});
				return newValue;
			} else if (targetType === String && !isMap) {
				if (value === null || value === void 0) return value;
				return String(value);
			} else if (targetType === Number && !isMap) {
				if (value === null || value === void 0) return value;
				return Number(value);
			} else if (targetType === Boolean && !isMap) {
				if (value === null || value === void 0) return value;
				return Boolean(value);
			} else if ((targetType === Date || value instanceof Date) && !isMap) {
				if (value instanceof Date) return new Date(value.valueOf());
				if (value === null || value === void 0) return value;
				return new Date(value);
			} else if (!!(0, utils_1.getGlobal)().Buffer && (targetType === Buffer || value instanceof Buffer) && !isMap) {
				if (value === null || value === void 0) return value;
				return Buffer.from(value);
			} else if ((0, utils_1.isPromise)(value) && !isMap) return new Promise((resolve, reject) => {
				value.then((data) => resolve(this.transform(void 0, data, targetType, void 0, void 0, level + 1)), reject);
			});
			else if (!isMap && value !== null && typeof value === "object" && typeof value.then === "function") return value;
			else if (typeof value === "object" && value !== null) {
				if (!targetType && value.constructor !== Object) if (!Array.isArray(value) && value.constructor === Array) {} else targetType = value.constructor;
				if (!targetType && source) targetType = source.constructor;
				if (this.options.enableCircularCheck) this.recursionStack.add(value);
				const keys = this.getKeys(targetType, value, isMap);
				let newValue = source ? source : {};
				if (!source && (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS || this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS)) if (isMap) newValue = /* @__PURE__ */ new Map();
				else if (targetType) newValue = new targetType();
				else newValue = {};
				for (const key of keys) {
					if (key === "__proto__" || key === "constructor") continue;
					const valueKey = key;
					let newValueKey = key, propertyName = key;
					if (!this.options.ignoreDecorators && targetType) {
						if (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS) {
							const exposeMetadata = storage_1.defaultMetadataStorage.findExposeMetadataByCustomName(targetType, key);
							if (exposeMetadata) {
								propertyName = exposeMetadata.propertyName;
								newValueKey = exposeMetadata.propertyName;
							}
						} else if (this.transformationType === enums_1.TransformationType.CLASS_TO_PLAIN || this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS) {
							const exposeMetadata = storage_1.defaultMetadataStorage.findExposeMetadata(targetType, key);
							if (exposeMetadata && exposeMetadata.options && exposeMetadata.options.name) newValueKey = exposeMetadata.options.name;
						}
					}
					let subValue = void 0;
					if (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS)
 /**
					* This section is added for the following report:
					* https://github.com/typestack/class-transformer/issues/596
					*
					* We should not call functions or constructors when transforming to class.
					*/
					subValue = value[valueKey];
					else if (value instanceof Map) subValue = value.get(valueKey);
					else if (value[valueKey] instanceof Function) subValue = value[valueKey]();
					else subValue = value[valueKey];
					let type = void 0, isSubValueMap = subValue instanceof Map;
					if (targetType && isMap) type = targetType;
					else if (targetType) {
						const metadata = storage_1.defaultMetadataStorage.findTypeMetadata(targetType, propertyName);
						if (metadata) {
							const options = {
								newObject: newValue,
								object: value,
								property: propertyName
							};
							const newType = metadata.typeFunction ? metadata.typeFunction(options) : metadata.reflectedType;
							if (metadata.options && metadata.options.discriminator && metadata.options.discriminator.property && metadata.options.discriminator.subTypes) if (!(value[valueKey] instanceof Array)) {
								if (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS) {
									type = metadata.options.discriminator.subTypes.find((subType) => {
										if (subValue && subValue instanceof Object && metadata.options.discriminator.property in subValue) return subType.name === subValue[metadata.options.discriminator.property];
									});
									type === void 0 ? type = newType : type = type.value;
									if (!metadata.options.keepDiscriminatorProperty) {
										if (subValue && subValue instanceof Object && metadata.options.discriminator.property in subValue) delete subValue[metadata.options.discriminator.property];
									}
								}
								if (this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS) type = subValue.constructor;
								if (this.transformationType === enums_1.TransformationType.CLASS_TO_PLAIN) {
									if (subValue) subValue[metadata.options.discriminator.property] = metadata.options.discriminator.subTypes.find((subType) => subType.value === subValue.constructor).name;
								}
							} else type = metadata;
							else type = newType;
							isSubValueMap = isSubValueMap || metadata.reflectedType === Map;
						} else if (this.options.targetMaps) this.options.targetMaps.filter((map) => map.target === targetType && !!map.properties[propertyName]).forEach((map) => type = map.properties[propertyName]);
						else if (this.options.enableImplicitConversion && this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS) {
							const reflectedType = Reflect.getMetadata("design:type", targetType.prototype, propertyName);
							if (reflectedType) type = reflectedType;
						}
					}
					const arrayType$1 = Array.isArray(value[valueKey]) ? this.getReflectedType(targetType, propertyName) : void 0;
					const subSource = source ? source[valueKey] : void 0;
					if (newValue.constructor.prototype) {
						const descriptor = Object.getOwnPropertyDescriptor(newValue.constructor.prototype, newValueKey);
						if ((this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS || this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS) && (descriptor && !descriptor.set || newValue[newValueKey] instanceof Function)) continue;
					}
					if (!this.options.enableCircularCheck || !this.isCircular(subValue)) {
						const transformKey = this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS ? newValueKey : key;
						let finalValue;
						if (this.transformationType === enums_1.TransformationType.CLASS_TO_PLAIN) {
							finalValue = value[transformKey];
							finalValue = this.applyCustomTransformations(finalValue, targetType, transformKey, value, this.transformationType);
							finalValue = value[transformKey] === finalValue ? subValue : finalValue;
							finalValue = this.transform(subSource, finalValue, type, arrayType$1, isSubValueMap, level + 1);
						} else if (subValue === void 0 && this.options.exposeDefaultValues) finalValue = newValue[newValueKey];
						else {
							finalValue = this.transform(subSource, subValue, type, arrayType$1, isSubValueMap, level + 1);
							finalValue = this.applyCustomTransformations(finalValue, targetType, transformKey, value, this.transformationType);
						}
						if (finalValue !== void 0 || this.options.exposeUnsetFields) if (newValue instanceof Map) newValue.set(newValueKey, finalValue);
						else newValue[newValueKey] = finalValue;
					} else if (this.transformationType === enums_1.TransformationType.CLASS_TO_CLASS) {
						let finalValue = subValue;
						finalValue = this.applyCustomTransformations(finalValue, targetType, key, value, this.transformationType);
						if (finalValue !== void 0 || this.options.exposeUnsetFields) if (newValue instanceof Map) newValue.set(newValueKey, finalValue);
						else newValue[newValueKey] = finalValue;
					}
				}
				if (this.options.enableCircularCheck) this.recursionStack.delete(value);
				return newValue;
			} else return value;
		}
		applyCustomTransformations(value, target, key, obj, transformationType) {
			let metadatas = storage_1.defaultMetadataStorage.findTransformMetadatas(target, key, this.transformationType);
			if (this.options.version !== void 0) metadatas = metadatas.filter((metadata) => {
				if (!metadata.options) return true;
				return this.checkVersion(metadata.options.since, metadata.options.until);
			});
			if (this.options.groups && this.options.groups.length) metadatas = metadatas.filter((metadata) => {
				if (!metadata.options) return true;
				return this.checkGroups(metadata.options.groups);
			});
			else metadatas = metadatas.filter((metadata) => {
				return !metadata.options || !metadata.options.groups || !metadata.options.groups.length;
			});
			metadatas.forEach((metadata) => {
				value = metadata.transformFn({
					value,
					key,
					obj,
					type: transformationType,
					options: this.options
				});
			});
			return value;
		}
		isCircular(object) {
			return this.recursionStack.has(object);
		}
		getReflectedType(target, propertyName) {
			if (!target) return void 0;
			const meta = storage_1.defaultMetadataStorage.findTypeMetadata(target, propertyName);
			return meta ? meta.reflectedType : void 0;
		}
		getKeys(target, object, isMap) {
			let strategy = storage_1.defaultMetadataStorage.getStrategy(target);
			if (strategy === "none") strategy = this.options.strategy || "exposeAll";
			let keys = [];
			if (strategy === "exposeAll" || isMap) if (object instanceof Map) keys = Array.from(object.keys());
			else keys = Object.keys(object);
			if (isMap) return keys;
			/**
			* If decorators are ignored but we don't want the extraneous values, then we use the
			* metadata to decide which property is needed, but doesn't apply the decorator effect.
			*/
			if (this.options.ignoreDecorators && this.options.excludeExtraneousValues && target) {
				const exposedProperties = storage_1.defaultMetadataStorage.getExposedProperties(target, this.transformationType);
				const excludedProperties = storage_1.defaultMetadataStorage.getExcludedProperties(target, this.transformationType);
				keys = [...exposedProperties, ...excludedProperties];
			}
			if (!this.options.ignoreDecorators && target) {
				let exposedProperties = storage_1.defaultMetadataStorage.getExposedProperties(target, this.transformationType);
				if (this.transformationType === enums_1.TransformationType.PLAIN_TO_CLASS) exposedProperties = exposedProperties.map((key) => {
					const exposeMetadata = storage_1.defaultMetadataStorage.findExposeMetadata(target, key);
					if (exposeMetadata && exposeMetadata.options && exposeMetadata.options.name) return exposeMetadata.options.name;
					return key;
				});
				if (this.options.excludeExtraneousValues) keys = exposedProperties;
				else keys = keys.concat(exposedProperties);
				const excludedProperties = storage_1.defaultMetadataStorage.getExcludedProperties(target, this.transformationType);
				if (excludedProperties.length > 0) keys = keys.filter((key) => {
					return !excludedProperties.includes(key);
				});
				if (this.options.version !== void 0) keys = keys.filter((key) => {
					const exposeMetadata = storage_1.defaultMetadataStorage.findExposeMetadata(target, key);
					if (!exposeMetadata || !exposeMetadata.options) return true;
					return this.checkVersion(exposeMetadata.options.since, exposeMetadata.options.until);
				});
				if (this.options.groups && this.options.groups.length) keys = keys.filter((key) => {
					const exposeMetadata = storage_1.defaultMetadataStorage.findExposeMetadata(target, key);
					if (!exposeMetadata || !exposeMetadata.options) return true;
					return this.checkGroups(exposeMetadata.options.groups);
				});
				else keys = keys.filter((key) => {
					const exposeMetadata = storage_1.defaultMetadataStorage.findExposeMetadata(target, key);
					return !exposeMetadata || !exposeMetadata.options || !exposeMetadata.options.groups || !exposeMetadata.options.groups.length;
				});
			}
			if (this.options.excludePrefixes && this.options.excludePrefixes.length) keys = keys.filter((key) => this.options.excludePrefixes.every((prefix) => {
				return key.substr(0, prefix.length) !== prefix;
			}));
			keys = keys.filter((key, index, self) => {
				return self.indexOf(key) === index;
			});
			return keys;
		}
		checkVersion(since, until) {
			let decision = true;
			if (decision && since) decision = this.options.version >= since;
			if (decision && until) decision = this.options.version < until;
			return decision;
		}
		checkGroups(groups) {
			if (!groups) return true;
			return this.options.groups.some((optionGroup) => groups.includes(optionGroup));
		}
	};
	exports.TransformOperationExecutor = TransformOperationExecutor;
}) });

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