

import { CredoError } from "../../../error/CredoError.mjs";
import "../../../error/index.mjs";
import { getJwkHumanDescription } from "../../kms/jwk/humanDescription.mjs";
import { P256PublicJwk } from "../../kms/jwk/kty/ec/P256PublicJwk.mjs";
import { P384PublicJwk } from "../../kms/jwk/kty/ec/P384PublicJwk.mjs";
import { P521PublicJwk } from "../../kms/jwk/kty/ec/P521PublicJwk.mjs";
import { Secp256k1PublicJwk } from "../../kms/jwk/kty/ec/Secp256k1PublicJwk.mjs";
import { X25519PublicJwk } from "../../kms/jwk/kty/okp/X25519PublicJwk.mjs";
import { Ed25519PublicJwk } from "../../kms/jwk/kty/okp/Ed25519PublicJwk.mjs";
import { PublicJwk } from "../../kms/jwk/PublicJwk.mjs";
import "../../kms/index.mjs";
import { getEd25519VerificationKey2018 } from "./verificationMethod/Ed25519VerificationKey2018.mjs";
import { getJsonWebKey2020 } from "./verificationMethod/JsonWebKey2020.mjs";
import { getX25519KeyAgreementKey2019 } from "./verificationMethod/X25519KeyAgreementKey2019.mjs";
import "./verificationMethod/index.mjs";
import { convertPublicKeyToX25519 } from "./key-type/ed25519.mjs";
import { DidDocumentBuilder } from "./DidDocumentBuilder.mjs";
import { SECURITY_JWS_CONTEXT_URL, SECURITY_X25519_CONTEXT_URL } from "../../vc/constants.mjs";
import { ED25519_SUITE_CONTEXT_URL_2018 } from "../../vc/data-integrity/signature-suites/ed25519/constants.mjs";

//#region src/modules/dids/domain/keyDidDocument.ts
function getDidDocumentForPublicJwk(did, publicJwk) {
	if (publicJwk.is(Ed25519PublicJwk)) return getEd25519DidDoc(did, publicJwk);
	if (publicJwk.is(X25519PublicJwk)) return getX25519DidDoc(did, publicJwk);
	if (publicJwk.is(P256PublicJwk) || publicJwk.is(P384PublicJwk) || publicJwk.is(P521PublicJwk) || publicJwk.is(Secp256k1PublicJwk)) return getJsonWebKey2020DidDocument(did, publicJwk);
	throw new CredoError(`Unsupported public key type for did document: ${getJwkHumanDescription(publicJwk.toJson())}`);
}
function getJsonWebKey2020DidDocument(did, publicJwk) {
	const verificationMethod = getJsonWebKey2020({
		did,
		publicJwk
	});
	const didDocumentBuilder = new DidDocumentBuilder(did);
	didDocumentBuilder.addContext(SECURITY_JWS_CONTEXT_URL).addVerificationMethod(verificationMethod);
	if (publicJwk.supportedSignatureAlgorithms.length === 0 && publicJwk.supportdEncryptionKeyAgreementAlgorithms.length === 0) throw new 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 = getEd25519VerificationKey2018({
		id: `${did}#${publicJwk.fingerprint}`,
		publicJwk,
		controller: did
	});
	const publicKeyX25519 = convertPublicKeyToX25519(publicJwk.publicKey.publicKey);
	const publicJwkX25519 = PublicJwk.fromPublicKey({
		kty: "OKP",
		crv: "X25519",
		publicKey: publicKeyX25519
	});
	const x25519VerificationMethod = getX25519KeyAgreementKey2019({
		id: `${did}#${publicJwkX25519.fingerprint}`,
		publicJwk: publicJwkX25519,
		controller: did
	});
	const didDocBuilder = getSignatureKeyBase({
		did,
		publicJwk,
		verificationMethod
	});
	didDocBuilder.addContext(ED25519_SUITE_CONTEXT_URL_2018).addContext(SECURITY_X25519_CONTEXT_URL).addKeyAgreement(x25519VerificationMethod);
	return didDocBuilder.build();
}
function getX25519DidDoc(did, publicJwk) {
	const verificationMethod = getX25519KeyAgreementKey2019({
		id: `${did}#${publicJwk.fingerprint}`,
		publicJwk,
		controller: did
	});
	return new DidDocumentBuilder(did).addKeyAgreement(verificationMethod).addContext(SECURITY_X25519_CONTEXT_URL).build();
}
function getSignatureKeyBase({ did, publicJwk, verificationMethod }) {
	const keyId = `${did}#${publicJwk.fingerprint}`;
	return new DidDocumentBuilder(did).addVerificationMethod(verificationMethod).addAuthentication(keyId).addAssertionMethod(keyId).addCapabilityDelegation(keyId).addCapabilityInvocation(keyId);
}

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