

import { CredoError } from "../../../error/CredoError.mjs";
import "../../../error/index.mjs";
import { JsonTransformer } from "../../../utils/JsonTransformer.mjs";
import "../../../utils/index.mjs";
import { isJsonObject } from "../../../types.mjs";
import { JwtPayload } from "../../../crypto/jose/jwt/JwtPayload.mjs";
import "../../../crypto/jose/jwt/index.mjs";
import { W3cPresentation } from "../models/presentation/W3cPresentation.mjs";

//#region src/modules/vc/jwt-vc/presentationTransformer.ts
function getJwtPayloadFromPresentation(presentation) {
	const vp = 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 JwtPayload(payloadOptions);
}
function getPresentationFromJwtPayload(jwtPayload) {
	if (!("vp" in jwtPayload.additionalClaims) || !isJsonObject(jwtPayload.additionalClaims.vp)) throw new CredoError("JWT does not contain a valid 'vp' claim");
	const jwtVp = jwtPayload.additionalClaims.vp;
	if (jwtVp.id && jwtPayload.jti !== jwtVp.id) throw new CredoError("JWT jti and vp.id do not match");
	if (typeof jwtVp.holder === "string" && jwtPayload.iss !== jwtVp.holder || isJsonObject(jwtVp.holder) && jwtVp.holder.id && jwtPayload.iss !== jwtVp.holder.id) throw new CredoError("JWT iss and vp.holder(.id) do not match");
	const dataModelVp = {
		...jwtVp,
		id: jwtPayload.jti,
		holder: jwtPayload.iss
	};
	return JsonTransformer.fromJSON(dataModelVp, W3cPresentation);
}

//#endregion
export { getJwtPayloadFromPresentation, getPresentationFromJwtPayload };
//# sourceMappingURL=presentationTransformer.mjs.map