

const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_JsonTransformer = require('../../../utils/JsonTransformer.js');
require('../../../utils/index.js');
const require_types = require('../../../types.js');
const require_JwtPayload = require('../../../crypto/jose/jwt/JwtPayload.js');
require('../../../crypto/jose/jwt/index.js');
const require_W3cPresentation = require('../models/presentation/W3cPresentation.js');

//#region src/modules/vc/jwt-vc/presentationTransformer.ts
function getJwtPayloadFromPresentation(presentation) {
	const vp = require_JsonTransformer.JsonTransformer.toJSON(presentation);
	const payloadOptions = { additionalClaims: { vp } };
	if (presentation.holderId) {
		payloadOptions.iss = presentation.holderId;
		if (typeof vp.holder === "string") delete vp.holder;
		else if (typeof vp.holder === "object") {
			delete vp.holder.id;
			if (Object.keys(vp.holder).length === 0) delete vp.holder;
		}
	}
	if (presentation.id) {
		payloadOptions.jti = presentation.id;
		delete vp.id;
	}
	return new require_JwtPayload.JwtPayload(payloadOptions);
}
function getPresentationFromJwtPayload(jwtPayload) {
	if (!("vp" in jwtPayload.additionalClaims) || !require_types.isJsonObject(jwtPayload.additionalClaims.vp)) throw new require_CredoError.CredoError("JWT does not contain a valid 'vp' claim");
	const jwtVp = jwtPayload.additionalClaims.vp;
	if (jwtVp.id && jwtPayload.jti !== jwtVp.id) throw new require_CredoError.CredoError("JWT jti and vp.id do not match");
	if (typeof jwtVp.holder === "string" && jwtPayload.iss !== jwtVp.holder || require_types.isJsonObject(jwtVp.holder) && jwtVp.holder.id && jwtPayload.iss !== jwtVp.holder.id) throw new require_CredoError.CredoError("JWT iss and vp.holder(.id) do not match");
	const dataModelVp = {
		...jwtVp,
		id: jwtPayload.jti,
		holder: jwtPayload.iss
	};
	return require_JsonTransformer.JsonTransformer.fromJSON(dataModelVp, require_W3cPresentation.W3cPresentation);
}

//#endregion
exports.getJwtPayloadFromPresentation = getJwtPayloadFromPresentation;
exports.getPresentationFromJwtPayload = getPresentationFromJwtPayload;