

const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_CredoError = require('../../../../error/CredoError.js');
require('../../../../error/index.js');
const require_decorateMetadata = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateMetadata.js');
const require_decorate = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.js');
const require_constants = require('../../constants.js');
const require_validators = require('../../validators.js');
require('./W3cV2Credential.js');
const require_W3cV2JwtVerifiableCredential = require('../../jwt-vc/W3cV2JwtVerifiableCredential.js');
const require_W3cV2SdJwtVerifiableCredential = require('../../sd-jwt-vc/W3cV2SdJwtVerifiableCredential.js');
let class_transformer = require("class-transformer");
class_transformer = require_rolldown_runtime.__toESM(class_transformer);
let class_validator = require("class-validator");
class_validator = require_rolldown_runtime.__toESM(class_validator);

//#region src/modules/vc/models/credential/W3cV2EnvelopedVerifiableCredential.ts
var W3cV2EnvelopedVerifiableCredential = class W3cV2EnvelopedVerifiableCredential {
	constructor(options) {
		if (options) {
			this.context = options.context ?? require_constants.CREDENTIALS_CONTEXT_V2_URL;
			this.id = options.id;
			this.type = options.type ?? require_constants.ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE;
			this._envelopedCredential = credentialFromDataUri(options.id);
		}
	}
	static fromVerifiableCredential(credential) {
		return new W3cV2EnvelopedVerifiableCredential({
			id: credentialToDataUri(credential),
			context: require_constants.CREDENTIALS_CONTEXT_V2_URL,
			type: require_constants.ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE
		});
	}
	/**
	* Gets the enveloped credential.
	*/
	get envelopedCredential() {
		if (!this._envelopedCredential) this._envelopedCredential = credentialFromDataUri(this.id);
		return this._envelopedCredential;
	}
	/**
	* Resolved credential is the fully resolved {@link W3cV2Credential} instance.
	*/
	get resolvedCredential() {
		return this.envelopedCredential.resolvedCredential;
	}
	/**
	* The {@link ClaimFormat} of the enveloped credential.
	*/
	get claimFormat() {
		return this.envelopedCredential.claimFormat;
	}
};
require_decorate.__decorate([(0, class_transformer.Exclude)(), require_decorateMetadata.__decorateMetadata("design:type", Object)], W3cV2EnvelopedVerifiableCredential.prototype, "_envelopedCredential", void 0);
require_decorate.__decorate([
	(0, class_transformer.Expose)({ name: "@context" }),
	require_validators.IsCredentialJsonLdContext({
		allowString: true,
		credentialContext: require_constants.CREDENTIALS_CONTEXT_V2_URL
	}),
	require_decorateMetadata.__decorateMetadata("design:type", Object)
], W3cV2EnvelopedVerifiableCredential.prototype, "context", void 0);
require_decorate.__decorate([(0, class_validator.IsDataURI)(), require_decorateMetadata.__decorateMetadata("design:type", String)], W3cV2EnvelopedVerifiableCredential.prototype, "id", void 0);
require_decorate.__decorate([IsEnvelopedVerifiableCredentialType(), require_decorateMetadata.__decorateMetadata("design:type", Object)], W3cV2EnvelopedVerifiableCredential.prototype, "type", void 0);
const jsonToClass = (v) => {
	if (!v || typeof v !== "object") throw new require_CredoError.CredoError("Invalid plain W3cV2EnvelopedVerifiableCredential");
	if ((0, class_validator.isInstance)(v, W3cV2EnvelopedVerifiableCredential)) return v;
	return (0, class_transformer.plainToInstance)(W3cV2EnvelopedVerifiableCredential, v);
};
const classToJson = (v) => {
	return (0, class_transformer.instanceToPlain)(v);
};
function W3cV2EnvelopedVerifiableCredentialTransformer() {
	return (0, class_transformer.Transform)(({ value, type }) => {
		if (type === class_transformer.TransformationType.PLAIN_TO_CLASS) return Array.isArray(value) ? value.map(jsonToClass) : jsonToClass(value);
		if (type === class_transformer.TransformationType.CLASS_TO_PLAIN) {
			if (Array.isArray(value)) return value.map(classToJson);
			return classToJson(value);
		}
		return value;
	});
}
function IsEnvelopedVerifiableCredentialType(validationOptions) {
	return (0, class_validator.ValidateBy)({
		name: "IsEnvelopedVerifiableCredentialType",
		validator: {
			validate: (value) => {
				return Array.isArray(value) ? value.includes(require_constants.ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE) && value.every((v) => typeof v === "string") : value === require_constants.ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE;
			},
			defaultMessage: (0, class_validator.buildMessage)((eachPrefix) => `${eachPrefix}$property must be a string that equals, or an array of strings which includes "${require_constants.ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE}"`, validationOptions)
		}
	}, validationOptions);
}
function credentialFromDataUri(uri) {
	if (!uri.startsWith("data:")) throw new require_CredoError.CredoError("Invalid Enveloped Verifiable Credential: \"id\" is not a valid data URI");
	const mimetypeData = uri.slice(5);
	const commaIndex = mimetypeData.indexOf(",");
	if (commaIndex === -1) throw new require_CredoError.CredoError("Invalid Enveloped Verifiable Credential: \"id\" data URI is missing comma separator");
	const mimetype = mimetypeData.slice(0, commaIndex);
	const data = mimetypeData.slice(commaIndex + 1);
	switch (mimetype) {
		case "application/vc+sd-jwt": return require_W3cV2SdJwtVerifiableCredential.W3cV2SdJwtVerifiableCredential.fromCompact(data);
		case "application/vc+jwt": return require_W3cV2JwtVerifiableCredential.W3cV2JwtVerifiableCredential.fromCompact(data);
		default: throw new require_CredoError.CredoError(`Unsupported Enveloped Verifiable Credential: ${mimetype} not recognized`);
	}
}
function credentialToDataUri(presentation) {
	if (presentation instanceof require_W3cV2SdJwtVerifiableCredential.W3cV2SdJwtVerifiableCredential) return `data:application/vc+sd-jwt,${presentation.encoded}`;
	if (presentation instanceof require_W3cV2JwtVerifiableCredential.W3cV2JwtVerifiableCredential) return `data:application/vc+jwt,${presentation.encoded}`;
	throw new require_CredoError.CredoError("Unsupported Verifiable Credential instance");
}

//#endregion
exports.IsEnvelopedVerifiableCredentialType = IsEnvelopedVerifiableCredentialType;
exports.W3cV2EnvelopedVerifiableCredential = W3cV2EnvelopedVerifiableCredential;
exports.W3cV2EnvelopedVerifiableCredentialTransformer = W3cV2EnvelopedVerifiableCredentialTransformer;