import{c as v,i as w,b as l}from"./init-DI1ou7Mp.js";import{N as b}from"./Navbar-II0eMt6W.js";import{_ as F}from"./Checkbox-C3tFJb86.js";import"./theme-BfDtj5yE.js";import"./_plugin-vue_export-helper-DlAUqK2U.js";const f={name:"New App",output:"",cmd:"","exclude-global-prep-cmd":!1,"exclude-global-state-cmd":!1,elevated:!1,"auto-detach":!0,"wait-all":!0,"exit-timeout":5,"prep-cmd":[],"state-cmd":[],detached:[],"image-path":"","scale-factor":100,"use-app-identity":!1,"per-client-app-identity":!1,"allow-client-commands":!0,"virtual-display":!1,"terminate-on-pause":!1,gamepad:""},m=v({components:{Navbar:b,Checkbox:F},data(){return{apps:[],showEditForm:!1,actionDisabled:!1,editForm:null,detachedCmd:"",coverSearching:!1,coverFinderBusy:!1,coverCandidates:[],platform:"",currentApp:"",draggingApp:-1,hostName:"",hostUUID:"",listReordered:!1}},created(){this.loadApps(),fetch("./api/config",{credentials:"include"}).then(e=>e.json()).then(e=>this.platform=e.platform)},methods:{onDragStart(e,t){if(this.showEditForm){e.preventDefault();return}this.draggingApp=t,this.apps.push({})},onDragEnter(e,t){this.draggingApp<0||(e.preventDefault(),e.dataTransfer.dropEffect="move",t.dragover=!0)},onDragOver(e){this.draggingApp<0||(e.preventDefault(),e.dataTransfer.dropEffect="move")},onDragLeave(e){e.dragover=!1},onDragEnd(){this.draggingApp=-1,this.apps.pop()},onDrop(e,t,r){if(t.dragover=!1,this.draggingApp<0||(e.preventDefault(),r===this.draggingApp||r-1===this.draggingApp))return;const i=this.apps[this.draggingApp];this.apps.splice(this.draggingApp,1),r>this.draggingApp&&(r-=1),this.apps.splice(r,0,i),this.listReordered=!0},alphabetizeApps(){let e=0;this.apps.sort((t,r)=>{const i=t.name.localeCompare(r.name);return e+=i,i}),this.listReordered=e!==this.apps.length-1,this.listReordered||alert(this.$t("apps.already_ordered"))},saveOrder(){this.actionDisabled=!0;const e=this.apps.reduce((t,r)=>(r.uuid&&t.push(r.uuid),t),[]);fetch("./api/apps/reorder",{credentials:"include",headers:{"Content-Type":"application/json"},method:"POST",body:JSON.stringify({order:e})}).then(t=>t.json()).then(t=>{t.status||alert(this.$t("apps.reorder_failed")+t.error)}).finally(()=>this.loadApps()).finally(()=>{this.actionDisabled=!1})},loadApps(){return fetch("./api/apps",{credentials:"include"}).then(e=>e.json()).then(e=>{this.apps=e.apps.filter(t=>t.uuid).map(t=>({...t,launching:!1,dragover:!1})),this.currentApp=e.current_app,this.hostName=e.host_name,this.hostUUID=e.host_uuid,this.listReordered=!1})},newApp(){this.editForm=Object.assign({},f),this.showEditForm=!0},launchApp(e,t){if(!(["localhost","127.0.0.1","[::1]"].indexOf(location.hostname)>=0)&&confirm(this.$t("apps.launch_local_client"))){const i=document.createElement("a");i.href=e.target.href,i.click();return}confirm(this.$t("apps.launch_warning"))&&(this.actionDisabled=!0,fetch("./api/apps/launch",{credentials:"include",headers:{"Content-Type":"application/json"},method:"POST",body:JSON.stringify({uuid:t.uuid})}).then(i=>i.json()).then(i=>{i.status||alert(this.$t("apps.launch_failed")+i.error)}).finally(()=>{this.actionDisabled=!1,this.loadApps()}))},closeApp(){confirm(this.$t("apps.close_warning"))&&(this.actionDisabled=!0,fetch("./api/apps/close",{credentials:"include",headers:{"Content-Type":"application/json"},method:"POST"}).then(e=>e.json()).then(e=>{e.status||alert("apps.close_failed")}).finally(()=>{this.actionDisabled=!1,this.loadApps()}))},editApp(e){this.editForm=Object.assign({},f,JSON.parse(JSON.stringify(e))),this.showEditForm=!0},exportLauncherFile(e){const t=document.createElement("a"),r=`# Artemis app entry
# Exported by Apollo
# https://github.com/ClassicOldSong/Apollo

[host_uuid] ${this.hostUUID}
[host_name] ${this.hostName}
[app_uuid] ${e.uuid}
[app_name] ${e.name}
`;t.href=`data:text/plain;charset=utf-8,${encodeURIComponent(r)}`,t.download=`${e.name}.art`,t.click()},showDeleteForm(e){confirm("Are you sure to delete "+e.name+"?")&&(this.actionDisabled=!0,fetch("./api/apps/delete",{credentials:"include",headers:{"Content-Type":"application/json"},method:"POST",body:JSON.stringify({uuid:e.uuid})}).then(r=>r.json()).then(r=>{r.status||alert("Delete failed! "+r.error)}).finally(()=>{this.actionDisabled=!1,this.loadApps()}))},addCmd(e,t){const r={do:"",undo:""};this.platform==="windows"&&(r.elevated=!1),t<0?e.push(r):e.splice(t,0,r)},showCoverFinder(e){this.coverCandidates=[],this.coverSearching=!0;const t=this.$refs.coverFinderDropdown;if(!t){console.error("Ref not found!");return}if(this.coverFinderDropdown=l.Dropdown.getInstance(t),!this.coverFinderDropdown&&(this.coverFinderDropdown=new l.Dropdown(t),!this.coverFinderDropdown))return;this.coverFinderDropdown.show();function r(a){let n=a.substring(0,Math.min(a.length,2)).toLowerCase().replaceAll(/[^a-z\d]/g,"");return n||"@"}function i(a){if(!a)return Promise.resolve([]);let n=a.replaceAll(/\s+/g,".").toLowerCase(),h="https://raw.githubusercontent.com/LizardByte/GameDB/gh-pages",g=r(a);return fetch(`${h}/buckets/${g}.json`).then(function(o){if(!o.ok)throw new Error("Failed to search covers");return o.json()}).then(o=>Promise.all(Object.keys(o).map(s=>o[s].name.replaceAll(/\s+/g,".").toLowerCase().startsWith(n)?fetch(`${h}/games/${s}.json`).then(function(p){return p.json()}).catch(()=>null):null).filter(s=>s))).then(o=>o.filter(s=>s&&s.cover&&s.cover.url).map(s=>{const d=s.cover.url,p=d.lastIndexOf("."),c=d.lastIndexOf("/");if(p<0||c<0)return null;const u=d.substring(c+1,p);return{name:s.name,key:`igdb_${s.id}`,url:`https://images.igdb.com/igdb/image/upload/t_cover_big/${u}.jpg`,saveUrl:`https://images.igdb.com/igdb/image/upload/t_cover_big_2x/${u}.png`}}).filter(s=>s))}i(this.editForm.name.toString().trim()).then(a=>this.coverCandidates=a).finally(()=>this.coverSearching=!1)},closeCoverFinder(){const e=this.$refs.coverFinderDropdown;if(!e)return;const t=this.coverFinderDropdown=l.Dropdown.getInstance(e);t&&t.hide()},useCover(e){this.coverFinderBusy=!0,fetch("./api/covers/upload",{credentials:"include",headers:{"Content-Type":"application/json"},method:"POST",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.name=this.editForm.name.trim(),this.editForm.name&&(this.editForm["exit-timeout"]=parseInt(this.editForm["exit-timeout"])||5,this.editForm["scale-factor"]=parseInt(this.editForm["scale-factor"])||100,this.editForm["image-path"]=this.editForm["image-path"].toString().trim().replace(/"/g,""),delete this.editForm.id,delete this.editForm.launching,delete this.editForm.dragover,fetch("./api/apps",{credentials:"include",headers:{"Content-Type":"application/json"},method:"POST",body:JSON.stringify(this.editForm)}).then(e=>e.json()).then(e=>{if(!e.status)throw alert(this.$t("apps.save_failed")+e.error),new Error(`App save failed: ${e.error}`)}).then(()=>{this.showEditForm=!1,this.loadApps()}))}}});m.directive("dropdown-show",{mounted:function(e,t){e.addEventListener("show.bs.dropdown",t.value)}});w(m);
