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