

import { CredoError } from "../../../error/CredoError.mjs";
import "../../../error/index.mjs";
import { ClaimFormat } from "../../vc/models/ClaimFormat.mjs";
import { W3cJsonLdVerifiablePresentation } from "../../vc/data-integrity/models/W3cJsonLdVerifiablePresentation.mjs";
import { W3cJwtVerifiablePresentation } from "../../vc/jwt-vc/W3cJwtVerifiablePresentation.mjs";
import { W3cV2SdJwtVerifiablePresentation } from "../../vc/sd-jwt-vc/W3cV2SdJwtVerifiablePresentation.mjs";
import "../../vc/sd-jwt-vc/index.mjs";
import { W3cV2JwtVerifiablePresentation } from "../../vc/jwt-vc/W3cV2JwtVerifiablePresentation.mjs";
import "../../vc/index.mjs";
import { MdocDeviceResponse } from "../../mdoc/MdocDeviceResponse.mjs";
import "../../mdoc/index.mjs";
import { JSONPath } from "@astronautlabs/jsonpath";

//#region src/modules/dif-presentation-exchange/utils/presentationSelection.ts
function extractPresentationsWithDescriptorsFromSubmission(presentations, submission, definition) {
	return submission.descriptor_map.map((descriptor) => {
		const [presentation] = JSONPath.query(presentations, descriptor.path);
		const inputDescriptor = definition.input_descriptors.find(({ id }) => id === descriptor.id);
		if (!presentation) throw new 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 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: ClaimFormat.MsoMdoc,
				descriptor,
				presentation,
				credential: document,
				inputDescriptor
			};
		}
		if (presentation instanceof W3cJwtVerifiablePresentation || presentation instanceof 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] = JSONPath.query(presentation.claimFormat === ClaimFormat.JwtVp ? { vp: presentation } : presentation, descriptor.path_nested.path);
			if (!verifiableCredential) throw new 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 W3cV2JwtVerifiablePresentation || presentation instanceof W3cV2SdJwtVerifiablePresentation) throw new CredoError(`Presentation format '${presentation.claimFormat}' is not supported with DIF Presentation Exchange`);
		return {
			claimFormat: ClaimFormat.SdJwtDc,
			descriptor,
			presentation,
			credential: presentation,
			inputDescriptor
		};
	});
}

//#endregion
export { extractPresentationsWithDescriptorsFromSubmission };
//# sourceMappingURL=presentationSelection.mjs.map