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