

const require_CredoError = require('../../../../error/CredoError.js');
require('../../../../error/index.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_peerDidNumAlgo2 = require('./peerDidNumAlgo2.js');

//#region src/modules/dids/methods/peer/PeerDidResolver.ts
var PeerDidResolver = class {
	constructor() {
		this.supportedMethods = ["peer"];
		this.allowsCaching = false;
		this.allowsLocalDidRecord = false;
	}
	async resolve(agentContext, did) {
		const didRepository = agentContext.dependencyManager.resolve(require_DidRepository.DidRepository);
		const didDocumentMetadata = {};
		try {
			let didDocument;
			if (!require_didPeer.isValidPeerDid(did)) throw new require_CredoError.CredoError(`did ${did} is not a valid peer did`);
			const numAlgo = require_didPeer.getNumAlgoFromPeerDid(did);
			if (numAlgo === require_didPeer.PeerDidNumAlgo.InceptionKeyWithoutDoc) didDocument = require_peerDidNumAlgo0.didToNumAlgo0DidDocument(did);
			else if (numAlgo === require_didPeer.PeerDidNumAlgo.GenesisDoc) {
				const [didDocumentRecord] = await didRepository.findAllByDid(agentContext, did);
				if (!didDocumentRecord) throw new require_CredoError.CredoError(`No did record found for peer did ${did}.`);
				if (!didDocumentRecord.didDocument) throw new require_CredoError.CredoError(`Found did record for method 1 peer did (${did}), but no did document.`);
				didDocument = didDocumentRecord.didDocument;
			} else if (numAlgo === require_didPeer.PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc) didDocument = require_peerDidNumAlgo2.didToNumAlgo2DidDocument(did);
			else if (require_peerDidNumAlgo4.isShortFormDidPeer4(did)) {
				const [didRecord] = await didRepository.findAllByDid(agentContext, did);
				if (!didRecord) throw new require_CredoError.CredoError(`No did record found for peer did ${did}.`);
				didDocument = require_peerDidNumAlgo4.didToNumAlgo4DidDocument(didRecord.did);
			} else didDocument = require_peerDidNumAlgo4.didToNumAlgo4DidDocument(did);
			return {
				didDocument,
				didDocumentMetadata,
				didResolutionMetadata: { contentType: "application/did+ld+json" }
			};
		} catch (error) {
			agentContext.config.logger.error(`Error resolving did '${did}'`, { error });
			return {
				didDocument: null,
				didDocumentMetadata,
				didResolutionMetadata: {
					error: "notFound",
					message: `resolver_error: Unable to resolve did '${did}': ${error}`
				}
			};
		}
	}
};

//#endregion
exports.PeerDidResolver = PeerDidResolver;