const t=new class{constructor(){this.page=null,this.userInfo={id:null},this.parseCurrentPageFromUrl(),window.addEventListener("popstate",t=>{this.updateState()})}parseCurrentPageFromUrl(){const t=new URLSearchParams(window.location.search),e=Object.fromEntries(t.entries());this.page=e.pg??null,this.projectId=e.projectId??null,this.userId=e.userId??null}async loadCurrentUser(){this.userInfo=await async function(){try{const t=await fetch("/api/current_user",{method:"GET",headers:{"Content-Type":"application/json"}});if(!t.ok){const e=await t.json();if(e?.detail)throw new Error(e?.detail);throw new Error(`HTTP error! status: ${t.status}, message: ${e?.detail||"Unknown error"}`)}return await t.json()}catch(t){return{id:null}}}(),this.updateStateForRequest()}urlClick(t){const e=`/UI/?${t}`;window.history.pushState({},"",e),this.clearPageContent(),this.updateState()}async login(){const t=document.getElementById("login_email").value,e=document.getElementById("login_password").value,a=document.getElementById("loginResponseMessage");a.innerHTML="";try{await async function(t,e){try{const a=new URLSearchParams;a.append("username",t),a.append("password",e);const s=await fetch("/api/login",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:a.toString()});if(!s.ok){const t=await s.json();if(t?.detail)throw new Error(t?.detail);throw new Error(`HTTP error! status: ${s.status}, message: ${t?.detail||"Unknown error"}`)}return await s.json()}catch(t){throw t}}(t,e);this.updateState()}catch(t){t.response&&t.response.data&&t.response.data.detail?a.innerHTML=t.response.data.detail:a.innerHTML=t||"An error occurred during login.",a.style.color="red"}}async logout(){await async function(){try{const t=await fetch("/api/logout",{method:"POST",headers:{"Content-Type":"application/json"}});if(!t.ok){const e=await t.json();if(e?.detail)throw new Error(e?.detail);throw new Error(`HTTP error! status: ${t.status}, message: ${e?.detail||"Unknown error"}`)}return await t.json()}catch(t){throw t}}(),this.updateState()}async createNewProject(){await async function(){try{const t=await fetch("/api/projects",{method:"POST",headers:{"Content-Type":"application/json"}});if(!t.ok){const e=await t.json();if(e?.detail)throw new Error(e?.detail);throw new Error(`HTTP error! status: ${t.status}, message: ${e?.detail||"Unknown error"}`)}return await t.json()}catch(t){throw t}}(),this.updateState()}async updateUserPassword(t,e,a){const s=document.getElementById("updateUserPasswordMessage");s.innerHTML="",e===a?e.length<6?s.innerHTML="Password must be at least 6 characters long.":await this.updateUser(t,{password:e}):s.innerHTML="Passwords do not match."}async updateUser(t,e){await async function(t,e){try{const a=await fetch(`/api/users/${t}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!a.ok){const t=await a.json();if(t?.detail)throw new Error(t?.detail);throw new Error(`HTTP error! status: ${a.status}, message: ${t?.detail||"Unknown error"}`)}return await a.json()}catch(t){throw t}}(t,e),this.updateState()}async updateProject(t,e){await async function(t,e){try{const a=await fetch(`/api/projects/${t}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!a.ok){const t=await a.json();if(t?.detail)throw new Error(t?.detail);throw new Error(`HTTP error! status: ${a.status}, message: ${t?.detail||"Unknown error"}`)}return await a.json()}catch(t){throw t}}(t,e),this.updateState()}async deleteIfcFile(t){await async function(t){try{const e=await fetch(`/api/ifc_file/${t}`,{method:"DELETE",headers:{}});if(!e.ok){const t=await e.json();throw new Error(`HTTP error! status: ${e.status}, message: ${t?.detail||"Failed to delete file"}`)}return!0}catch(t){throw t}}(t),this.updateState()}async uploadIfcFile(t){await async function(t){const e=document.createElement("input");e.type="file",e.accept=".ifc",e.onchange=async e=>{const a=e.target.files[0];if(!a)return;if(!a.name.endsWith(".ifc"))return void alert("Only .ifc files are allowed.");const s=new FormData;s.append("file",a);try{const e=await fetch(`/api/ifc_file/${t}`,{method:"POST",headers:{},body:s});if(e.ok)await e.json(),app.updateState();else{const t=await e.json();alert(`File upload failed: ${t.detail||e.statusText}`)}}catch(t){alert("An error occurred during file upload.")}},e.click()}(t)}async registerNewUser(t,e,a,s,r,n){t=t.trim(),e=e.trim(),a=a.trim(),n=n.trim();const o=document.getElementById("registrationResponseMessage");if(o.innerHTML="",""===t||""===e||""===a||""===s||""===r||""===n)return o.innerHTML="All fields are required.",void(o.style.color="red");if(s!==r)return o.innerHTML="Passwords do not match.",void(o.style.color="red");if(s.length<6)return o.innerHTML="Password must be at least 6 characters long.",void(o.style.color="red");o.style.color="black";try{await async function(t,e,a,s,r){try{const n=await fetch("/api/users",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:t,surname:e,mail:a,password:s,registration_security_key:r})});if(!n.ok){const t=await n.json();if(t?.detail)throw new Error(t?.detail);throw new Error(`HTTP error! status: ${n.status}, message: ${t?.detail||"Unknown error"}`)}return await n.json()}catch(t){throw t}}(t,e,a,s,n);o.innerHTML="Registration successful. You can now log in.",o.style.color="green";for(const t of["name","surname","email","password_1","password_2","security_code"]){const e=document.getElementById(`new_user_${t}`);e&&(e.value="")}}catch(t){t.response&&t.response.data&&t.response.data.detail?o.innerHTML=t.response.data.detail:o.innerHTML=t||"An error occurred during registration.",o.style.color="red"}}async deleteProject(t){confirm("Are you sure you want to delete this project? This action cannot be undone.")&&(await async function(t){try{const e=await fetch(`/api/projects/${t}`,{method:"DELETE",headers:{}});if(!e.ok){const t=await e.json();throw new Error(`HTTP error! status: ${e.status}, message: ${t?.detail||"Failed to delete file"}`)}return!0}catch(t){throw t}}(t),this.updateState())}updateHeaderLogin(){var t=document.getElementById("contentHeaderLogin");if(null===this.userInfo.id){var e="";e+='',t.innerHTML=e}else{e="";e+=''+this.userInfo.mail+"",e+='',t.innerHTML=e}}updateMenuContent(){var t=document.getElementById("contentMenu");if(null===this.userInfo.id){var e="";e+=`
`,e+=``,t.innerHTML=e}else{e="";e+=``,e+=``,this.userInfo.rights_admin&&(e+=``),t.innerHTML=e}}clearPageContent(){document.getElementById("contentMain").innerHTML=""}updatePageContent(){const t=null!==this.userInfo.id;var e=document.getElementById("contentMain");if("about"===this.page){var a="";a+="about
",a+='',e.innerHTML=a}else if("settings"===this.page&&t){a="";a+='',e.innerHTML=a,this.updateHtmlSettings()}else if("projects"===this.page&&t){a="";a+='',e.innerHTML=a,this.updateHtmlProjects()}else if("users"===this.page&&t){a="";a+='',e.innerHTML=a,this.updateHtmlUsers()}else if("login"!==this.page||t)if("registration"!==this.page||t)e.innerHTML="";else{a="";a+='',e.innerHTML=a,this.updateHtmlRegistration()}else{var a="";a+='',e.innerHTML=a,this.updateHtmlLogin()}}async updateHtmlRegistration(){document.getElementById("contentPageRegistration").innerHTML=''}async updateHtmlUsers(){var t=document.getElementById("contentPageUsers");if(null===this.userId){const a=await async function(){try{const t=await fetch("/api/users",{method:"GET",headers:{"Content-Type":"application/json"}});if(!t.ok){const e=await t.json();if(e?.detail)throw new Error(e?.detail);throw new Error(`HTTP error! status: ${t.status}, message: ${e?.detail||"Unknown error"}`)}return await t.json()}catch(t){throw t}}();var e="";e+='',e+='
',a.forEach((t,a)=>{e+='',e+=`| ${a+1} | `,e+=`${t.surname} ${t.name} | `,e+=`${t.mail} | `,e+=` | `,e+="
"}),e+="
",e+="
",t.innerHTML=e}else await async function(t){try{const e=await fetch(`/api/users/${t}`,{method:"GET",headers:{"Content-Type":"application/json"}});if(!e.ok){const t=await e.json();if(t?.detail)throw new Error(t?.detail);throw new Error(`HTTP error! status: ${e.status}, message: ${t?.detail||"Unknown error"}`)}return await e.json()}catch(t){throw t}}(this.userId)}async updateHtmlSettings(){var t=document.getElementById("contentPageSettings"),e="";e+='',e+='
',e+='',e+='| Change password: | ',e+="
",e+='',e+="| New password: | ",e+=' | ',e+="
",e+='',e+="New password: (repeat) | ",e+=' | ',e+="
",e+='',e+=` | `,e+="
",e+='',e+=' | ',e+="
",e+="
",e+="
",t.innerHTML=e}async updateHtmlProjects(){var t=document.getElementById("contentPageProjects");if(null===this.projectId){const a=await async function(){try{const t=await fetch("/api/projects",{method:"GET",headers:{"Content-Type":"application/json"}});if(!t.ok){const e=await t.json();if(e?.detail)throw new Error(e?.detail);throw new Error(`HTTP error! status: ${t.status}, message: ${e?.detail||"Unknown error"}`)}return await t.json()}catch(t){throw t}}();var e="";e+='',e+='
',e+='
',a.forEach((t,s)=>{e+=``,e+=`| ${s+1} | `,e+=`${t.name} | `,e+=` | `,e+="
"}),e+="
",e+="
",e+='
',e+="
",t.innerHTML=e}else{const a=await async function(t){try{const e=await fetch(`/api/projects/${t}`,{method:"GET",headers:{"Content-Type":"application/json"}});if(!e.ok){const t=await e.json();if(t?.detail)throw new Error(t?.detail);throw new Error(`HTTP error! status: ${e.status}, message: ${t?.detail||"Unknown error"}`)}return await e.json()}catch(t){throw t}}(this.projectId);e="";e+='',e+='
',e+='
',e+='',e+="| Name: | ",e+=` | `,e+="
",e+='',e+="| IFC file: | ",e+="",e+='',!0===a.fileExists&&(e+=` `),0===a.numAttributes&&(!0===a.fileExists?e+=` `:e+=` `),e+=" ",e+=" | ",e+="
",!0===a.fileExists&&(e+='',e+=" | ",e+=` | `,e+="
"),e+='',e+=' | ',e+="
",e+='',e+="| Owner: | ",e+=`${a.userEntity.name} ${a.userEntity.surname} (${a.userEntity.mail}) | `,e+="
",!0===a.fileExists&&(e+='',e+="| Outputs: | ",e+="",e+='",e+=" | ",e+="
"),e+="
",e+="
",e+='
',e+='
',e+='',e+='| Cooperating users: | ',e+="
",a.userEntitiesWithAccess.forEach((t,s)=>{e+='',e+=`| ${s+1}) | `,e+=`${t.name} ${t.surname} | `,e+=`${t.mail} | `,e+=` | `,e+="
"}),e+='',e+='| Add new user: | ',e+="",e+='',e+=" | ",e+="",e+=``,e+=" | ",e+="
",e+="
",e+="
",e+='
',e+='',e+="
",e+="
",t.innerHTML=e}}async updateHtmlLogin(){document.getElementById("contentPageLogin").innerHTML=''}updateState(){this.parseCurrentPageFromUrl(),this.loadCurrentUser()}updateStateForRequest(){this.updateHeaderLogin(),this.updateMenuContent(),this.updatePageContent()}};window.app=t,window.addEventListener("load",()=>{t.updateState()}),window.addEventListener("popstate",()=>{t.updateState()});