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