

import { CredoError } from "../../../../error/CredoError.mjs";
import "../../../../error/index.mjs";
import { __decorateMetadata } from "../../../../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorateMetadata.mjs";
import { __decorate } from "../../../../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorate.mjs";
import { Transform, TransformationType } from "class-transformer";
import { IsOptional, ValidateBy, buildMessage, isInstance } from "class-validator";

//#region src/modules/vc/models/credential/W3cCredentialSubject.ts
var _ref;
var W3cCredentialSubject = class {
	constructor(options) {
		if (options) {
			this.id = options.id;
			const { id,...claims } = options.claims ?? {};
			this.claims = Object.keys(claims).length > 0 ? claims : void 0;
		}
	}
};
__decorate([IsOptional(), __decorateMetadata("design:type", String)], W3cCredentialSubject.prototype, "id", void 0);
__decorate([IsOptional(), __decorateMetadata("design:type", typeof (_ref = typeof Record !== "undefined" && Record) === "function" ? _ref : Object)], W3cCredentialSubject.prototype, "claims", void 0);
function W3cCredentialSubjectTransformer() {
	return Transform(({ value, type }) => {
		if (type === TransformationType.PLAIN_TO_CLASS) {
			const vToClass = (v) => {
				if (!v || typeof v !== "object") throw new CredoError("Invalid credential subject");
				if (isInstance(v, W3cCredentialSubject)) return v;
				const { id,...claims } = v;
				if (id !== void 0 && typeof id !== "string") throw new CredoError("Invalid credential subject id");
				return new W3cCredentialSubject({
					id,
					claims
				});
			};
			if (Array.isArray(value) && value.length === 0) throw new CredoError("At least one credential subject is required");
			return Array.isArray(value) ? value.map(vToClass) : vToClass(value);
		}
		if (type === TransformationType.CLASS_TO_PLAIN) {
			const vToJson = (v) => {
				if (v instanceof W3cCredentialSubject) return v.id ? {
					...v.claims,
					id: v.id
				} : { ...v.claims };
				return v;
			};
			return Array.isArray(value) ? value.map(vToJson) : vToJson(value);
		}
		return value;
	});
}
function IsW3cCredentialSubject(validationOptions) {
	return ValidateBy({
		name: "IsW3cCredentialSubject",
		validator: {
			validate: (value) => {
				return isInstance(value, W3cCredentialSubject);
			},
			defaultMessage: buildMessage((eachPrefix) => `${eachPrefix}$property must be an object or an array of objects with an optional id property`, validationOptions)
		}
	}, validationOptions);
}

//#endregion
export { IsW3cCredentialSubject, W3cCredentialSubject, W3cCredentialSubjectTransformer };
//# sourceMappingURL=W3cCredentialSubject.mjs.map