const pe=document.querySelector("#manual-report-form"),g=window.BOOMCAST_I18N||{};function be(){if(!pe)return null;const e=document.querySelector("#burst-start"),t=e.closest(".burst-listener"),n=e.closest(".manual-report__supporting"),i=document.querySelector("#burst-state"),C=document.querySelector("#burst-panel"),y=document.querySelector("#burst-heading"),L=document.querySelector("#burst-detail"),w=document.querySelector("#burst-waveform"),Z=document.querySelector("#burst-preview"),ie=document.querySelector("#burst-resume"),se=document.querySelector("#burst-discard"),Ce=document.querySelector("#burst-stop"),_=document.querySelector("#burst-error"),I=new Float32Array(300);let f=0,G="off",M,d,X,c,S,N,k,v;function $(){const o=w.getContext("2d");o.clearRect(0,0,w.width,w.height),o.fillStyle="#ffbd62";const l=w.width/I.length,m=Math.max(.003,...I);for(let s=0;s{for(let V=0;V{if(l.type==="level"&&(G==="listening"||G==="tail"))I[f]=l.value,f=(f+1)%I.length,$();else if(l.type==="trigger"&&G==="listening")x("tail");else if(l.type==="clip"){const m=new File([Ie(l.samples,l.sampleRate)],Ae(),{type:"audio/wav"});v?(v(m),v=null):G==="tail"&&(ee(),k=m,N=URL.createObjectURL(m),Z.src=N,Z.hidden=!1,x("captured"))}},X.connect(c),c.connect(S).connect(d.destination),await d.resume(),I.fill(0),f=0,$(),x("listening")}catch(l){await P(),t&&(t.dataset.state="error"),n&&(n.dataset.state="error"),i&&(i.textContent=g.microphoneUnavailable||"Microphone unavailable"),_.textContent=l.name==="NotAllowedError"?g.microphoneDenied||"Microphone permission was denied. Manual reporting is still available.":g.microphoneFailed||"Microphone capture failed.",_.hidden=!1}finally{e.disabled=!1}}async function P(){v&&(v(null),v=null),ee(),c==null||c.disconnect(),X==null||X.disconnect(),S==null||S.disconnect(),M==null||M.getTracks().forEach(o=>o.stop()),await(d==null?void 0:d.close()),M=d=X=c=S=null,I.fill(0),f=0,x("off")}function te(){ee(),I.fill(0),f=0,$(),c==null||c.port.postMessage("reset"),c&&x("listening")}return e.addEventListener("click",me),Ce.addEventListener("click",P),se.addEventListener("click",te),ie.addEventListener("click",te),window.addEventListener("pagehide",P),document.addEventListener("visibilitychange",()=>{document.hidden&&P()}),{get active(){return G!=="off"},async clipForSubmission(){return k||(G==="off"||!c?null:new Promise(o=>{v=o,c.port.postMessage("snapshot")}))},submissionSucceeded:te}}const a=window.BOOMCAST_I18N||{},A=document.querySelector("#manual-report-form"),K=document.querySelector("#manual-submit"),F=document.querySelector("#manual-submit-label"),b=document.querySelector("#manual-occurrences"),le=document.querySelector("#manual-decrease"),ge=document.querySelector("#manual-increase"),he=document.querySelector("#manual-latitude"),ye=document.querySelector("#manual-longitude"),O=document.querySelector("#manual-direction-degrees"),we=document.querySelector("#manual-direction-accuracy"),j=()=>document.querySelector("#manual-capture-direction"),u=()=>document.querySelector("#manual-direction-status"),r=()=>document.querySelector("#manual-direction-panel"),H=document.querySelector("#manual-compass"),J=document.querySelector("#manual-compass-home"),z=document.querySelector("#manual-compass-rose"),fe=document.querySelector("#manual-compass-heading"),E=document.querySelector("#manual-report-time"),re=document.querySelector("#report-clock-hours"),ce=document.querySelector("#report-clock-minutes");let B,T,R=null;const Y=be(),ne=e=>Number.isFinite(e),ae=e=>[a.north||"North",a.northeast||"Northeast",a.east||"East",a.southeast||"Southeast",a.south||"South",a.southwest||"Southwest",a.west||"West",a.northwest||"Northwest"][Math.floor((e+22.5)%360/45)],Q=e=>Number(e.toFixed(1)).toString(),ue=()=>{var C,y;if(!E)return;const e=new Date;E.dateTime=e.toISOString();const t=new Intl.DateTimeFormat(document.documentElement.lang||void 0,{hour:"2-digit",minute:"2-digit",hourCycle:"h23"}).formatToParts(e),n=(C=t.find(L=>L.type==="hour"))==null?void 0:C.value,i=(y=t.find(L=>L.type==="minute"))==null?void 0:y.value;!n||!i||!re||!ce||(re.textContent=n,ce.textContent=i,E.setAttribute("aria-label",`${n}:${i}`))};ue();E&&window.setInterval(ue,6e4);const Ge=(F==null?void 0:F.textContent)||"I heard fireworks",h=(e,t="")=>{if(A.dataset.state=e,K.disabled=e==="submitting",F&&(F.textContent=e==="submitting"?a.sendingReport||"Sending…":Ge),t){const n=document.querySelector("#manual-submission");n.className="manual-report__submission",n.textContent=t}},de=(e,t)=>{const n=new DOMParser().parseFromString(e,"text/html").querySelector("#manual-submission");return n?(document.querySelector("#manual-submission").replaceWith(n),h(t?"submitted":"error"),t):(h("error",a.reportFailed||"Could not send the report. Please try again."),!1)},q=e=>{const t=Number.parseInt(e,10);b.value=String(Math.min(1e3,Math.max(1,Number.isFinite(t)?t:1))),le.disabled=b.value==="1",ge.disabled=b.value==="1000"};q(b.value);le.addEventListener("click",()=>q(Number(b.value)-1));ge.addEventListener("click",()=>q(Number(b.value)+1));b.addEventListener("change",()=>q(b.value));const U=()=>{B&&window.removeEventListener("deviceorientation",B),window.clearTimeout(T),B=null,T=null,H.hidden=!0,R=null},Se=e=>{const t=-e;if(R===null)R=t,z.style.transition="none",z.style.transform=`rotate(${R}deg)`,z.offsetWidth,z.style.transition="";else{const n=(t-R+540)%360-180;R+=n,z.style.transform=`rotate(${R}deg)`}fe.textContent=`${Q(e)}° · ${ae(e)}`,H.setAttribute("aria-label",`${a.compassDirection||"Compass direction"} ${Q(e)}° ${ae(e)}`),H.hidden=!1},ve=(e,t)=>{const n=u();if(n)if(n.classList.toggle("manual-direction-panel__status--accuracy",t!==null),t===null)n.textContent=a.accuracyUnavailable||"Accuracy unavailable";else{const C=document.createElement("span");C.textContent=a.accuracy||"Accuracy";const y=document.createElement("strong");y.textContent=`±${Q(t)}°`,n.replaceChildren(C,y)}const i=j();i&&(i.textContent=a.stopCapture||"Stop measuring direction")},Re=()=>{u()&&(U(),r()&&(r().dataset.state="measuring"),u().textContent=a.pointPhone||"Point the top of your phone toward the sound — direction will update live.",B=e=>{const t="webkitCompassHeading"in e,n=ne(e.webkitCompassHeading)?e.webkitCompassHeading:!t&&e.absolute&&ne(e.alpha)?(360-e.alpha)%360:null;if(n===null||n<0||n>=360)return;const i=ne(e.webkitCompassAccuracy)&&e.webkitCompassAccuracy>=0?e.webkitCompassAccuracy:null;O.value=n,we.value=i===null?"":i,ve(n,i),Se(n),window.clearTimeout(T)},window.addEventListener("deviceorientation",B),T=window.setTimeout(()=>{U();const e=j(),t=u();e&&(e.textContent=a.captureDirection||"Start measuring direction"),t&&(t.textContent=a.compassUnavailable||"Compass unavailable on this device."),r()&&(r().dataset.state="unavailable")},15e3))},Ze=async()=>{var e;if(B){U(),j().textContent=a.captureAgain||"Measure again",r()&&(r().dataset.state=O.value?"captured":"ready");return}if((e=u())==null||e.classList.remove("manual-direction-panel__status--accuracy"),!window.isSecureContext){u().textContent=a.secureCompass||"Compass capture requires a secure HTTPS connection.",r()&&(r().dataset.state="unavailable");return}if(typeof window.DeviceOrientationEvent>"u"){u().textContent=a.compassUnavailable||"Compass unavailable on this device.",r()&&(r().dataset.state="unavailable");return}if(typeof DeviceOrientationEvent.requestPermission=="function"){u().textContent=a.compassPermission||"Requesting compass permission…";try{if(await DeviceOrientationEvent.requestPermission(!0)!=="granted"){u()&&(u().textContent=a.compassDenied||"Compass permission denied."),r()&&(r().dataset.state="unavailable");return}}catch{u()&&(u().textContent=a.compassDenied||"Compass permission denied."),r()&&(r().dataset.state="unavailable");return}}Re()},oe=async()=>{if(q(b.value),U(),h("submitting",a.sendingReport||"Sending…"),Y!=null&&Y.active){try{const e=await Y.clipForSubmission(),t=new FormData(A);e&&t.append("audio",e);const n=await fetch(A.action,{method:"POST",body:t,credentials:"same-origin"}),i=await n.text();de(i,n.ok)&&Y.submissionSucceeded()}catch{h("error",a.reportFailed||"Could not send the report. Please try again.")}return}if(window.htmx)try{htmx.ajax("POST",A.action,{source:A,target:"#manual-submission",swap:"outerHTML"})}catch{h("error",a.reportFailed||"Could not send the report. Please try again.")}else A.submit()};A.addEventListener("click",e=>{e.target.closest("#manual-capture-direction")&&Ze(),e.target.closest(".manual-direction-panel__close")&&(U(),J&&J.append(H))});document.body.addEventListener("htmx:beforeSwap",e=>{var t;((t=e.detail.target)==null?void 0:t.id)==="manual-direction-panel"&&J&&J.append(H)});document.body.addEventListener("htmx:afterSwap",e=>{var n,i;if(((n=e.detail.target)==null?void 0:n.id)!=="manual-direction-panel")return;j()&&((i=r().querySelector(".manual-direction-panel__copy"))==null||i.after(H));const t=document.querySelector("#manual-direction-summary");if(t&&O.value!==""){const C=Number(O.value);t.textContent=`${ae(C)} · ${Q(C)}°`,r().dataset.state="captured"}});K.addEventListener("click",()=>{if(!K.disabled){if(h("submitting",a.requestingLocation||"Requesting your optional location…"),!navigator.geolocation){oe();return}navigator.geolocation.getCurrentPosition(e=>{he.value=e.coords.latitude,ye.value=e.coords.longitude,oe()},()=>oe(),{enableHighAccuracy:!1,maximumAge:3e5,timeout:1e4})}});A.addEventListener("submit",e=>{e.preventDefault(),K.disabled||K.click()});document.body.addEventListener("htmx:afterRequest",e=>{if(e.detail.elt!==A)return;const t=e.detail.xhr;t?t.status>=400?de(t.responseText,!1):h("submitted"):h("error",a.reportFailed||"Could not send the report. Please try again.")});document.body.addEventListener("htmx:sendError",e=>{e.detail.elt===A&&h("error",a.reportFailed||"Could not send the report. Please try again.")});