const enum EEventsExecuteExitCode{
	STOPPED_BY_EVENT = 1,
	NOT_STOPPED = 2
}

setup.eventsExecute = function(events:PrioritizedEventDict){
	var result = document.createElement("div");
	State.temporary.actions ??= [];
	const existingActionsLength = State.temporary.actions.length;

	for(const _eventPriority of Object.keys(events).sort((a,b)=>Number(b)-Number(a))){
		const _eventsByPriority = events[_eventPriority] || [];

		let event = _eventsByPriority.pluckRandomWeighted();
		while(event){
			if('passage' in event)
				$(result).wiki(`<<gs '${event.passage}'>>`);
			else if('contents' in event)
				$(result).wiki(event.contents);

			if(State.temporary.eventStopsRendering){
				const newActions = State.temporary.actions.slice(existingActionsLength);
				return {exitCode:EEventsExecuteExitCode.STOPPED_BY_EVENT,contents:result.innerHTML,actions:newActions};
			}
			event = _eventsByPriority.pluckRandomWeighted();
		}
	}

	const newActions = State.temporary.actions.slice(existingActionsLength);
	return {exitCode:EEventsExecuteExitCode.NOT_STOPPED,contents:result.innerHTML,actions:newActions};
}