

import { PublicJwk } from "../../../kms/jwk/PublicJwk.mjs";
import { KeyManagementApi } from "../../../kms/KeyManagementApi.mjs";
import "../../../kms/index.mjs";
import { DidDocumentRole } from "../../domain/DidDocumentRole.mjs";
import { DidRecord } from "../../repository/DidRecord.mjs";
import { DidRepository } from "../../repository/DidRepository.mjs";
import "../../repository/index.mjs";
import { DidJwk } from "./DidJwk.mjs";

//#region src/modules/dids/methods/jwk/JwkDidRegistrar.ts
var JwkDidRegistrar = class {
	constructor() {
		this.supportedMethods = ["jwk"];
	}
	async create(agentContext, options) {
		const didRepository = agentContext.dependencyManager.resolve(DidRepository);
		try {
			let publicJwk;
			let keyId;
			const kms = agentContext.dependencyManager.resolve(KeyManagementApi);
			if (options.options.createKey) {
				const createKeyResult = await kms.createKey(options.options.createKey);
				publicJwk = createKeyResult.publicJwk;
				keyId = createKeyResult.keyId;
			} else if (options.options.keyId) {
				const _publicJwk = await kms.getPublicKey({ keyId: options.options.keyId });
				keyId = options.options.keyId;
				if (!_publicJwk) return {
					didDocumentMetadata: {},
					didRegistrationMetadata: {},
					didState: {
						state: "failed",
						reason: `notFound: key with key id '${options.options.keyId}' not found`
					}
				};
				if (_publicJwk.kty === "oct") return {
					didDocumentMetadata: {},
					didRegistrationMetadata: {},
					didState: {
						state: "failed",
						reason: `notFound: key with key id '${options.options.keyId}' uses unsupported kty 'oct' for did:jwk`
					}
				};
				publicJwk = _publicJwk;
			} else return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "failed",
					reason: "Missing keyId or createKey"
				}
			};
			const didJwk = DidJwk.fromPublicJwk(PublicJwk.fromPublicJwk(publicJwk));
			const didRecord = new DidRecord({
				did: didJwk.did,
				role: DidDocumentRole.Created,
				keys: [{
					didDocumentRelativeKeyId: "#0",
					kmsKeyId: keyId
				}]
			});
			await didRepository.save(agentContext, didRecord);
			return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "finished",
					did: didJwk.did,
					didDocument: didJwk.didDocument
				}
			};
		} catch (error) {
			return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "failed",
					reason: `unknownError: ${error.message}`
				}
			};
		}
	}
	async update() {
		return {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				reason: "notSupported: cannot update did:jwk did"
			}
		};
	}
	async deactivate() {
		return {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				reason: "notSupported: cannot deactivate did:jwk did"
			}
		};
	}
};

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