(() => { 'use strict'; const host=document.querySelector('[data-geography-location]'); if(!host)return; const projectId=host.dataset.projectId,locationId=host.dataset.geographyLocation; const status=host.querySelector('[data-geography-status]'),body=host.querySelector('[data-geography-body]'); const token=document.querySelector('input[name="__RequestVerificationToken"]')?.value; async function change(action,data){ const response=await fetch('/Geography/'+action,{method:'POST',headers:{RequestVerificationToken:token},body:new URLSearchParams({projectId,locationId,...data})}); const result=await response.json().catch(()=>null); if(!response.ok||response.redirected)throw Error(result?.message||'Could not update geographical match.'); await load(); if(result?.message){const p=document.createElement('p');p.setAttribute('role','status');p.textContent=result.message;body.prepend(p);} } function button(label,action){const b=document.createElement('button');b.type='button';b.className='btn btn-outline-secondary btn-sm';b.textContent=label;b.onclick=async()=>{b.disabled=true;try{await action();}catch(e){status.textContent=e.message;b.disabled=false;}};body.append(b);} function kind(m){return m.PlaceID?.startsWith('node:')?'Point location':/^(way|relation):/.test(m.PlaceID||'')?'Area location':'';} function line(text){const p=document.createElement('p');p.textContent=text;body.append(p);} async function load(){ const response=await fetch('/Geography/State?'+new URLSearchParams({projectId,locationId}),{cache:'no-store'}); if(!response.ok||response.redirected)throw Error('Geographical status unavailable.'); const rows=await response.json(),state=rows[0]; status.textContent=state?.status==='Pending'?({'Awaiting hierarchy request':'Not requested','Queued':state.attempts?'Retry scheduled':'Queued','Processing':'Looking up','Waiting configuration':'Lookup disabled'}[state.jobState]||'Pending'):(state?.status||'Not requested');body.replaceChildren(); if(!state){line('Awaiting geographical classification.');return;} if(state.message)line(state.message); if(!state.isExcluded)button('Resolve this arranged hierarchy (up to 20 places)',()=>change('Hierarchy',{})); button(state.isExcluded?'Enable automatic geographical lookup':'Exclude automatic geographical lookup',()=>change('Exclude',{generation:state.generation,isExcluded:!state.isExcluded})); if(state.matchJson){const m=JSON.parse(state.matchJson);line(`${m.Name}${kind(m)?' · '+kind(m):''} · ${m.AdministrativeNames.join(', ')} · ${m.CountryCode}`);line(`${m.Latitude}, ${m.Longitude}`);line(m.Attribution);} if(state.status==='Needs confirmation'){ const candidates=JSON.parse(state.candidatesJson||'[]'); candidates.forEach((m,index)=>button(`${m.Name}${kind(m)?' · '+kind(m):''} · ${m.AdministrativeNames.join(', ')} · ${m.CountryCode} (${m.Latitude}, ${m.Longitude}) — ${m.Attribution}`,()=>change('Confirm',{generation:state.generation,candidateIndex:index}))); } if(state.status==='Resolved')button('Change geographical match',()=>change('Retry',{generation:state.generation,replaceConfirmed:true})); if(['Unresolved','Needs confirmation'].includes(state.status))button('Retry geographical lookup',()=>change('Retry',{generation:state.generation})); } load().catch(e=>status.textContent=e.message); })();