

const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_PublicJwk = require('../../../kms/jwk/PublicJwk.js');
require('../../../kms/index.js');
const require_Ed25519VerificationKey2018 = require('../../domain/verificationMethod/Ed25519VerificationKey2018.js');
const require_X25519KeyAgreementKey2019 = require('../../domain/verificationMethod/X25519KeyAgreementKey2019.js');
const require_DidCommV1Service = require('../../domain/service/DidCommV1Service.js');
const require_DidDocumentBuilder = require('../../domain/DidDocumentBuilder.js');
require('../../domain/index.js');
const require_DidKey = require('../key/DidKey.js');
require('../key/index.js');
let __stablelib_ed25519 = require("@stablelib/ed25519");
__stablelib_ed25519 = require_rolldown_runtime.__toESM(__stablelib_ed25519);

//#region src/modules/dids/methods/peer/createPeerDidDocumentFromServices.ts
function createPeerDidDocumentFromServices(services, withKeys) {
	const didDocumentBuilder = new require_DidDocumentBuilder.DidDocumentBuilder("");
	const recipientKeyIdMapping = {};
	const keys = [];
	let keyIndex = 1;
	services.forEach((service, index) => {
		const recipientKeys = service.recipientKeys.map((recipientKey) => {
			if (recipientKeyIdMapping[recipientKey.fingerprint]) return recipientKeyIdMapping[recipientKey.fingerprint];
			const x25519Key = require_PublicJwk.PublicJwk.fromPublicKey({
				crv: "X25519",
				kty: "OKP",
				publicKey: (0, __stablelib_ed25519.convertPublicKeyToX25519)(recipientKey.publicKey.publicKey)
			});
			const ed25519RelativeVerificationMethodId = `#key-${keyIndex++}`;
			const ed25519VerificationMethod = require_Ed25519VerificationKey2018.getEd25519VerificationKey2018({
				id: ed25519RelativeVerificationMethodId,
				publicJwk: recipientKey,
				controller: "#id"
			});
			const x25519RelativeVerificationMethodId = `#key-${keyIndex++}`;
			const x25519VerificationMethod = require_X25519KeyAgreementKey2019.getX25519KeyAgreementKey2019({
				id: x25519RelativeVerificationMethodId,
				publicJwk: x25519Key,
				controller: "#id"
			});
			recipientKeyIdMapping[recipientKey.fingerprint] = ed25519VerificationMethod.id;
			if (withKeys) {
				keys.push({
					didDocumentRelativeKeyId: ed25519RelativeVerificationMethodId,
					kmsKeyId: recipientKey.keyId
				});
				keys.push({
					didDocumentRelativeKeyId: x25519RelativeVerificationMethodId,
					kmsKeyId: recipientKey.keyId
				});
			}
			didDocumentBuilder.addAuthentication(ed25519VerificationMethod).addKeyAgreement(x25519VerificationMethod);
			return recipientKeyIdMapping[recipientKey.fingerprint];
		});
		const routingKeys = service.routingKeys?.map((key) => {
			return `${new require_DidKey.DidKey(key).did}#${key.fingerprint}`;
		});
		didDocumentBuilder.addService(new require_DidCommV1Service.DidCommV1Service({
			id: service.id,
			priority: index,
			serviceEndpoint: service.serviceEndpoint,
			recipientKeys,
			routingKeys
		}));
	});
	return {
		didDocument: didDocumentBuilder.build(),
		keys: withKeys ? keys : void 0
	};
}

//#endregion
exports.createPeerDidDocumentFromServices = createPeerDidDocumentFromServices;