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_constants = require('./constants.js');
const require_DidCommTransportEventTypes = require('./transport/DidCommTransportEventTypes.js');
require('./transport/index.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/DidCommTransportService.ts
var _ref, _ref2;
let DidCommTransportService = class DidCommTransportService$1 {
	constructor(agentContext, eventEmitter) {
		this.transportSessionTable = {};
		this.agentContext = agentContext;
		this.eventEmitter = eventEmitter;
	}
	saveSession(session) {
		if (session.connectionId) {
			const oldSessions = this.getExistingSessionsForConnectionIdAndType(session.connectionId, session.type);
			for (const oldSession of oldSessions) if (oldSession && oldSession.id !== session.id) this.removeSession(oldSession);
		}
		this.transportSessionTable[session.id] = session;
		this.eventEmitter.emit(this.agentContext, {
			type: require_DidCommTransportEventTypes.DidCommTransportEventTypes.DidCommTransportSessionSaved,
			payload: { session }
		});
	}
	findSessionByConnectionId(connectionId) {
		return Object.values(this.transportSessionTable).find((session) => session?.connectionId === connectionId);
	}
	setConnectionIdForSession(sessionId, connectionId) {
		const session = this.findSessionById(sessionId);
		if (!session) throw new __credo_ts_core.CredoError(`Session not found with id ${sessionId}`);
		session.connectionId = connectionId;
		this.saveSession(session);
	}
	hasInboundEndpoint(didDocument) {
		return Boolean(didDocument.didCommServices?.find((s) => s.serviceEndpoint !== require_constants.DID_COMM_TRANSPORT_QUEUE));
	}
	findSessionById(sessionId) {
		return this.transportSessionTable[sessionId];
	}
	removeSession(session) {
		delete this.transportSessionTable[session.id];
		this.eventEmitter.emit(this.agentContext, {
			type: require_DidCommTransportEventTypes.DidCommTransportEventTypes.DidCommTransportSessionRemoved,
			payload: { session }
		});
	}
	getExistingSessionsForConnectionIdAndType(connectionId, type) {
		return Object.values(this.transportSessionTable).filter((session) => session?.connectionId === connectionId && session.type === type);
	}
};
DidCommTransportService = require_decorate.__decorate([(0, __credo_ts_core.injectable)(), require_decorateMetadata.__decorateMetadata("design:paramtypes", [typeof (_ref = typeof __credo_ts_core.AgentContext !== "undefined" && __credo_ts_core.AgentContext) === "function" ? _ref : Object, typeof (_ref2 = typeof __credo_ts_core.EventEmitter !== "undefined" && __credo_ts_core.EventEmitter) === "function" ? _ref2 : Object])], DidCommTransportService);

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