

const require_JsonTransformer = require('../../../../utils/JsonTransformer.js');
require('../../../../utils/index.js');
const require_PublicJwk = require('../../../kms/jwk/PublicJwk.js');
const require_KeyManagementApi = require('../../../kms/KeyManagementApi.js');
require('../../../kms/index.js');
const require_DidDocument = require('../../domain/DidDocument.js');
const require_DidDocumentRole = require('../../domain/DidDocumentRole.js');
require('../../domain/index.js');
const require_DidRecord = require('../../repository/DidRecord.js');
const require_DidRepository = require('../../repository/DidRepository.js');
require('../../repository/index.js');
const require_peerDidNumAlgo4 = require('./peerDidNumAlgo4.js');
const require_didPeer = require('./didPeer.js');
const require_peerDidNumAlgo0 = require('./peerDidNumAlgo0.js');
const require_peerDidNumAlgo1 = require('./peerDidNumAlgo1.js');
const require_peerDidNumAlgo2 = require('./peerDidNumAlgo2.js');

//#region src/modules/dids/methods/peer/PeerDidRegistrar.ts
var PeerDidRegistrar = class {
	constructor() {
		this.supportedMethods = ["peer"];
	}
	async create(agentContext, options) {
		const kms = agentContext.dependencyManager.resolve(require_KeyManagementApi.KeyManagementApi);
		const didRepository = agentContext.dependencyManager.resolve(require_DidRepository.DidRepository);
		let did;
		let didDocument;
		let keys;
		try {
			if (isPeerDidNumAlgo0CreateOptions(options)) {
				let publicJwk;
				if (options.options.createKey) {
					const createKeyResult = await kms.createKey(options.options.createKey);
					publicJwk = require_PublicJwk.PublicJwk.fromPublicJwk(createKeyResult.publicJwk);
					keys = [{
						didDocumentRelativeKeyId: `#${publicJwk.fingerprint}`,
						kmsKeyId: createKeyResult.keyId
					}];
				} else {
					const _publicJwk = await kms.getPublicKey({ 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 = require_PublicJwk.PublicJwk.fromPublicJwk(_publicJwk);
					keys = [{
						didDocumentRelativeKeyId: `#${publicJwk.fingerprint}`,
						kmsKeyId: options.options.keyId
					}];
				}
				didDocument = require_peerDidNumAlgo0.publicJwkToNumAlgo0DidDocument(publicJwk);
				did = didDocument.id;
			} else if (isPeerDidNumAlgo1CreateOptions(options)) {
				const didDocumentJson = options.didDocument.toJSON();
				did = require_peerDidNumAlgo1.didDocumentJsonToNumAlgo1Did(didDocumentJson);
				keys = options.options.keys;
				didDocument = require_JsonTransformer.JsonTransformer.fromJSON({
					...didDocumentJson,
					id: did
				}, require_DidDocument.DidDocument);
			} else if (isPeerDidNumAlgo2CreateOptions(options)) {
				const didDocumentJson = options.didDocument.toJSON();
				did = require_peerDidNumAlgo2.didDocumentToNumAlgo2Did(options.didDocument);
				keys = options.options.keys;
				didDocument = require_JsonTransformer.JsonTransformer.fromJSON({
					...didDocumentJson,
					id: did
				}, require_DidDocument.DidDocument);
			} else if (isPeerDidNumAlgo4CreateOptions(options)) {
				const didDocumentJson = options.didDocument.toJSON();
				keys = options.options.keys;
				const { longFormDid, shortFormDid } = require_peerDidNumAlgo4.didDocumentToNumAlgo4Did(options.didDocument);
				did = longFormDid;
				didDocument = require_JsonTransformer.JsonTransformer.fromJSON({
					...didDocumentJson,
					id: longFormDid,
					alsoKnownAs: [shortFormDid]
				}, require_DidDocument.DidDocument);
			} else return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "failed",
					reason: "Missing or incorrect numAlgo provided"
				}
			};
			if (!keys || keys.length === 0) return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "failed",
					reason: `Missing required 'keys' linking did document verification method id to the kms key id. Provide at least one key in the create options`
				}
			};
			const didRecord = new require_DidRecord.DidRecord({
				did,
				role: require_DidDocumentRole.DidDocumentRole.Created,
				didDocument: isPeerDidNumAlgo1CreateOptions(options) ? didDocument : void 0,
				keys,
				tags: {
					recipientKeyFingerprints: didDocument.recipientKeys.map((key) => key.fingerprint),
					alternativeDids: require_didPeer.getAlternativeDidsForPeerDid(did)
				}
			});
			await didRepository.save(agentContext, didRecord);
			return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "finished",
					did: didDocument.id,
					didDocument
				}
			};
		} catch (error) {
			return {
				didDocumentMetadata: {},
				didRegistrationMetadata: {},
				didState: {
					state: "failed",
					reason: `unknownError: ${error.message}`
				}
			};
		}
	}
	async update() {
		return {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				reason: "notImplemented: updating did:peer not implemented yet"
			}
		};
	}
	async deactivate() {
		return {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				reason: "notImplemented: deactivating did:peer not implemented yet"
			}
		};
	}
};
function isPeerDidNumAlgo1CreateOptions(options) {
	return options.options.numAlgo === require_didPeer.PeerDidNumAlgo.GenesisDoc;
}
function isPeerDidNumAlgo0CreateOptions(options) {
	return options.options.numAlgo === require_didPeer.PeerDidNumAlgo.InceptionKeyWithoutDoc;
}
function isPeerDidNumAlgo2CreateOptions(options) {
	return options.options.numAlgo === require_didPeer.PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc;
}
function isPeerDidNumAlgo4CreateOptions(options) {
	return options.options.numAlgo === require_didPeer.PeerDidNumAlgo.ShortFormAndLongForm;
}

//#endregion
exports.PeerDidRegistrar = PeerDidRegistrar;