:: NPCs_script[script]
	setup.npcSettings ??= {}

	setup.npcSettings.relationshipMargins = [20,40,60,80]

:: NPCFunctions[include]
<<switch $location_var[$here][0]>>
	<<case 'relationshipDescription'>>
		<<set $result = ''>>

		<<set _npcFam = $npc($location_var[$here][1]).fam>>
		/*<<if _npcFam < 5>>
			<<set $result += "You don't know each other.">>
		<<elseif _npcFam < 20>>
			<<set $result += "You barely know each other.">>
		<<elseif _npcFam < 100>>
			<<set $result += "You know little about each other.">>
		<<elseif _npcFam < 300>>
			<<set $result += "You are acquainted with each other.">>
		<<elseif _npcFam < 500>>
			<<set $result += "You somewhat know each other.">>
		<<elseif _npcFam < 700>>
			<<set $result += "You are familiar with each other.">>
		<<elseif _npcFam < 800>>
			<<set $result += "You are very familiar with each other.">>
		<<elseif _npcFam < 900>>
			<<set $result += "You are extremely familiar with each other.">>
		<<else>>
			<<set $result += "You are extremely familiar with each other.">>
		<</if>>*/

		<<if _npcFam < 5>>
			<<set $result += "You don't know each other.">>
		<<elseif _npcFam < 20>>
			<<set $result += "You barely know each other.">>
		<<elseif _npcFam < 200>>
			<<set $result += "You know each other as well as colleagues or class mates do.">>
		<<elseif _npcFam < 400>>
			<<set $result += "You know each other as well as friends do.">>
		<<elseif _npcFam < 600>>
			<<set $result += "You know each other as well as close friends do.">>
		<<elseif _npcFam < 800>>
			<<set $result += "You know each other as well as best friends do.">>
		<<elseif _npcFam < 900>>
			<<set $result += "You know each other as well as close family members do.">>
		<<else>>
			<<set $result += "You know each other as well as soul mates do.">>
		<</if>>

		<<set $result += ' '>>

		<<set _npcRel = $npc($location_var[$here][1]).rel>>
		<<if _npcRel < setup.npcSettings.relationshipMargins[0]>>
			<<set $result += 'Your relationship is awful.'>>
		<<elseif _npcRel < setup.npcSettings.relationshipMargins[1]>>
			<<set $result += 'Your relationship is tense.'>>
		<<elseif _npcRel < setup.npcSettings.relationshipMargins[2]>>
			<<set $result += 'You have a normal relationship.'>>
		<<elseif _npcRel < setup.npcSettings.relationshipMargins[3]>>
			<<set $result += 'You have a good relationship.'>>
		<<else>>
			<<set $result += 'You have a great relationship.'>>
		<</if>>
<</switch>>

:: NPC_Widgets[widget]

<<widget 'npc' container>>

	<<if typeof _args[0] == "object">>
		<<set _npcId = _args[0].id>>
	<<else>>
		<<set _npcId = _args[0] ?? _autoCapture.loadedNPCIds.last()>>
	<</if>>
	<!-- Present NPCs -->
	<<set _presentNPCs ??= {}>>
	<<set _presentNPCs[_npcId] ||= _contents>>

	<<if _args[2]>>
		<<if ['firstname','lastname','fullname','usedname','nickname','titlename'].includes(_args[2])>>
			<<set _displayedName = $npcs.get(_npcId,_args[2])>>
		<<else>>
			<<set _displayedName = _args[2]>>
		<</if>>
	<<else>>
		<<set _displayedName = $npc(_npcId).usedname>>
	<</if>>



	<<set _flags = _args[1] ?? {}>>
	<<set _c to _contents>>


	<<capture _c, _npcId, _flags>>
		<<set _note = $npc(_npcId).notes ?? ''>>

		<<set _image = $npc(_npcId).image>>
		<<if _image>>
			<<set _note = "<<image '"+_image+"'>>" + _note>>
		<</if>>

		<<set _note += '<hr/>'+setup.func('NPCFunctions','relationshipDescription',_npcId)>>

		<<if _note>>
			<<linkWithTooltip _displayedName _note>>
				<<npcInfoContainerAdd _npcId _flags>>
			<</linkWithTooltip>>
		<<else>>
			<!-- This should never happen -->
			<<link _displayedName>>
				<<npcInfoContainerAdd _npcId _flags>>
			<</link>>
		<</if>>
	<</capture>>
<</widget>>

<<widget 'npcInfoContainerAdd'>>
	<<set _npcIC_npc = _args[0]>>
	<<set _npcIC_flags = _args[1] ?? {}>>

	<<append "#passages">>
		<div id="npcInfoContainer">
			<div id="npcInfo">
				
				<h2><<=$npc(_npcIC_npc).usedname>> (<<=$npc(_npcIC_npc).fullname>>)</h2>

				
				<<set _image = $npc(_npcIC_npc).image>>
				<<if _image>>
					<<image _image>>
				<</if>>



				<div class="long">
					<<=$npc(_npcIC_npc).notes>>

					<h3>Relationship</h3><<=func('NPCFunctions','relationshipDescription',_npcIC_npc)>>

					<h3>Preferences</h3>
					<ul>
						<<set _commentedPreferences = $npcs.prerencesKnown(_npcIC_npc)>>
						<<for _preferenceKey,_preferenceMagnitude range _commentedPreferences>>
							<<set _prefDesc = setup.npcPreferences[_preferenceKey]?.d ?? 'Description missing'>>
							<li>
								_prefDesc: _preferenceMagnitude
							</li>
						<</for>>
					</ul>
				</div>

				<div id="npcActions">
					<<if _tags.includes('indoors') or _tags.includes('outdoors')>>
						<<set _npcIC_actions = Object.assign({},$npc(_npcIC_npc).interactions,_npcIC_flags.interactions ?? {})>>
						<<for _label, _targetPassage range _npcIC_actions>>
							<<capture _targetPassage>>
								<div class="action"><<link _label>><<gt _targetPassage _npcIC_npc>><</link>></div>	
							<</capture>>
						<</for>>
					<</if>>

					<div class="action"><<link "Close">><<npcInfoContainerRemove>><</link>></div>

					<div class="action"><<link "About NPCs">>
						<<replace '#npcInfo'>>
							<h2>Npcs</h2>
							<div id="npcsAbout"><<include 'npcsAbout'>></div>
							<div id="npcActions"><div class="action"><<link "Close">><<npcInfoContainerRemove>><</link>></div></div>
						<</replace>>
					<</link>></div>
				</div>

			</div>
		</div>
	<</append>>
	<<timed 1s>>
		<<append "#npcInfoContainer">>
			<<link "">>
				<<npcInfoContainerRemove>>
			<</link>>
		<</append>>
	<</timed>>
<</widget>>

<<widget 'npcInfoContainerRemove'>>
	<<remove "#npcInfoContainer">>
<</widget>>



<<widget 'npcList'>>
	<<if ['array','object'].includes(typeof _args[0])>>
		<<set _npcNames = []>>
		<<for _key, _npcId range _args[0]>>
			<<if typeof _npcId == 'object'>>
				<<run _npcNames.push("<<npc '"+_key+"'>>"+(_npcId.contents ?? '')+"<</npc>>")>>
			<<else>>
				<<run _npcNames.push("<<npc '"+_npcId+"'>><</npc>>")>>
			<</if>>
		<</for>>
		<<if _args[2] and _npcNames.length > 1>>
			<<=_args[2].split('#')[0]>>
		<<elseif _args[1] and _npcNames.length > 0>>
			<<=_args[1].split('#')[0]>>
		<</if>>
		<<concatAnd _npcNames>>
		<<if _args[2] and _npcNames.length > 1>>
			<<=_args[2].split('#')[1]>>
		<<elseif _args[1] and _npcNames.length > 0>>
			<<=_args[1].split('#')[1]>>
		<</if>>
	<</if>>
<</widget>>

<<widget "npcLoad" container>>
	<<set _autoCapture.loadedNPCIds ??= []>>
	<<run _autoCapture.loadedNPCIds.push(_args[0])>>
	<!--<<set _loadedNPCId = _autoCapture.loadedNPCIds.last()>>-->

	_contents

	<<run _autoCapture.loadedNPCIds.pop()>>
	<!--<<if _autoCapture.loadedNPCIds.length > 0>>
		<<set _loadedNPCId = _autoCapture.loadedNPCIds.last()>>
	<<else>>
		<<set _loadedNPCId = undefined>>
	<</if>>-->
<</widget>>

<<widget 'npcPronoun'>>
	<<if Array.isArray(_args[0])>>
		<<if _args[0].length == 1>>
			<<set _npcGender = $npc(_args[0][0]).gender>>
		<<else>>
			<<set _npcGender = -1>>
		<</if>>
	<<elseif typeof _args[0] == 'object'>>
		<<if Object.keys(_args[0]).length == 1>>
			<<set _npcGender = $npcs.get(Object.keys(_args[0])[0]).gender>>
		<<else>>
			<<set _npcGender = -1>>
		<</if>>
	<<else>>
		<<set _npcGender = $npcs.get(_args[0]).gender>>
	<</if>>
	<<switch _npcGender>>
		<<case -1>>
			<<set _pronouns = {
				'himherthem': 'them',
				'HimHerThem': 'Them',
			}>>
		<<case 0>>
			<<set _pronouns = {
				'himherthem': 'him',
				'HimHerThem': 'Him',
			}>>
		<<case 1>>
			<<set _pronouns = {
				'himherthem': 'her',
				'HimHerThem': 'Her',
			}>>
	<</switch>>
	<<=_pronouns[_args[1]]>>

<</widget>>

<<widget 'npcRemember'>>

<</widget>>

<<widget 'g'>>
	<!--<<=$npcs.pronoun(_loadedNPCId,_args[0])>>-->
	<<=$npcs.pronoun(_autoCapture?.loadedNPCIds?.last(),_args[0])>>
<</widget>>

<<widget 'he'>>
	<<genderedWord `['he','she']` _args[0]>>
<</widget>>

<<widget 'He'>>
	<<genderedWord `['He','She']` _args[0]>>
<</widget>>

<<widget 'genderedWord'>>
	<<if _args[1]>>
		<<set _npcId = _args[1]>>
	<<elseif _autoCapture.loadedNPCIds>>
		<<set _npcId = _autoCapture.loadedNPCIds.last()>>
	<</if>>
	<<set _npcGender = $npc(_npcId).gender>>
	<<switch _npcGender>>
		<<case 0>>
			<<=_args[0][0]>>
		<<case 1>>
			<<=_args[0][1]>>
		<<default>>
			<<=`'Key not found: '+_npcGender+' in <<genderedWord>>'`>>
	<</switch>>
<</widget>>