:: event[event]
	<<set _temporaryEvents = {}>>
	<<set _eventReturnPassageTags = []>>
	<<set _eventPassages = Story.lookup("tags", $eventReturnLocation.passage+'_events')>>
	<<for _eventPassage range _eventPassages>>
		<<gs _eventPassage.title>>
	<</for>>

	<<set _eventsRegisteredForPassageAndTags = $events.registeredByPassage($eventReturnLocation.passage)>>

	<<set _targetPassageTags = $eventReturnLocation.tags>>
	<<for _targetPassageTag range _targetPassageTags>>
		<<set _eventsRegisteredForPassageAndTags = _eventsRegisteredForPassageAndTags.concat($events.registeredByTag(_targetPassageTag))>>
		<<set _eventPassages = Story.lookup("tags", _targetPassageTag+'_events')>>
		<<for _eventPassage range _eventPassages>>
			<<gs _eventPassage.title>>
		<</for>>
	<</for>>

	<<set _allEvents = setup.mergeDeep({},_eventsRegisteredForPassageAndTags,_temporaryEvents)>>
	<<include 'eventsExecute'>>

    <<if !_eventStopsRendering>>
		<<log `'No event, executing default passage: '+$eventReturnLocation.passage`>>
        <<set _thisMainPassage = $eventReturnLocation.passage>>
        <<set $here = $eventReturnLocation.passage>>
        <<set _tags = _eventReturnPassageTags.concat($eventReturnLocation.tags)>>
		<<set $location_var[$here] = $eventReturnLocation.passageArguments>>
        <<include $eventReturnLocation.passage>>
	<</if>>


:: event_widget[widget]
<<widget 'event' container>>
	<<set _eventPriority = _args[0] ?? 0>>
	<<set _eventWeight = _args[1] ?? 1>>

	<<set _temporaryEvents[_eventPriority] ??= []>>

	<<run _temporaryEvents[_eventPriority].push({priority: _eventPriority, weight: _eventWeight,  contents: _contents})>>
<</widget>>

<<widget 'eventExecute'>>

	<<set _eventTime = {requested:_args[1] ?? 0,used:0}>>

	<<set _eventStopsRendering = false>>
	<<set _temporaryEvents = {}>>

	<<set _eventPassages = Story.lookup("tags", _args[0]+'_events')>>
	<<for _eventPassage range _eventPassages>>
		<<gs _eventPassage.title>>
	<</for>>
	<<set _allEvents = _temporaryEvents>>
	<<include 'eventsExecute'>>
<</widget>>

<<widget 'eventFired'>>
	<<set _eventStopsRendering = true>>
<</widget>>

<<widget 'tagAdd'>>
	<<run _tags.push(_args[0])>>
	<<if Array.isArray(_eventReturnPassageTags)>>
		<<run _eventReturnPassageTags.push(_args[0])>>
	<</if>>
<</widget>>

:: eventsExecute[include]
<!--Shared code. Internal use only! Only call with include, not gs. -->
<<set _eventExecuteResult = setup.eventsExecute(_allEvents)>>
<<=_eventExecuteResult.contents>>
<<set _eventStopsRendering = (_eventExecuteResult.exitCode == 1)>>


:: eventsExeceuteDeprecated[include deprecated]
<<for _eventPriority range Object.keys(_allEvents).sort((a,b)=>b-a)>>
	<<set _eventsByPriority = _allEvents[_eventPriority] || []>>
	<<for _event = _eventsByPriority.pluckRandomWeighted()>>
		<<if 'passage' in _event>>
			<<gs _event.passage>>
		<<elseif 'contents' in _event>>
			_event.contents
		<</if>>
		<<if _eventStopsRendering>>
			<<break>>
		<</if>>
	<</for>>
	<<if _eventStopsRendering>>
		<<break>>
	<</if>>
<</for>>
