

const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.js');
const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_ClaimFormat = require('../../vc/models/ClaimFormat.js');
const require_W3cJsonLdVerifiablePresentation = require('../../vc/data-integrity/models/W3cJsonLdVerifiablePresentation.js');
const require_W3cJwtVerifiablePresentation = require('../../vc/jwt-vc/W3cJwtVerifiablePresentation.js');
const require_W3cV2JwtVerifiablePresentation = require('../../vc/jwt-vc/W3cV2JwtVerifiablePresentation.js');
const require_W3cV2SdJwtVerifiablePresentation = require('../../vc/sd-jwt-vc/W3cV2SdJwtVerifiablePresentation.js');
require('../../vc/sd-jwt-vc/index.js');
require('../../vc/index.js');
const require_MdocDeviceResponse = require('../../mdoc/MdocDeviceResponse.js');
require('../../mdoc/index.js');
let __astronautlabs_jsonpath = require("@astronautlabs/jsonpath");
__astronautlabs_jsonpath = require_rolldown_runtime.__toESM(__astronautlabs_jsonpath);

//#region src/modules/dif-presentation-exchange/utils/presentationSelection.ts
function extractPresentationsWithDescriptorsFromSubmission(presentations, submission, definition) {
	return submission.descriptor_map.map((descriptor) => {
		const [presentation] = __astronautlabs_jsonpath.JSONPath.query(presentations, descriptor.path);
		const inputDescriptor = definition.input_descriptors.find(({ id }) => id === descriptor.id);
		if (!presentation) throw new require_CredoError.CredoError(`Unable to extract presentation at path '${descriptor.path}' for submission descriptor '${descriptor.id}'`);
		if (!inputDescriptor) throw new Error(`Unable to extract input descriptor '${descriptor.id}' from definition '${definition.id}' for submission '${submission.id}'`);
		if (presentation instanceof require_MdocDeviceResponse.MdocDeviceResponse) {
			const document = presentation.documents.find((document$1) => document$1.docType === descriptor.id);
			if (!document) throw new Error(`Unable to extract mdoc document with doctype '${descriptor.id}' from mdoc device response for submission '${submission.id}'.`);
			return {
				claimFormat: require_ClaimFormat.ClaimFormat.MsoMdoc,
				descriptor,
				presentation,
				credential: document,
				inputDescriptor
			};
		}
		if (presentation instanceof require_W3cJwtVerifiablePresentation.W3cJwtVerifiablePresentation || presentation instanceof require_W3cJsonLdVerifiablePresentation.W3cJsonLdVerifiablePresentation) {
			if (!descriptor.path_nested) throw new Error(`Submission descriptor '${descriptor.id}' for submission '${submission.id}' has no 'path_nested' but presentation is format '${presentation.claimFormat}'`);
			const [verifiableCredential] = __astronautlabs_jsonpath.JSONPath.query(presentation.claimFormat === require_ClaimFormat.ClaimFormat.JwtVp ? { vp: presentation } : presentation, descriptor.path_nested.path);
			if (!verifiableCredential) throw new require_CredoError.CredoError(`Unable to extract credential at path '${descriptor.path_nested.path}' from presentation at path '${descriptor.path}' for submission descriptor '${descriptor.id}'`);
			return {
				claimFormat: presentation.claimFormat,
				descriptor,
				presentation,
				credential: verifiableCredential,
				inputDescriptor
			};
		}
		if (presentation instanceof require_W3cV2JwtVerifiablePresentation.W3cV2JwtVerifiablePresentation || presentation instanceof require_W3cV2SdJwtVerifiablePresentation.W3cV2SdJwtVerifiablePresentation) throw new require_CredoError.CredoError(`Presentation format '${presentation.claimFormat}' is not supported with DIF Presentation Exchange`);
		return {
			claimFormat: require_ClaimFormat.ClaimFormat.SdJwtDc,
			descriptor,
			presentation,
			credential: presentation,
			inputDescriptor
		};
	});
}

//#endregion
exports.extractPresentationsWithDescriptorsFromSubmission = extractPresentationsWithDescriptorsFromSubmission;