

const require_credentialUseTypes = require('../../../utils/credentialUseTypes.js');
const require_ClaimFormat = require('../../vc/models/ClaimFormat.js');
require('../../vc/index.js');
const require_credentialUse = require('../../../utils/credentialUse.js');
require('../../mdoc/index.js');
require('../../sd-jwt-vc/index.js');
const require_DcqlError = require('../DcqlError.js');

//#region src/modules/dcql/utils/DcqlPresentationsToCreate.ts
function dcqlGetPresentationsToCreate(credentialsForInputDescriptor) {
	const presentationsToCreate = {};
	for (const [credentialQueryId, matches] of Object.entries(credentialsForInputDescriptor)) for (const match of matches) {
		const matchIndex = matches.indexOf(match);
		let presentationToCreate;
		const useMode = match.useMode ?? require_credentialUseTypes.CredentialMultiInstanceUseMode.NewOrFirst;
		if (!require_credentialUse.canUseInstanceFromCredentialRecord({
			credentialRecord: match.credentialRecord,
			useMode
		})) throw new require_DcqlError.DcqlError(`Unable to prepare presentation for credential at index ${matchIndex} for query credential '${credentialQueryId}'. No new credential instance available on the credential record.`);
		switch (match.claimFormat) {
			case require_ClaimFormat.ClaimFormat.SdJwtDc:
				presentationToCreate = {
					claimFormat: require_ClaimFormat.ClaimFormat.SdJwtDc,
					credentialRecord: match.credentialRecord,
					disclosedPayload: match.disclosedPayload,
					additionalPayload: match.additionalPayload,
					useMode
				};
				break;
			case require_ClaimFormat.ClaimFormat.MsoMdoc:
				presentationToCreate = {
					claimFormat: require_ClaimFormat.ClaimFormat.MsoMdoc,
					credentialRecord: match.credentialRecord,
					disclosedPayload: match.disclosedPayload,
					useMode
				};
				break;
			case require_ClaimFormat.ClaimFormat.JwtW3cVc:
			case require_ClaimFormat.ClaimFormat.SdJwtW3cVc:
				presentationToCreate = {
					claimFormat: match.claimFormat === require_ClaimFormat.ClaimFormat.JwtW3cVc ? require_ClaimFormat.ClaimFormat.JwtW3cVp : require_ClaimFormat.ClaimFormat.SdJwtW3cVp,
					credentialRecord: match.credentialRecord,
					disclosedPayload: match.disclosedPayload
				};
				break;
			default: presentationToCreate = {
				claimFormat: match.claimFormat === require_ClaimFormat.ClaimFormat.LdpVc ? require_ClaimFormat.ClaimFormat.LdpVp : require_ClaimFormat.ClaimFormat.JwtVp,
				credentialRecord: match.credentialRecord,
				disclosedPayload: match.disclosedPayload,
				useMode
			};
		}
		if (!presentationsToCreate[credentialQueryId]) presentationsToCreate[credentialQueryId] = [presentationToCreate];
		else presentationsToCreate[credentialQueryId].push(presentationToCreate);
	}
	return presentationsToCreate;
}

//#endregion
exports.dcqlGetPresentationsToCreate = dcqlGetPresentationsToCreate;