

const require_ClaimFormat = require('../../vc/models/ClaimFormat.js');
require('../../vc/index.js');
const require_MdocRecord = require('../../mdoc/repository/MdocRecord.js');
require('../../mdoc/index.js');
require('../../sd-jwt-vc/index.js');

//#region src/modules/dif-presentation-exchange/utils/presentationsToCreate.ts
function getPresentationsToCreate(credentialsForInputDescriptor) {
	const presentationsToCreate = [];
	for (const [inputDescriptorId, credentials] of Object.entries(credentialsForInputDescriptor)) for (const credential of credentials) if (credential.claimFormat === require_ClaimFormat.ClaimFormat.SdJwtDc) presentationsToCreate.push({
		claimFormat: require_ClaimFormat.ClaimFormat.SdJwtDc,
		subjectIds: [],
		verifiableCredentials: [{
			inputDescriptorId,
			credential: credential.credentialRecord,
			additionalPayload: credential.additionalPayload
		}]
	});
	else if (credential.credentialRecord instanceof require_MdocRecord.MdocRecord) presentationsToCreate.push({
		claimFormat: require_ClaimFormat.ClaimFormat.MsoMdoc,
		verifiableCredentials: [{
			inputDescriptorId,
			credential: credential.credentialRecord
		}],
		subjectIds: []
	});
	else {
		const subjectId = credential.credentialRecord.firstCredential.credentialSubjectIds[0];
		const expectedClaimFormat = credential.credentialRecord.firstCredential.claimFormat === require_ClaimFormat.ClaimFormat.LdpVc ? require_ClaimFormat.ClaimFormat.LdpVp : require_ClaimFormat.ClaimFormat.JwtVp;
		const matchingClaimFormatAndSubject = presentationsToCreate.find((p) => p.claimFormat === expectedClaimFormat && Boolean(p.subjectIds?.includes(subjectId)));
		if (matchingClaimFormatAndSubject) matchingClaimFormatAndSubject.verifiableCredentials.push({
			inputDescriptorId,
			credential: credential.credentialRecord
		});
		else presentationsToCreate.push({
			claimFormat: expectedClaimFormat,
			subjectIds: [subjectId],
			verifiableCredentials: [{
				credential: credential.credentialRecord,
				inputDescriptorId
			}]
		});
	}
	return presentationsToCreate;
}

//#endregion
exports.getPresentationsToCreate = getPresentationsToCreate;