(function(root,factory){const api=factory();if(typeof module==='object'&&module.exports)module.exports=api;else root.PlotDirectorMapStory=api;})(globalThis,function(){ 'use strict'; const mapped=p=>!!p?.featureID; function filterScenes(data,state){return data.scenes.filter(s=>(!state.bookID||s.bookID===state.bookID) &&(state.lens!=='chapter'||!state.chapterID||s.chapterID===state.chapterID) &&(state.lens!=='plot'||!state.plotIDs.length||s.plotLineIDs.some(id=>state.plotIDs.includes(id))) &&(state.mapping==='all'||state.mapping==='mapped'&&mapped(s.position)||state.mapping==='unmapped'&&s.position.status==='Unmapped' ||state.mapping==='missing'&&s.position.status==='No Location'||state.mapping==='elsewhere'&&!mapped(s.position)&&!!s.position.detailMapID) &&(state.planning==='all'||state.planning==='planning'&&mapped(s.position)&&s.position.isProvisional||state.planning==='established'&&mapped(s.position)&&!s.position.isProvisional));} function journeys(data,characterIDs,scenes){const allowed=new Set(scenes.map(s=>s.sceneID));return data.stops.filter(s=>characterIDs.includes(s.characterID)&&allowed.has(s.sceneID));} function timeEvents(data,bookID){const byId=new Map(data.scenes.map(s=>[s.sceneID,s])),events=new Map(data.scenes.filter(s=>s.timePrecision&&(!bookID||s.bookID===bookID)).map(s=>[s.startDateTime+':'+s.sceneID,s]));for(const stop of data.stops){const scene=byId.get(stop.sceneID);if(stop.observedAt&&stop.timePrecision&&scene&&(!bookID||scene.bookID===bookID))events.set(stop.observedAt+':'+scene.sceneID,{...scene,startDateTime:stop.observedAt,timePrecision:stop.timePrecision});}return [...events.values()].sort((a,b)=>a.startDateTime.localeCompare(b.startDateTime)||a.narrativeOrder-b.narrativeOrder);} // Story dates are author wall-clock values, not browser time zones. Never interpolate between events. function timePositions(data,at,characterIDs=[],bookID=0){ const scenes=new Map(data.scenes.map(s=>[s.sceneID,s])),day=at?.slice(0,10); return data.characters.filter(c=>!characterIDs.length||characterIDs.includes(c.characterID)).map(character=>{ const candidates=data.stops.filter(s=>s.characterID===character.characterID).map(stop=>{const source=scenes.get(stop.sceneID);return {stop,scene:source&&stop.observedAt!==undefined?{...source,startDateTime:stop.observedAt,timePrecision:stop.timePrecision}:source};}) .filter(x=>x.scene?.timePrecision&&x.scene.startDateTime&&(!bookID||x.scene.bookID===bookID) &&(x.scene.timePrecision==='Day'?x.scene.startDateTime.slice(0,10)<=day:x.scene.startDateTime<=at)); if(!candidates.length)return {character,position:null,reason:'Unknown: no confirmed dated appearance at or before this point.'}; candidates.sort((a,b)=>a.scene.startDateTime.localeCompare(b.scene.startDateTime)||a.scene.narrativeOrder-b.scene.narrativeOrder); const latest=candidates.at(-1),latestDay=latest.scene.startDateTime.slice(0,10); const sameDay=candidates.filter(x=>x.scene.startDateTime.slice(0,10)===latestDay); const dateOnly=sameDay.filter(x=>x.scene.timePrecision==='Day'); if(dateOnly.length&&(latestDay===day||new Set(sameDay.map(x=>x.stop.position.locationID??'unknown')).size>1)) return {character,position:null,scene:latest.scene,reason:'Unknown within this date: date-only appearances do not establish a precise position.'}; const latestTime=candidates.filter(x=>x.scene.startDateTime===latest.scene.startDateTime); if(new Set(latestTime.map(x=>x.stop.position.locationID??'unknown')).size>1) return {character,position:null,scene:latest.scene,reason:'Unknown: simultaneous appearances have different or unknown Locations.'}; if(!latest.stop.position.locationID)return {character,position:null,scene:latest.scene,reason:'Unknown: the latest confirmed appearance has no known Location.'}; return {character,position:latest.stop.position,scene:latest.scene,reason:'Last known Location; position between scenes is not established.'}; }); } function density(scenes){const counts=new Map();for(const s of scenes){const id=s.position.featureID;if(!id)continue;if(!counts.has(id))counts.set(id,[]);counts.get(id).push(s);}return counts;} return {mapped,filterScenes,journeys,timeEvents,timePositions,density}; });