

const require_PublicJwk = require('../../../kms/jwk/PublicJwk.js');
const require_KeyManagementApi = require('../../../kms/KeyManagementApi.js');
require('../../../kms/index.js');
const require_DidDocumentRole = require('../../domain/DidDocumentRole.js');
const require_DidRecord = require('../../repository/DidRecord.js');
const require_DidRepository = require('../../repository/DidRepository.js');
require('../../repository/index.js');
const require_DidJwk = require('./DidJwk.js');

//#region src/modules/dids/methods/jwk/JwkDidRegistrar.ts
var JwkDidRegistrar = class {
	constructor() {
		this.supportedMethods = ["jwk"];
	}
	async create(agentContext, options) {
		const didRepository = agentContext.dependencyManager.resolve(require_DidRepository.DidRepository);
		try {
			let publicJwk;
			let keyId;
			const kms = agentContext.dependencyManager.resolve(require_KeyManagementApi.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 = require_DidJwk.DidJwk.fromPublicJwk(require_PublicJwk.PublicJwk.fromPublicJwk(publicJwk));
			const didRecord = new require_DidRecord.DidRecord({
				did: didJwk.did,
				role: require_DidDocumentRole.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
exports.JwkDidRegistrar = JwkDidRegistrar;