import { __decorateMetadata } from "../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateMetadata.mjs";
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.mjs";
import { DID_COMM_TRANSPORT_QUEUE } from "./constants.mjs";
import { DidCommTransportEventTypes } from "./transport/DidCommTransportEventTypes.mjs";
import "./transport/index.mjs";
import { AgentContext, CredoError, EventEmitter, injectable } from "@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: 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 CredoError(`Session not found with id ${sessionId}`);
		session.connectionId = connectionId;
		this.saveSession(session);
	}
	hasInboundEndpoint(didDocument) {
		return Boolean(didDocument.didCommServices?.find((s) => s.serviceEndpoint !== DID_COMM_TRANSPORT_QUEUE));
	}
	findSessionById(sessionId) {
		return this.transportSessionTable[sessionId];
	}
	removeSession(session) {
		delete this.transportSessionTable[session.id];
		this.eventEmitter.emit(this.agentContext, {
			type: DidCommTransportEventTypes.DidCommTransportSessionRemoved,
			payload: { session }
		});
	}
	getExistingSessionsForConnectionIdAndType(connectionId, type) {
		return Object.values(this.transportSessionTable).filter((session) => session?.connectionId === connectionId && session.type === type);
	}
};
DidCommTransportService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [typeof (_ref = typeof AgentContext !== "undefined" && AgentContext) === "function" ? _ref : Object, typeof (_ref2 = typeof EventEmitter !== "undefined" && EventEmitter) === "function" ? _ref2 : Object])], DidCommTransportService);

//#endregion
export { DidCommTransportService };
//# sourceMappingURL=DidCommTransportService.mjs.map