

import { PublicJwk } from "../../../kms/jwk/PublicJwk.mjs";
import "../../../kms/index.mjs";
import { DidCommV1Service } from "../../domain/service/DidCommV1Service.mjs";
import { getEd25519VerificationKey2018 } from "../../domain/verificationMethod/Ed25519VerificationKey2018.mjs";
import { getX25519KeyAgreementKey2019 } from "../../domain/verificationMethod/X25519KeyAgreementKey2019.mjs";
import { DidDocumentBuilder } from "../../domain/DidDocumentBuilder.mjs";
import "../../domain/index.mjs";
import { DidKey } from "../key/DidKey.mjs";
import "../key/index.mjs";
import { convertPublicKeyToX25519 } from "@stablelib/ed25519";

//#region src/modules/dids/methods/peer/createPeerDidDocumentFromServices.ts
function createPeerDidDocumentFromServices(services, withKeys) {
	const didDocumentBuilder = new 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 = PublicJwk.fromPublicKey({
				crv: "X25519",
				kty: "OKP",
				publicKey: convertPublicKeyToX25519(recipientKey.publicKey.publicKey)
			});
			const ed25519RelativeVerificationMethodId = `#key-${keyIndex++}`;
			const ed25519VerificationMethod = getEd25519VerificationKey2018({
				id: ed25519RelativeVerificationMethodId,
				publicJwk: recipientKey,
				controller: "#id"
			});
			const x25519RelativeVerificationMethodId = `#key-${keyIndex++}`;
			const x25519VerificationMethod = 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 DidKey(key).did}#${key.fingerprint}`;
		});
		didDocumentBuilder.addService(new DidCommV1Service({
			id: service.id,
			priority: index,
			serviceEndpoint: service.serviceEndpoint,
			recipientKeys,
			routingKeys
		}));
	});
	return {
		didDocument: didDocumentBuilder.build(),
		keys: withKeys ? keys : void 0
	};
}

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