const require_rolldown_runtime = require('../../../../../_virtual/rolldown_runtime.js');
const require_messageType = require('../../util/messageType.js');
const require_DidCommDidExchangeState = require('./models/DidCommDidExchangeState.js');
const require_DidCommDidExchangeRole = require('./models/DidCommDidExchangeRole.js');
require('./models/index.js');
const require_DidCommDidExchangeRequestMessage = require('./messages/DidCommDidExchangeRequestMessage.js');
const require_DidCommDidExchangeResponseMessage = require('./messages/DidCommDidExchangeResponseMessage.js');
const require_DidCommDidExchangeCompleteMessage = require('./messages/DidCommDidExchangeCompleteMessage.js');
require('./messages/index.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/modules/connections/DidExchangeStateMachine.ts
var DidExchangeStateMachine = class DidExchangeStateMachine {
	static assertCreateMessageState(messageType, record) {
		const rule = DidExchangeStateMachine.createMessageStateRules.find((r) => require_messageType.canHandleMessageType(r.message, messageType));
		if (!rule) throw new __credo_ts_core.CredoError(`Could not find create message rule for ${messageType}`);
		if (rule.state !== record.state || rule.role !== record.role) throw new __credo_ts_core.CredoError(`Record with role ${record.role} is in invalid state ${record.state} to create ${messageType}. Expected state for role ${rule.role} is ${rule.state}.`);
	}
	static assertProcessMessageState(messageType, record) {
		const rule = DidExchangeStateMachine.processMessageStateRules.find((r) => require_messageType.canHandleMessageType(r.message, messageType));
		if (!rule) throw new __credo_ts_core.CredoError(`Could not find create message rule for ${messageType}`);
		if (rule.state !== record.state || rule.role !== record.role) throw new __credo_ts_core.CredoError(`Record with role ${record.role} is in invalid state ${record.state} to process ${messageType.messageTypeUri}. Expected state for role ${rule.role} is ${rule.state}.`);
	}
	static nextState(messageType, record) {
		const rule = DidExchangeStateMachine.createMessageStateRules.concat(DidExchangeStateMachine.processMessageStateRules).find((r) => require_messageType.canHandleMessageType(r.message, messageType) && r.role === record.role);
		if (!rule) throw new __credo_ts_core.CredoError(`Could not find create message rule for messageType ${messageType.messageTypeUri}, state ${record.state} and role ${record.role}`);
		return rule.nextState;
	}
};
DidExchangeStateMachine.createMessageStateRules = [
	{
		message: require_DidCommDidExchangeRequestMessage.DidCommDidExchangeRequestMessage,
		state: require_DidCommDidExchangeState.DidCommDidExchangeState.InvitationReceived,
		role: require_DidCommDidExchangeRole.DidCommDidExchangeRole.Requester,
		nextState: require_DidCommDidExchangeState.DidCommDidExchangeState.RequestSent
	},
	{
		message: require_DidCommDidExchangeResponseMessage.DidCommDidExchangeResponseMessage,
		state: require_DidCommDidExchangeState.DidCommDidExchangeState.RequestReceived,
		role: require_DidCommDidExchangeRole.DidCommDidExchangeRole.Responder,
		nextState: require_DidCommDidExchangeState.DidCommDidExchangeState.ResponseSent
	},
	{
		message: require_DidCommDidExchangeCompleteMessage.DidCommDidExchangeCompleteMessage,
		state: require_DidCommDidExchangeState.DidCommDidExchangeState.ResponseReceived,
		role: require_DidCommDidExchangeRole.DidCommDidExchangeRole.Requester,
		nextState: require_DidCommDidExchangeState.DidCommDidExchangeState.Completed
	}
];
DidExchangeStateMachine.processMessageStateRules = [
	{
		message: require_DidCommDidExchangeRequestMessage.DidCommDidExchangeRequestMessage,
		state: require_DidCommDidExchangeState.DidCommDidExchangeState.InvitationSent,
		role: require_DidCommDidExchangeRole.DidCommDidExchangeRole.Responder,
		nextState: require_DidCommDidExchangeState.DidCommDidExchangeState.RequestReceived
	},
	{
		message: require_DidCommDidExchangeResponseMessage.DidCommDidExchangeResponseMessage,
		state: require_DidCommDidExchangeState.DidCommDidExchangeState.RequestSent,
		role: require_DidCommDidExchangeRole.DidCommDidExchangeRole.Requester,
		nextState: require_DidCommDidExchangeState.DidCommDidExchangeState.ResponseReceived
	},
	{
		message: require_DidCommDidExchangeCompleteMessage.DidCommDidExchangeCompleteMessage,
		state: require_DidCommDidExchangeState.DidCommDidExchangeState.ResponseSent,
		role: require_DidCommDidExchangeRole.DidCommDidExchangeRole.Responder,
		nextState: require_DidCommDidExchangeState.DidCommDidExchangeState.Completed
	}
];

//#endregion
exports.DidExchangeStateMachine = DidExchangeStateMachine;