// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
import{get as h,PopStateHandlers as p,setLastLocation as w}from"./utils.js";import{hideSitePane as c}from"./better_ui.js";const s=document.getElementById("main");let d={};const l=[];function a(t,e){if(!t){console.error("No data received");return}if(t.redirect){window.location.href=t.redirect;return}const n=t.url;if(!n){console.error("No URL in data ",t);return}if(console.log("Handling data",t),!e){if(l.length===1&&l[0]===n){console.log("URL is the same as last loaded, ignoring");return}history.pushState({data:t,url:n,stateType:"dynLoad"},t.title,n),w(n)}if(!t.body){window.location.reload();return}if(document.onkeydown=()=>{},s.innerHTML=t.body,t.css){const o=document.createElement("style");o.innerHTML=t.css,s.appendChild(o)}for(const o of t.stylesheets){const i=document.createElement("link");i.rel="stylesheet",i.type="text/css",i.href=o,s.appendChild(i)}for(const o of t.scripts)if(o.src){const i=document.createElement("script");i.type=o.type,i.src=o.src,s.appendChild(i)}else console.error("Script without src",o);window.hideSitePane&&c(),document.title=t.title;const r=document.getElementById("title");return r&&(r.setAttribute("short_title",t.short_title||t.title),r.innerText=t.title),u(),d=t,!0}function u(){for(const t of document.getElementsByTagName("A"))m(t)}function m(t){t.hasAttribute("no-dynload")||y(t)}function y(t){var r;if(t.target==="_blank")return;const e=(t.href.startsWith("/")?window.location.origin+t.href:t.href).trim(),n=e.split("?")[0];!e.startsWith(window.location.origin)||((r=n.split("/").pop())!=null?r:"").includes(".")&&n!==window.location.origin+"/redirect"||n===window.location.origin+"/chat"||e.startsWith("#")||e.startsWith(window.location.href.split("#")[0]+"#")||(t.onclick=o=>(o.preventDefault(),g(e)))}function g(t){return console.log("Loading URL",t),history.replaceState({data:d,url:window.location.href,scrollPos:[document.documentElement.scrollLeft||document.body.scrollLeft,document.documentElement.scrollTop||document.body.scrollTop],stateType:"dynLoad"},document.title,window.location.href),f(t)}async function f(t,e=!1){if(!e&&t===window.location.href){console.log("URL is the same as current, just hide site pane"),window.hideSitePane&&c();return}s.prepend("Laden... Wenn dies zu lange (über ein paar Sekunden) dauert, lade bitte die Seite neu."),await h(t,"",n=>a(n,!1),n=>{console.log(n),t===window.location.href?window.location.reload():window.location.href=t},"application/vnd.asozial.dynload+json")}async function L(t){if(t.state){const e=t.state;if(console.log("Popstate",e),t.state.data&&a(e,!0)||await f(e.url||window.location.href,!0),e.scrollPos){window.scrollTo(e.scrollPos[0],e.scrollPos[1]);return}}console.error("Couldn't handle state.",t.state),window.location.reload()}p.dynLoad=L;u();// @license-end
//# sourceMappingURL=dynload.js.map
