

const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.js');
const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_humanDescription = require('../../kms/jwk/humanDescription.js');
const require_P256PublicJwk = require('../../kms/jwk/kty/ec/P256PublicJwk.js');
const require_P384PublicJwk = require('../../kms/jwk/kty/ec/P384PublicJwk.js');
const require_P521PublicJwk = require('../../kms/jwk/kty/ec/P521PublicJwk.js');
const require_Secp256k1PublicJwk = require('../../kms/jwk/kty/ec/Secp256k1PublicJwk.js');
const require_X25519PublicJwk = require('../../kms/jwk/kty/okp/X25519PublicJwk.js');
const require_Ed25519PublicJwk = require('../../kms/jwk/kty/okp/Ed25519PublicJwk.js');
const require_PublicJwk = require('../../kms/jwk/PublicJwk.js');
require('../../kms/index.js');
const require_constants = require('../../vc/constants.js');
const require_Ed25519VerificationKey2018 = require('./verificationMethod/Ed25519VerificationKey2018.js');
const require_JsonWebKey2020 = require('./verificationMethod/JsonWebKey2020.js');
const require_X25519KeyAgreementKey2019 = require('./verificationMethod/X25519KeyAgreementKey2019.js');
require('./verificationMethod/index.js');
require('./key-type/ed25519.js');
const require_DidDocumentBuilder = require('./DidDocumentBuilder.js');
const require_constants$1 = require('../../vc/data-integrity/signature-suites/ed25519/constants.js');
let __stablelib_ed25519 = require("@stablelib/ed25519");
__stablelib_ed25519 = require_rolldown_runtime.__toESM(__stablelib_ed25519);

//#region src/modules/dids/domain/keyDidDocument.ts
function getDidDocumentForPublicJwk(did, publicJwk) {
	if (publicJwk.is(require_Ed25519PublicJwk.Ed25519PublicJwk)) return getEd25519DidDoc(did, publicJwk);
	if (publicJwk.is(require_X25519PublicJwk.X25519PublicJwk)) return getX25519DidDoc(did, publicJwk);
	if (publicJwk.is(require_P256PublicJwk.P256PublicJwk) || publicJwk.is(require_P384PublicJwk.P384PublicJwk) || publicJwk.is(require_P521PublicJwk.P521PublicJwk) || publicJwk.is(require_Secp256k1PublicJwk.Secp256k1PublicJwk)) return getJsonWebKey2020DidDocument(did, publicJwk);
	throw new require_CredoError.CredoError(`Unsupported public key type for did document: ${require_humanDescription.getJwkHumanDescription(publicJwk.toJson())}`);
}
function getJsonWebKey2020DidDocument(did, publicJwk) {
	const verificationMethod = require_JsonWebKey2020.getJsonWebKey2020({
		did,
		publicJwk
	});
	const didDocumentBuilder = new require_DidDocumentBuilder.DidDocumentBuilder(did);
	didDocumentBuilder.addContext(require_constants.SECURITY_JWS_CONTEXT_URL).addVerificationMethod(verificationMethod);
	if (publicJwk.supportedSignatureAlgorithms.length === 0 && publicJwk.supportdEncryptionKeyAgreementAlgorithms.length === 0) throw new require_CredoError.CredoError("Key must support at least signing or encrypting");
	if (publicJwk.supportedSignatureAlgorithms.length > 0) didDocumentBuilder.addAuthentication(verificationMethod.id).addAssertionMethod(verificationMethod.id).addCapabilityDelegation(verificationMethod.id).addCapabilityInvocation(verificationMethod.id);
	if (publicJwk.supportdEncryptionKeyAgreementAlgorithms.length > 0) didDocumentBuilder.addKeyAgreement(verificationMethod.id);
	return didDocumentBuilder.build();
}
function getEd25519DidDoc(did, publicJwk) {
	const verificationMethod = require_Ed25519VerificationKey2018.getEd25519VerificationKey2018({
		id: `${did}#${publicJwk.fingerprint}`,
		publicJwk,
		controller: did
	});
	const publicKeyX25519 = (0, __stablelib_ed25519.convertPublicKeyToX25519)(publicJwk.publicKey.publicKey);
	const publicJwkX25519 = require_PublicJwk.PublicJwk.fromPublicKey({
		kty: "OKP",
		crv: "X25519",
		publicKey: publicKeyX25519
	});
	const x25519VerificationMethod = require_X25519KeyAgreementKey2019.getX25519KeyAgreementKey2019({
		id: `${did}#${publicJwkX25519.fingerprint}`,
		publicJwk: publicJwkX25519,
		controller: did
	});
	const didDocBuilder = getSignatureKeyBase({
		did,
		publicJwk,
		verificationMethod
	});
	didDocBuilder.addContext(require_constants$1.ED25519_SUITE_CONTEXT_URL_2018).addContext(require_constants.SECURITY_X25519_CONTEXT_URL).addKeyAgreement(x25519VerificationMethod);
	return didDocBuilder.build();
}
function getX25519DidDoc(did, publicJwk) {
	const verificationMethod = require_X25519KeyAgreementKey2019.getX25519KeyAgreementKey2019({
		id: `${did}#${publicJwk.fingerprint}`,
		publicJwk,
		controller: did
	});
	return new require_DidDocumentBuilder.DidDocumentBuilder(did).addKeyAgreement(verificationMethod).addContext(require_constants.SECURITY_X25519_CONTEXT_URL).build();
}
function getSignatureKeyBase({ did, publicJwk, verificationMethod }) {
	const keyId = `${did}#${publicJwk.fingerprint}`;
	return new require_DidDocumentBuilder.DidDocumentBuilder(did).addVerificationMethod(verificationMethod).addAuthentication(keyId).addAssertionMethod(keyId).addCapabilityDelegation(keyId).addCapabilityInvocation(keyId);
}

//#endregion
exports.getDidDocumentForPublicJwk = getDidDocumentForPublicJwk;