

const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_MessageValidator = require('../../../utils/MessageValidator.js');
const require_JsonTransformer = require('../../../utils/JsonTransformer.js');
require('../../../utils/index.js');
const require_Jwt = require('../../../crypto/jose/jwt/Jwt.js');
const require_ClaimFormat = require('../models/ClaimFormat.js');
const require_W3cV2Credential = require('../models/credential/W3cV2Credential.js');

//#region src/modules/vc/jwt-vc/W3cV2JwtVerifiableCredential.ts
/**
* Represents a Verifiable Credential encoded as a JWT.
*
* @see https://www.w3.org/TR/vc-jose-cose/#securing-with-jose
*/
var W3cV2JwtVerifiableCredential = class W3cV2JwtVerifiableCredential {
	constructor(options) {
		this.jwt = options.jwt;
		this.resolvedCredential = require_JsonTransformer.JsonTransformer.fromJSON(options.jwt.payload.additionalClaims, require_W3cV2Credential.W3cV2Credential, { validate: false });
		this.validate();
	}
	static fromCompact(compact) {
		return new W3cV2JwtVerifiableCredential({ jwt: require_Jwt.Jwt.fromSerializedJwt(compact) });
	}
	/**
	* The encoded version of this credential.
	*/
	get encoded() {
		return this.jwt.serializedJwt;
	}
	/**
	* The {@link ClaimFormat} of the credential.
	*
	* For W3C VC JWT credentials this is always `vc+jwt`.
	*/
	get claimFormat() {
		return require_ClaimFormat.ClaimFormat.JwtW3cVc;
	}
	/**
	* Validates the JWT and the resolved credential contained.
	*/
	validate() {
		require_MessageValidator.MessageValidator.validateSync(this.resolvedCredential);
		const jwt = this.jwt;
		const header = jwt.header;
		const payload = jwt.payload;
		if ("typ" in header && header.typ !== "vc+jwt") throw new require_CredoError.CredoError(`The provided W3C VC JWT does not have the correct 'typ' header.`);
		if ("cyt" in header && header.cyt !== "vc") throw new require_CredoError.CredoError(`The provided W3C VC JWT does not have the correct 'cyt' header.`);
		const iss = header.iss ?? payload.iss;
		if (iss) {
			if (this.resolvedCredential.issuerId !== iss) throw new require_CredoError.CredoError(`The provided W3C VC JWT has both 'iss' and 'issuer' claims, but they differ.`);
		}
		if (payload.jti) {
			if (this.resolvedCredential.id && this.resolvedCredential.id !== payload.jti) throw new require_CredoError.CredoError(`The provided W3C VC JWT has both 'jti' and 'id' claims, but they differ.`);
		}
		if (payload.sub) {
			if (!this.resolvedCredential.credentialSubjectIds.includes(payload.sub)) throw new require_CredoError.CredoError(`The provided W3C VC JWT has a 'sub' claim, but it does not match any credentialSubject.`);
		}
	}
};

//#endregion
exports.W3cV2JwtVerifiableCredential = W3cV2JwtVerifiableCredential;