

const require_X25519PublicJwk = require('../kms/jwk/kty/okp/X25519PublicJwk.js');
const require_Ed25519PublicJwk = require('../kms/jwk/kty/okp/Ed25519PublicJwk.js');
const require_equals = require('../kms/jwk/equals.js');
require('../kms/index.js');
require('./domain/verificationMethod/index.js');
const require_keyDidMapping = require('./domain/key-type/keyDidMapping.js');
require('./domain/DidDocument.js');

//#region src/modules/dids/findMatchingEd25519Key.ts
/**
* Tries to find a matching Ed25519 key to the supplied X25519 key
* @param x25519Key X25519 key
* @param didDocument Did document containing all the keys
* @returns a matching Ed25519 key or `undefined` (if no matching key found)
*/
function findMatchingEd25519Key(x25519Key, didDocument) {
	const verificationMethods = didDocument.verificationMethod ?? [];
	const keyAgreements = didDocument.keyAgreement ?? [];
	const authentications = didDocument.authentication ?? [];
	return [
		...verificationMethods,
		...authentications.filter((keyAgreement) => typeof keyAgreement !== "string"),
		...keyAgreements.filter((keyAgreement) => typeof keyAgreement !== "string")
	].map((keyReference) => {
		const verificationMethod = didDocument.dereferenceKey(keyReference.id);
		return {
			publicJwk: require_keyDidMapping.getPublicJwkFromVerificationMethod(verificationMethod),
			verificationMethod
		};
	}).find((v) => {
		if (!v.publicJwk.is(require_Ed25519PublicJwk.Ed25519PublicJwk)) return false;
		return require_equals.assymetricPublicJwkMatches(v.publicJwk.convertTo(require_X25519PublicJwk.X25519PublicJwk).toJson(), x25519Key.toJson());
	});
}

//#endregion
exports.findMatchingEd25519Key = findMatchingEd25519Key;