

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

//#region src/modules/dids/methods/key/KeyDidRegistrar.ts
var KeyDidRegistrar = class {
	constructor() {
		this.supportedMethods = ["key"];
	}
	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 {
				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:key`
					}
				};
				publicJwk = _publicJwk;
			}
			const didKey = new DidKey(PublicJwk.fromPublicJwk(publicJwk));
			const didRecord = new DidRecord({
				did: didKey.did,
				role: DidDocumentRole.Created,
				keys: [{
					didDocumentRelativeKeyId: `#${didKey.publicJwk.fingerprint}`,
					kmsKeyId: keyId
				}]
			});
			await didRepository.save(agentContext, didRecord);
			return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "finished",
					did: didKey.did,
					didDocument: didKey.didDocument,
					secret: {}
				}
			};
		} 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:key did"
			}
		};
	}
	async deactivate() {
		return {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				reason: "notSupported: cannot deactivate did:key did"
			}
		};
	}
};

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