

const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.js');
const require_constants = require('../../../constants.js');
const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
require('../../../plugins/index.js');
const require_decorateMetadata = require('../../../_virtual/_@oxc-project_runtime@0.97.0/helpers/decorateMetadata.js');
const require_decorate = require('../../../_virtual/_@oxc-project_runtime@0.97.0/helpers/decorate.js');
const require_JsonTransformer = require('../../../utils/JsonTransformer.js');
require('../../../utils/index.js');
const require_parse = require('../domain/parse.js');
const require_DidDocument = require('../domain/DidDocument.js');
require('../domain/index.js');
const require_decorateParam = require('../../../_virtual/_@oxc-project_runtime@0.97.0/helpers/decorateParam.js');
const require_CacheModuleConfig = require('../../cache/CacheModuleConfig.js');
const require_DidRepository = require('../repository/DidRepository.js');
require('../repository/index.js');
const require_DidsModuleConfig = require('../DidsModuleConfig.js');
require('../../cache/index.js');
let tsyringe = require("tsyringe");

//#region src/modules/dids/services/DidResolverService.ts
var _ref, _ref2;
let DidResolverService = class DidResolverService$1 {
	constructor(logger, didsModuleConfig, didRepository) {
		this.logger = logger;
		this.didsModuleConfig = didsModuleConfig;
		this.didRepository = didRepository;
	}
	async resolve(agentContext, didUrl, options = {}) {
		this.logger.debug(`resolving didUrl ${didUrl}`);
		const result = {
			didResolutionMetadata: {},
			didDocument: null,
			didDocumentMetadata: {}
		};
		let parsed;
		try {
			parsed = require_parse.parseDid(didUrl);
		} catch (_error) {
			return {
				...result,
				didResolutionMetadata: { error: "invalidDid" }
			};
		}
		const resolver = this.findResolver(parsed);
		if (!resolver) return {
			...result,
			didResolutionMetadata: {
				error: "unsupportedDidMethod",
				message: `No did resolver registered for did method ${parsed.method}`
			}
		};
		const { useCache = true, cacheDurationInSeconds = 300, persistInCache = true, useLocalCreatedDidRecord = true } = options;
		const cacheKey = this.getCacheKey(parsed.did);
		if (resolver.allowsCaching && useCache) {
			const cachedDidDocument = await agentContext.dependencyManager.resolve(require_CacheModuleConfig.CacheModuleConfig).cache.get(agentContext, cacheKey);
			if (cachedDidDocument) return {
				...cachedDidDocument,
				didDocument: require_JsonTransformer.JsonTransformer.fromJSON(cachedDidDocument.didDocument, require_DidDocument.DidDocument),
				didResolutionMetadata: {
					...cachedDidDocument.didResolutionMetadata,
					servedFromCache: true
				}
			};
		}
		if (resolver.allowsLocalDidRecord && useLocalCreatedDidRecord) {
			const [didRecord] = await this.didRepository.getCreatedDids(agentContext, { did: parsed.did });
			if (didRecord?.didDocument) return {
				didDocument: didRecord.didDocument,
				didDocumentMetadata: {},
				didResolutionMetadata: {
					servedFromCache: false,
					servedFromDidRecord: true
				}
			};
		}
		let resolutionResult = await resolver.resolve(agentContext, parsed.did, parsed, options);
		resolutionResult = {
			...resolutionResult,
			didResolutionMetadata: {
				...resolutionResult.didResolutionMetadata,
				resolutionTime: Date.now(),
				servedFromCache: resolutionResult.didResolutionMetadata.servedFromCache ?? false
			}
		};
		if (resolutionResult.didDocument && resolver.allowsCaching && persistInCache) await agentContext.dependencyManager.resolve(require_CacheModuleConfig.CacheModuleConfig).cache.set(agentContext, cacheKey, {
			...resolutionResult,
			didDocument: resolutionResult.didDocument.toJSON()
		}, cacheDurationInSeconds);
		return resolutionResult;
	}
	/**
	* Resolve a did document. This uses the default resolution options, and thus
	* will use caching if available.
	*/
	async resolveDidDocument(agentContext, did) {
		const { didDocument, didResolutionMetadata: { error, message } } = await this.resolve(agentContext, did);
		if (!didDocument) throw new require_CredoError.CredoError(`Unable to resolve did document for did '${did}': ${error} ${message}`);
		return didDocument;
	}
	async invalidateCacheForDid(agentContext, did) {
		await agentContext.dependencyManager.resolve(require_CacheModuleConfig.CacheModuleConfig).cache.remove(agentContext, this.getCacheKey(did));
	}
	getCacheKey(did) {
		return `did:resolver:${did}`;
	}
	findResolver(parsed) {
		return this.didsModuleConfig.resolvers.find((r) => r.supportedMethods.includes(parsed.method)) ?? null;
	}
	/**
	* Get all supported did methods for the did resolver.
	*/
	get supportedMethods() {
		return Array.from(new Set(this.didsModuleConfig.resolvers.flatMap((r) => r.supportedMethods)));
	}
};
DidResolverService = require_decorate.__decorate([
	(0, tsyringe.injectable)(),
	require_decorateParam.__decorateParam(0, (0, tsyringe.inject)(require_constants.InjectionSymbols.Logger)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [
		Object,
		typeof (_ref = typeof require_DidsModuleConfig.DidsModuleConfig !== "undefined" && require_DidsModuleConfig.DidsModuleConfig) === "function" ? _ref : Object,
		typeof (_ref2 = typeof require_DidRepository.DidRepository !== "undefined" && require_DidRepository.DidRepository) === "function" ? _ref2 : Object
	])
], DidResolverService);

//#endregion
Object.defineProperty(exports, 'DidResolverService', {
  enumerable: true,
  get: function () {
    return DidResolverService;
  }
});