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');
const require_decorateParam = require('../../../../../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.js');
require('../../../../connections/index.js');
const require_DidCommCredentialEvents = require('../../../DidCommCredentialEvents.js');
const require_DidCommRevocationNotification = require('../../../models/DidCommRevocationNotification.js');
const require_DidCommCredentialExchangeRepository = require('../../../repository/DidCommCredentialExchangeRepository.js');
require('../../../repository/index.js');
const require_DidCommRevocationNotificationV2Message = require('../messages/DidCommRevocationNotificationV2Message.js');
const require_revocationIdentifier = require('../util/revocationIdentifier.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/modules/credentials/protocol/revocation-notification/services/DidCommRevocationNotificationService.ts
var _ref, _ref2;
let DidCommRevocationNotificationService = class DidCommRevocationNotificationService$1 {
	constructor(credentialRepository, eventEmitter, logger) {
		this.credentialRepository = credentialRepository;
		this.eventEmitter = eventEmitter;
		this.logger = logger;
	}
	async processRevocationNotification(agentContext, anonCredsRevocationRegistryId, anonCredsCredentialRevocationId, connection, comment) {
		const query = { $or: [{
			anonCredsRevocationRegistryId,
			anonCredsCredentialRevocationId,
			connectionId: connection.id
		}, {
			anonCredsUnqualifiedRevocationRegistryId: anonCredsRevocationRegistryId,
			anonCredsCredentialRevocationId,
			connectionId: connection.id
		}] };
		this.logger.trace("Getting record by query for revocation notification:", query);
		const credentialExchangeRecord = await this.credentialRepository.getSingleByQuery(agentContext, query);
		credentialExchangeRecord.revocationNotification = new require_DidCommRevocationNotification.DidCommRevocationNotification(comment);
		await this.credentialRepository.update(agentContext, credentialExchangeRecord);
		this.logger.trace("Emitting DidCommRevocationNotificationReceivedEvent");
		this.eventEmitter.emit(agentContext, {
			type: require_DidCommCredentialEvents.DidCommCredentialEventTypes.DidCommRevocationNotificationReceived,
			payload: { credentialExchangeRecord: credentialExchangeRecord.clone() }
		});
	}
	/**
	* Process a received {@link DidCommRevocationNotificationV1Message}. This will create a
	* {@link DidCommRevocationNotification} and store it in the corresponding {@link CredentialRecord}
	*
	* @param messageContext message context of RevocationNotificationMessageV1
	*/
	async v1ProcessRevocationNotification(messageContext) {
		this.logger.info("Processing revocation notification v1", { message: messageContext.message });
		const threadId = messageContext.message.issueThread;
		try {
			const threadIdGroups = threadId.match(require_revocationIdentifier.v1ThreadRegex);
			if (!threadIdGroups) throw new __credo_ts_core.CredoError(`Incorrect revocation notification threadId format: \n${threadId}\ndoes not match\n"indy::<revocation_registry_id>::<credential_revocation_id>"`);
			const [, , anonCredsRevocationRegistryId, anonCredsCredentialRevocationId] = threadIdGroups;
			const comment = messageContext.message.comment;
			const connection = messageContext.assertReadyConnection();
			await this.processRevocationNotification(messageContext.agentContext, anonCredsRevocationRegistryId, anonCredsCredentialRevocationId, connection, comment);
		} catch (error) {
			this.logger.warn("Failed to process revocation notification message", {
				error,
				threadId
			});
		}
	}
	/**
	* Create a V2 Revocation Notification message
	*/
	async v2CreateRevocationNotification(options) {
		const { credentialId, revocationFormat, comment, requestAck } = options;
		const message = new require_DidCommRevocationNotificationV2Message.DidCommRevocationNotificationV2Message({
			credentialId,
			revocationFormat,
			comment
		});
		if (requestAck) message.setPleaseAck();
		return { message };
	}
	/**
	* Process a received {@link DidCommRevocationNotificationV2Message}. This will create a
	* {@link DidCommRevocationNotification} and store it in the corresponding {@link CredentialRecord}
	*
	* @param messageContext message context of RevocationNotificationMessageV2
	*/
	async v2ProcessRevocationNotification(messageContext) {
		this.logger.info("Processing revocation notification v2", { message: messageContext.message });
		const credentialId = messageContext.message.credentialId;
		if (![require_revocationIdentifier.v2IndyRevocationFormat, require_revocationIdentifier.v2AnonCredsRevocationFormat].includes(messageContext.message.revocationFormat)) throw new __credo_ts_core.CredoError(`Unknown revocation format: ${messageContext.message.revocationFormat}. Supported formats are indy-anoncreds and anoncreds`);
		try {
			const credentialIdGroups = credentialId.match(require_revocationIdentifier.v2IndyRevocationIdentifierRegex) ?? credentialId.match(require_revocationIdentifier.v2AnonCredsRevocationIdentifierRegex);
			if (!credentialIdGroups) throw new __credo_ts_core.CredoError(`Incorrect revocation notification credentialId format: \n${credentialId}\ndoes not match\n"<revocation_registry_id>::<credential_revocation_id>"`);
			const [, anonCredsRevocationRegistryId, anonCredsCredentialRevocationId] = credentialIdGroups;
			const comment = messageContext.message.comment;
			const connection = messageContext.assertReadyConnection();
			await this.processRevocationNotification(messageContext.agentContext, anonCredsRevocationRegistryId, anonCredsCredentialRevocationId, connection, comment);
		} catch (error) {
			this.logger.warn("Failed to process revocation notification message", {
				error,
				credentialId
			});
		}
	}
};
DidCommRevocationNotificationService = require_decorate.__decorate([
	(0, __credo_ts_core.injectable)(),
	require_decorateParam.__decorateParam(2, (0, __credo_ts_core.inject)(__credo_ts_core.InjectionSymbols.Logger)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [
		typeof (_ref = typeof require_DidCommCredentialExchangeRepository.DidCommCredentialExchangeRepository !== "undefined" && require_DidCommCredentialExchangeRepository.DidCommCredentialExchangeRepository) === "function" ? _ref : Object,
		typeof (_ref2 = typeof __credo_ts_core.EventEmitter !== "undefined" && __credo_ts_core.EventEmitter) === "function" ? _ref2 : Object,
		Object
	])
], DidCommRevocationNotificationService);

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