const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_decorateMetadata = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateMetadata.js');
const require_decorate = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/services/DidCommDocumentService.ts
var _ref, _ref2;
let DidCommDocumentService = class DidCommDocumentService$1 {
	constructor(didResolverService, didRepository) {
		this.didResolverService = didResolverService;
		this.didRepository = didRepository;
	}
	async resolveServicesFromDid(agentContext, did) {
		const didDocument = await this.didResolverService.resolveDidDocument(agentContext, did);
		const resolvedServices = [];
		const didCommServices = (0, __credo_ts_core.parseDid)(did).fragment ? didDocument.didCommServices.filter((service) => service.id === did) : didDocument.didCommServices;
		for (const didCommService of didCommServices) if (didCommService.type === __credo_ts_core.IndyAgentService.type) resolvedServices.push({
			id: didCommService.id,
			recipientKeys: didCommService.recipientKeys.map(__credo_ts_core.verkeyToPublicJwk),
			routingKeys: didCommService.routingKeys?.map(__credo_ts_core.verkeyToPublicJwk) || [],
			serviceEndpoint: didCommService.serviceEndpoint
		});
		else if (didCommService.type === __credo_ts_core.DidCommV1Service.type) {
			const routingKeys = [];
			for (const routingKey of didCommService.routingKeys ?? []) {
				const publicJwk = (0, __credo_ts_core.getPublicJwkFromVerificationMethod)((await this.didResolverService.resolveDidDocument(agentContext, routingKey)).dereferenceKey(routingKey, ["authentication", "keyAgreement"]));
				if (!publicJwk.is(__credo_ts_core.Kms.Ed25519PublicJwk)) throw new __credo_ts_core.CredoError(`Expected Ed25519PublicJwk but found ${publicJwk.JwkClass.name}`);
				routingKeys.push(publicJwk);
			}
			const recipientKeys = didCommService.recipientKeys.map((recipientKeyReference) => {
				const publicJwk = (0, __credo_ts_core.getPublicJwkFromVerificationMethod)(didDocument.dereferenceKey(recipientKeyReference, ["authentication", "keyAgreement"]));
				if (publicJwk.is(__credo_ts_core.Kms.X25519PublicJwk)) {
					const matchingEd25519Key = (0, __credo_ts_core.findMatchingEd25519Key)(publicJwk, didDocument);
					if (matchingEd25519Key) return matchingEd25519Key.publicJwk;
				}
				if (!publicJwk.is(__credo_ts_core.Kms.Ed25519PublicJwk)) throw new __credo_ts_core.CredoError(`Expected Ed25519PublicJwk but found ${publicJwk.JwkClass.name}`);
				return publicJwk;
			});
			resolvedServices.push({
				id: didCommService.id,
				recipientKeys,
				routingKeys,
				serviceEndpoint: didCommService.serviceEndpoint
			});
		}
		return resolvedServices;
	}
	async resolveCreatedDidDocumentWithKeysByRecipientKey(agentContext, publicJwk) {
		const didRecord = await this.didRepository.findCreatedDidByRecipientKey(agentContext, publicJwk);
		if (!didRecord) throw new __credo_ts_core.RecordNotFoundError(`Created did for public jwk ${publicJwk.jwkTypehumanDescription} not found`, { recordType: __credo_ts_core.DidRecord.type });
		if (didRecord.didDocument) return {
			keys: didRecord.keys,
			didDocument: didRecord.didDocument
		};
		const didDocument = await this.didResolverService.resolveDidDocument(agentContext, didRecord.did);
		return {
			keys: didRecord.keys,
			didDocument
		};
	}
};
DidCommDocumentService = require_decorate.__decorate([(0, __credo_ts_core.injectable)(), require_decorateMetadata.__decorateMetadata("design:paramtypes", [typeof (_ref = typeof __credo_ts_core.DidResolverService !== "undefined" && __credo_ts_core.DidResolverService) === "function" ? _ref : Object, typeof (_ref2 = typeof __credo_ts_core.DidRepository !== "undefined" && __credo_ts_core.DidRepository) === "function" ? _ref2 : Object])], DidCommDocumentService);

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