import{shallowRef as l,computed as B,watch as R,onMounted as K,onBeforeUnmount as F}from"vue";import o from"../../_util/KeyCode.mjs";import I from"../../_util/raf.mjs";import{addGlobalMousedownEvent as P,getTargetFromEvent as b}from"../utils/uiUtil.mjs";function N({open:a,value:D,isClickOutside:r,triggerOpen:f,forwardKeydown:c,onKeydown:T,blurToCancel:y,onSubmit:m,onCancel:d,onFocus:h,onBlur:w}){const t=l(!1),v=l(!1),s=l(!1),n=l(!1),p=l(!1),M=B(()=>({onMousedown:()=>{t.value=!0,f(!0)},onKeydown:e=>{if(T(e,()=>{p.value=!0}),!p.value){switch(e.which){case o.ENTER:{a.value?m()!==!1&&(t.value=!0):f(!0),e.preventDefault();return}case o.TAB:{t.value&&a.value&&!e.shiftKey?(t.value=!1,e.preventDefault()):!t.value&&a.value&&!c(e)&&e.shiftKey&&(t.value=!0,e.preventDefault());return}case o.ESC:{t.value=!0,d();return}}!a.value&&![o.SHIFT].includes(e.which)?f(!0):t.value||c(e)}},onFocus:e=>{t.value=!0,v.value=!0,h&&h(e)},onBlur:e=>{if(s.value||!r(document.activeElement)){s.value=!1;return}y.value?setTimeout(()=>{let{activeElement:u}=document;for(;u&&u.shadowRoot;)u=u.shadowRoot.activeElement;r(u)&&d()},0):a.value&&(f(!1),n.value&&m()),v.value=!1,w&&w(e)}}));R(a,()=>{n.value=!1}),R(D,()=>{n.value=!0});const i=l();return K(()=>{i.value=P(e=>{const u=b(e);if(a.value){const E=r(u);E?(!v.value||E)&&f(!1):(s.value=!0,I(()=>{s.value=!1}))}})}),F(()=>{i.value&&i.value()}),[M,{focused:v,typing:t}]}export{N as default};
