import{d as S,w as j,o as M,f as U,g as n,A as o,_ as A,P as _,j as s,t as i,O as p,J as b,B as v,am as H,an as B,a8 as x,C as k,ax as I,K as N,L as J,H as K,i as P,r as m,c as T,a as G}from"./index-DOFLUERq.js";import{I as g}from"./iconify-p99pbjf9.js";import{_ as W}from"./_plugin-vue_export-helper-DlAUqK2U.js";const X={class:"link-field"},Y={key:0,class:"field-label"},Z={key:0,class:"required"},ee={class:"input-wrapper"},ae={key:0,class:"link-buttons"},te={key:0,class:"dropdown-menu"},le={class:"dropdown-content"},se={key:0,class:"loading-state"},ne=["onClick"],oe={class:"item-label"},ie={key:1,class:"item-description"},ce={key:0,class:"empty-state"},re=S({__name:"LinkField",props:{modelValue:{},options:{},label:{},placeholder:{},required:{type:Boolean},disabled:{type:Boolean},getQuery:{type:Function},enableCreate:{type:Boolean}},emits:["update:modelValue","create"],setup(f,{emit:F}){const a=f,h=F,C=m(null),c=m(""),u=m(!1),w=m(!1),r=m([]),O=T(()=>!!a.modelValue);j(()=>a.modelValue,e=>{e&&!c.value&&(c.value=e,y("").then(()=>{const l=r.value.find(t=>t.value===e);l&&(c.value=l.label)}))},{immediate:!0});async function y(e=""){if(a.options){w.value=!0;try{const l=a.getQuery?.()||{},t=await G.post("/api/action/jingrow.desk.search.search_link",{pagetype:a.options,txt:e||"",page_length:20,...l});if(t.data?.message){const $=t.data.message.filter(d=>d.value&&!d.value.includes("__link_option")).map(d=>({value:d.value,label:d.label||d.value,description:d.description}));a.enableCreate!==!1?r.value=[...$,{value:"__create_new__",label:`${i("Create")} ${i(a.options)}`,type:"create"}]:r.value=$}else a.enableCreate!==!1?r.value=[{value:"__create_new__",label:`${i("Create")} ${i(a.options)}`,type:"create"}]:r.value=[]}catch(l){console.error("Search link error:",l),a.enableCreate!==!1?r.value=[{value:"__create_new__",label:`${i("Create")} ${i(a.options)}`,type:"create"}]:r.value=[]}finally{w.value=!1}}}async function q(){a.disabled||u.value||(await y(c.value),u.value=!0)}async function D(){a.disabled||u.value||(await y(c.value),u.value=!0)}async function Q(e){c.value=e,await y(e)}function z(e){if(e.type==="create"){u.value=!1,h("create");return}c.value=e.label,h("update:modelValue",e.value),u.value=!1}function E(){c.value="",h("update:modelValue","")}function R(){if(a.modelValue){const e=a.options||"";let l;e.toLowerCase().includes("email template")?l=`/app/email-template/${a.modelValue}`:l=`/app/${e.toLowerCase().replace(/\s+/g,"-")}/${a.modelValue}`,window.open(l,"_blank")}}function V(e){C.value&&!C.value.contains(e.target)&&(u.value=!1)}return M(()=>{document.addEventListener("click",V)}),U(()=>{document.removeEventListener("click",V)}),(e,l)=>(n(),o("div",X,[f.label?(n(),o("label",Y,[A(_(s(i)(f.label))+" ",1),f.required?(n(),o("span",Z,"*")):p("",!0)])):p("",!0),b("div",{class:"link-field-container",ref_key:"containerRef",ref:C},[b("div",ee,[v(s(H),{value:c.value,"onUpdate:value":l[0]||(l[0]=t=>c.value=t),placeholder:"",disabled:f.disabled,onFocus:q,onInput:Q,onClick:D,class:"link-input"},null,8,["value","disabled"]),O.value?(n(),o("div",ae,[v(s(B),{size:"small",quaternary:"",onClick:x(E,["stop"]),title:s(i)("Clear"),class:"action-btn"},{icon:k(()=>[v(s(I),null,{default:k(()=>[v(s(g),{icon:"mdi:close"})]),_:1})]),_:1},8,["title"]),v(s(B),{size:"small",quaternary:"",onClick:x(R,["stop"]),title:s(i)("View Details"),class:"action-btn"},{icon:k(()=>[v(s(I),null,{default:k(()=>[v(s(g),{icon:"mdi:arrow-right"})]),_:1})]),_:1},8,["title"])])):p("",!0)]),u.value?(n(),o("div",te,[b("div",le,[w.value?(n(),o("div",se,_(s(i)("Loading...")),1)):(n(),o(N,{key:1},[(n(!0),o(N,null,J(r.value,t=>(n(),o("div",{key:t.value,class:K(["dropdown-item",{"create-item":t.type==="create"}]),onClick:L=>z(t)},[t.type==="create"?(n(),P(s(g),{key:0,icon:"mdi:plus",class:"item-icon"})):p("",!0),b("span",oe,_(t.label),1),t.description?(n(),o("span",ie,_(t.description),1)):p("",!0)],10,ne))),128)),r.value.length===0?(n(),o("div",ce,_(s(i)("No Results found")),1)):p("",!0)],64))])])):p("",!0)],512)]))}}),fe=W(re,[["__scopeId","data-v-b2e9010a"]]);export{fe as L};
