"use strict";document.addEventListener("alpine:init",()=>{Alpine.data("agentForm",(x,F)=>({isOpen:!1,repositoryId:"",instructions:"",priority:"normal",repositoryFiles:[],selectedRepositoryId:null,autocomplete:{isOpen:!1,selectedIndex:-1,filteredFiles:[],currentPrefix:"",cursorPosition:0},init(){this.setupEventListeners()},open(){this.isOpen=!0,this.loadRepositories(),this.$nextTick(()=>{setTimeout(()=>this.initializeAutocomplete(),100)})},close(){this.isOpen=!1,this.resetForm(),this.$el.closest(".modal").remove()},resetForm(){this.repositoryId="",this.instructions="",this.priority="normal",this.repositoryFiles=[],this.selectedRepositoryId=null,this.closeAutocomplete()},setupEventListeners(){document.addEventListener("keydown",t=>{t.key==="Escape"&&this.isOpen&&this.close()})},async loadRepositories(){try{const t=await htmx.ajax("GET",`/api/git-repositories/project/${F}/options`,{target:this.$refs.repositorySelect,swap:"innerHTML"})}catch(t){console.error("Failed to load repositories:",t)}},async onRepositoryChange(){this.repositoryId&&this.repositoryId!==""?await this.loadFileListForAutocomplete(this.repositoryId):(this.repositoryFiles=[],this.selectedRepositoryId=null)},async loadFileListForAutocomplete(t){console.log("Loading files for repository:",t);try{const e=await fetch(`/api/git-repositories/${t}/all-files`);if(console.log("API response status:",e.status),!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);const o=await e.json();this.repositoryFiles=o,this.selectedRepositoryId=t,console.log("Loaded files for autocomplete:",o.length),console.log("Sample files:",o.slice(0,10))}catch(e){console.error("Failed to load files for autocomplete:",e),this.repositoryFiles=[]}},initializeAutocomplete(){const t=this.$refs.instructionsTextarea;t&&(t.addEventListener("input",e=>{this.handleAutocompleteInput(e)}),t.addEventListener("keydown",e=>{if(this.autocomplete.isOpen)switch(e.key){case"ArrowDown":e.preventDefault(),this.navigateAutocomplete(1);break;case"ArrowUp":e.preventDefault(),this.navigateAutocomplete(-1);break;case"Tab":case"Enter":this.autocomplete.selectedIndex>=0&&(e.preventDefault(),this.selectFile(this.autocomplete.filteredFiles[this.autocomplete.selectedIndex]));break;case"Escape":this.closeAutocomplete();break}}),document.addEventListener("click",e=>{var o;!((o=this.$refs.autocompleteDiv)!=null&&o.contains(e.target))&&e.target!==t&&this.closeAutocomplete()}))},handleAutocompleteInput(t){var n;const e=t.target,o=e.value,a=e.selectionStart;let h=-1;for(let s=a-1;s>=0;s--){if(o[s]==="@"){h=s;break}if(o[s]===" "||o[s]===` `)break}if(h>=0&&this.repositoryFiles&&this.repositoryFiles.length>0){const s=o.substring(h+1,a);this.autocomplete.currentPrefix=s,this.autocomplete.cursorPosition=a,console.log("Autocomplete prefix:",`"${s}"`,"Available files:",((n=this.repositoryFiles)==null?void 0:n.length)||0),this.autocomplete.filteredFiles=this.repositoryFiles.filter(l=>{const r=l.toLowerCase(),i=s.toLowerCase(),d=l.endsWith("/"),c=d?r.slice(0,-1):r;return c.startsWith(i)||r.includes("/"+i)||(c.split("/").pop()||"").startsWith(i)||d&&(c.split("/").pop()||c).startsWith(i)?!0:r.includes(i)}).sort((l,r)=>{const i=l.toLowerCase(),d=r.toLowerCase(),c=s.toLowerCase(),p=l.endsWith("/"),u=r.endsWith("/"),A=p?i.slice(0,-1):i,w=u?d.slice(0,-1):d,m=A.startsWith(c),f=w.startsWith(c);if(m&&!f)return-1;if(!m&&f)return 1;if(m&&f){if(p&&!u)return-1;if(!p&&u)return 1}const g=l.split("/").length,y=r.split("/").length;return g!==y?g-y:p&&!u?-1:!p&&u?1:l.localeCompare(r)}).slice(0,20),console.log("Filtered files:",this.autocomplete.filteredFiles),this.autocomplete.filteredFiles.length>0?this.showAutocomplete():this.closeAutocomplete()}else this.closeAutocomplete()},showAutocomplete(){this.autocomplete.isOpen=!0,this.autocomplete.selectedIndex=0},navigateAutocomplete(t){this.autocomplete.selectedIndex+=t,this.autocomplete.selectedIndex<0?this.autocomplete.selectedIndex=this.autocomplete.filteredFiles.length-1:this.autocomplete.selectedIndex>=this.autocomplete.filteredFiles.length&&(this.autocomplete.selectedIndex=0)},selectFile(t){const e=this.$refs.instructionsTextarea,o=e.value,a=this.autocomplete.cursorPosition,h=t.endsWith("/");let n=-1;for(let s=a-1;s>=0;s--)if(o[s]==="@"){n=s;break}if(n>=0){const s=o.substring(0,n),l=o.substring(a),r=s+"@"+t+l;this.instructions=r;const i=n+1+t.length;this.$nextTick(()=>{e.setSelectionRange(i,i),e.focus()}),h?(this.autocomplete.cursorPosition=i,setTimeout(()=>{this.handleAutocompleteInput({target:e})},10)):this.closeAutocomplete()}else this.closeAutocomplete()},closeAutocomplete(){this.autocomplete.isOpen=!1,this.autocomplete.selectedIndex=-1},async submitForm(){const t={repository_id:this.repositoryId||null,instructions:this.instructions,priority:this.priority};try{const e=await fetch(`/api/tasks/${x}/agent`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});e.ok?(this.close(),htmx.trigger(document.body,"refresh-tasks")):console.error("Failed to assign agent:",e.statusText)}catch(e){console.error("Failed to assign agent:",e)}},getAutocompleteItemClass(t){return`px-3 py-2 cursor-pointer hover:bg-gray-100 text-sm ${t===this.autocomplete.selectedIndex?"bg-blue-100":""}`}}))});