import{_ as T,a as _,R as v,G as u,S as C,c as r,b as o,e as c,F as h,g as p,D as w,J as x,t as l,an as m,E as d,I,f as R,h as f,o as a}from"./Bgmy51Qv.js";import{u as D}from"./7pZkuj9N.js";import{d as W}from"./B9SrJvRU.js";const L={setup(){const{setRequest:e,getRequest:t}=D();return{setRequest:e,getRequest:t}},name:"TagSearch",data(){return{setApi:"setTags",searchedString:"",tagList:null,loading:!1,autoSuggestionOpen:!1,deleteWarning:!1}},props:{tags:{type:String,default:""}},directives:{outsideClick:v},components:{Spinner:_},watch:{searchedString:W(function(){this.fetchingData()},700)},computed:{tagExists(){return this.tagList.findIndex(e=>e.title===this.searchedString)>-1},selectedTags(){return this.tags?this.tags.split(",").filter(e=>e):[]}},methods:{deleteKeyPress(){this.searchedString||(this.deleteWarning&&this.deleteTag(this.selectedTags[this.selectedTags.length-1]),this.deleteWarning=!this.deleteWarning)},deleteTag(e){this.$emit("delete",e)},openResult(){this.autoSuggestionOpen=!this.autoSuggestionOpen,this.fetchingData()},async saveTag(){this.loading=!0;try{await this.setRequest({params:{title:this.searchedString},api:"setTags"})}catch(e){f({statusCode:400,message:e.message})}this.loading=!1},async fetchingData(){this.loading=!0;try{const{data:e}=await this.getRequest({params:{q:this.searchedString},api:"getTags"});e&&(this.tagList=e)}catch(e){f({statusCode:400,message:e.message})}this.loading=!1},autoSuggestionClose(){this.autoSuggestionOpen=this.deleteWarning=!1,this.searchedString=""},emitAddingTag(e){this.selectedTags.findIndex(t=>t===e)===-1&&this.$emit("add",e)},clickedItem(e){e||(this.saveTag(),e=this.searchedString),this.emitAddingTag(e),this.autoSuggestionClose(),this.$refs.tagSearchInput.focus()}}},q={class:"tag-wrapper"},E={class:"tags"},O={class:"tag"},V=["onClick"],K={class:"tag-input"},N={key:0,class:"tag-dropdown"},b={key:0,class:"spinner-wrapper"},A={key:1},B=["onClick"],F={key:0},M={key:2};function P(e,t,z,G,i,n){const S=_,k=C("outside-click");return u((a(),r("div",q,[o("ul",E,[(a(!0),r(h,null,p(n.selectedTags,(s,g)=>(a(),r("li",{key:g,class:w({warning:i.deleteWarning&&g===n.selectedTags.length-1})},[o("span",O,[x(l(s)+" ",1),o("span",{class:"close-btn",onClick:y=>n.deleteTag(s)},t[5]||(t[5]=[o("i",{class:"icon close-icon"},null,-1)]),8,V)])],2))),128)),o("li",K,[u(o("input",{type:"text",ref:"tagSearchInput","onUpdate:modelValue":t[0]||(t[0]=s=>i.searchedString=s),onFocus:t[1]||(t[1]=(...s)=>n.openResult&&n.openResult(...s)),onKeydown:[t[2]||(t[2]=m(d(s=>n.clickedItem({title:i.searchedString}),["prevent"]),["enter"])),t[3]||(t[3]=m(s=>n.deleteKeyPress(),["delete"]))],placeholder:"Write here"},null,544),[[I,i.searchedString]])])]),i.autoSuggestionOpen?(a(),r("div",N,[i.loading?(a(),r("div",b,[R(S,{radius:40,color:"primary"})])):i.tagList?(a(),r("ul",A,[(a(!0),r(h,null,p(i.tagList,(s,g)=>(a(),r("li",{key:g},[o("a",{href:"#",onClick:d(y=>n.clickedItem(s.title),["prevent"])},l(s.title),9,B)]))),128)),i.searchedString&&!n.tagExists?(a(),r("li",F,[o("a",{href:"#",onClick:t[4]||(t[4]=d(s=>n.clickedItem(null),["prevent"]))},l(i.searchedString)+" ("+l(e.$t("error.new"))+") ",1)])):c("",!0)])):i.searchedString?c("",!0):(a(),r("p",M,l(e.$t("index.nf")),1))])):c("",!0)])),[[k,n.autoSuggestionClose]])}const H=T(L,[["render",P]]);export{H as _};
