import{c as F,i as g,b as h}from"./_plugin-vue_export-helper-5d7c36a3.js";import{N as v}from"./Navbar-ed33a51c.js";import{_ as w}from"./Checkbox-3fcd019d.js";const m=F({components:{Navbar:v,Checkbox:w},data(){return{apps:[],showEditForm:!1,editForm:null,detachedCmd:"",coverSearching:!1,coverFinderBusy:!1,coverCandidates:[],platform:""}},created(){fetch("./api/apps").then(e=>e.json()).then(e=>{console.log(e),this.apps=e.apps}),fetch("./api/config").then(e=>e.json()).then(e=>this.platform=e.platform)},methods:{newApp(){this.editForm={name:"",output:"",cmd:[],index:-1,"exclude-global-prep-cmd":!1,elevated:!1,"auto-detach":!0,"wait-all":!0,"exit-timeout":5,"prep-cmd":[],detached:[],"image-path":""},this.editForm.index=-1,this.showEditForm=!0},editApp(e){this.editForm=JSON.parse(JSON.stringify(this.apps[e])),this.editForm.index=e,this.editForm["prep-cmd"]===void 0&&(this.editForm["prep-cmd"]=[]),this.editForm.detached===void 0&&(this.editForm.detached=[]),this.editForm["exclude-global-prep-cmd"]===void 0&&(this.editForm["exclude-global-prep-cmd"]=!1),this.editForm.elevated===void 0&&this.platform==="windows"&&(this.editForm.elevated=!1),this.editForm["auto-detach"]===void 0&&(this.editForm["auto-detach"]=!0),this.editForm["wait-all"]===void 0&&(this.editForm["wait-all"]=!0),this.editForm["exit-timeout"]===void 0&&(this.editForm["exit-timeout"]=5),this.showEditForm=!0},showDeleteForm(e){confirm("Are you sure to delete "+this.apps[e].name+"?")&&fetch("./api/apps/"+e,{method:"DELETE",headers:{"Content-Type":"application/json"}}).then(a=>{a.status===200&&document.location.reload()})},addPrepCmd(){let e={do:"",undo:""};this.platform==="windows"&&(e={...e,elevated:!1}),this.editForm["prep-cmd"].push(e)},showCoverFinder(e){this.coverCandidates=[],this.coverSearching=!0;const t=this.$refs.coverFinderDropdown;if(!t){console.error("Ref not found!");return}if(this.coverFinderDropdown=h.Dropdown.getInstance(t),!this.coverFinderDropdown&&(this.coverFinderDropdown=new h.Dropdown(t),!this.coverFinderDropdown))return;this.coverFinderDropdown.show();function a(o){let n=o.substring(0,Math.min(o.length,2)).toLowerCase().replaceAll(/[^a-z\d]/g,"");return n||"@"}function u(o){if(!o)return Promise.resolve([]);let n=o.replaceAll(/\s+/g,".").toLowerCase(),c="https://raw.githubusercontent.com/LizardByte/GameDB/gh-pages",f=a(o);return fetch(`${c}/buckets/${f}.json`).then(function(i){if(!i.ok)throw new Error("Failed to search covers");return i.json()}).then(i=>Promise.all(Object.keys(i).map(r=>i[r].name.replaceAll(/\s+/g,".").toLowerCase().startsWith(n)?fetch(`${c}/games/${r}.json`).then(function(d){return d.json()}).catch(()=>null):null).filter(r=>r))).then(i=>i.filter(r=>r&&r.cover&&r.cover.url).map(r=>{const s=r.cover.url,d=s.lastIndexOf("."),p=s.lastIndexOf("/");if(d<0||p<0)return null;const l=s.substring(p+1,d);return{name:r.name,key:`igdb_${r.id}`,url:`https://images.igdb.com/igdb/image/upload/t_cover_big/${l}.jpg`,saveUrl:`https://images.igdb.com/igdb/image/upload/t_cover_big_2x/${l}.png`}}).filter(r=>r))}u(this.editForm.name.toString()).then(o=>this.coverCandidates=o).finally(()=>this.coverSearching=!1)},closeCoverFinder(){const e=this.$refs.coverFinderDropdown;if(!e)return;const t=this.coverFinderDropdown=h.Dropdown.getInstance(e);t&&t.hide()},useCover(e){this.coverFinderBusy=!0,fetch("./api/covers/upload",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:e.key,url:e.saveUrl})}).then(t=>{if(!t.ok)throw new Error("Failed to download covers");return t.json()}).then(t=>this.editForm["image-path"]=t.path).then(()=>this.closeCoverFinder()).finally(()=>this.coverFinderBusy=!1)},save(){this.editForm["image-path"]=this.editForm["image-path"].toString().replace(/"/g,""),fetch("./api/apps",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(this.editForm)}).then(e=>{e.status===200&&document.location.reload()})}}});m.directive("dropdown-show",{mounted:function(e,t){e.addEventListener("show.bs.dropdown",t.value)}});g(m);