import{defineComponent as $,ref as C,reactive as q,watchEffect as z,computed as G,provide as H,toRef as b,onUpdated as J,nextTick as Q,createVNode as d}from"vue";import X from"../../_util/BaseInput.mjs";import i from"../../_util/KeyCode.mjs";import Y from"../../_util/classNames.mjs";import Z from"../../_util/omit.mjs";import _ from"../../_util/props-util/initDefaultProps.mjs";import ee from"./KeywordTrigger.mjs";import te from"./MentionsContext.mjs";import{defaultProps as ne,vcMentionsProps as oe}from"./mentionsProps.mjs";import{replaceWithMeasure as se,getBeforeSelectionText as re,getLastMeasureIndex as ae,setInputSelection as ie}from"./util.mjs";function L(){}const Ie=$({compatConfig:{MODE:3},name:"Mentions",inheritAttrs:!1,props:_(oe,ne),emits:["change","select","search","focus","blur","pressenter"],setup(r,{emit:u,attrs:D,expose:K,slots:S}){const M=C(null),l=C(null),N=C(),t=q({value:r.value||"",measuring:!1,measureLocation:0,measureText:null,measurePrefix:"",activeIndex:0,isFocus:!1});z(()=>{t.value=r.value});const y=e=>{u("change",e)},B=({target:{value:e}})=>{y(e)},U=(e,n,o)=>{Object.assign(t,{measuring:!0,measureText:e,measurePrefix:n,measureLocation:o,activeIndex:0})},f=e=>{Object.assign(t,{measuring:!1,measureLocation:0,measureText:null}),e?.()},A=e=>{const{which:n}=e;if(t.measuring){if(n===i.UP||n===i.DOWN){const o=g.value.length,a=n===i.UP?-1:1,s=(t.activeIndex+a+o)%o;t.activeIndex=s,e.preventDefault()}else if(n===i.ESC)f();else if(n===i.ENTER){if(e.preventDefault(),!g.value.length){f();return}const o=g.value[t.activeIndex];E(o)}}},R=e=>{const{key:n,which:o}=e,{measureText:a,measuring:s}=t,{prefix:h,validateSearch:P}=r,v=e.target;if(v.composing)return;const x=re(v),{location:p,prefix:m}=ae(x,h);if(![i.ESC,i.UP,i.DOWN,i.ENTER].includes(o))if(p!==-1){const c=x.slice(p+m.length),I=P(c,r),T=!!F(c).length;I?(n===m||n==="Shift"||s||c!==a&&T)&&U(c,m,p):s&&f(),I&&u("search",c,m)}else s&&f()},W=e=>{t.measuring||u("pressenter",e)},j=e=>{O(e)},k=e=>{w(e)},O=e=>{clearTimeout(N.value);const{isFocus:n}=t;!n&&e&&u("focus",e),t.isFocus=!0},w=e=>{N.value=setTimeout(()=>{t.isFocus=!1,f(),u("blur",e)},100)},E=e=>{const{split:n}=r,{value:o=""}=e,{text:a,selectionLocation:s}=se(t.value,{measureLocation:t.measureLocation,targetText:o,prefix:t.measurePrefix,selectionStart:l.value.getSelectionStart(),split:n});y(a),f(()=>{ie(l.value.input,s)}),u("select",e,t.measurePrefix)},V=e=>{t.activeIndex=e},F=e=>{const n=e||t.measureText||"",{filterOption:o}=r;return r.options.filter(s=>o?o(n,s):!0)},g=G(()=>F());return K({blur:()=>{l.value.blur()},focus:()=>{l.value.focus()}}),H(te,{activeIndex:b(t,"activeIndex"),setActiveIndex:V,selectOption:E,onFocus:O,onBlur:w,loading:b(r,"loading")}),J(()=>{Q(()=>{t.measuring&&(M.value.scrollTop=l.value.getScrollTop())})}),()=>{const{measureLocation:e,measurePrefix:n,measuring:o}=t,{prefixCls:a,placement:s,transitionName:h,getPopupContainer:P,direction:v,...x}=r,{class:p,style:m,...c}=D,T={...Z(x,["value","prefix","split","validateSearch","filterOption","options","loading"]),...c,onChange:L,onSelect:L,value:t.value,onInput:B,onBlur:k,onKeydown:A,onKeyup:R,onFocus:j,onPressenter:W};return d("div",{class:Y(a,p),style:m},[d(X,{...T,ref:l,tag:"textarea"},null),o&&d("div",{ref:M,class:`${a}-measure`},[t.value.slice(0,e),d(ee,{prefixCls:a,transitionName:h,dropdownClassName:r.dropdownClassName,placement:s,options:o?g.value:[],visible:!0,direction:v,getPopupContainer:P},{default:()=>[d("span",null,[n])],notFoundContent:S.notFoundContent,option:S.option}),t.value.slice(e+n.length)])])}}});export{Ie as default};
