import{bz as ne,L as _,n as E,bC as se,d as oe,al as re,I as ce,o as O,b6 as H,ao as R,af as X,r as C,ab as ae,a7 as ue,p as ie,a8 as de,ak as V,b5 as fe}from"./index-34b18f6d.js";function le(e){return e==null}class ve extends Error{constructor(t){super(t),this.name="ElementPlusError"}}function Re(e,t){throw new ve(`[${e}] ${t}`)}function Ne(e,t){}const ke=(...e)=>t=>{e.forEach(s=>{ne(s)?s(t):s.value=t})},Ae=({from:e,replacement:t,scope:s,version:o,ref:c,type:i="API"},f)=>{_(()=>E(f),m=>{},{immediate:!0})},B={prefix:Math.floor(Math.random()*1e4),current:0},pe=Symbol("elIdInjection"),Ee=()=>re()?ce(pe,B):B,Ke=e=>{const t=Ee(),s=se();return oe(()=>E(e)||`${s.value}-id-${t.prefix}-${t.current++}`)};let p=[];const M=e=>{const t=e;t.key===X.esc&&p.forEach(s=>s(t))},me=e=>{O(()=>{p.length===0&&document.addEventListener("keydown",M),H&&p.push(e)}),R(()=>{p=p.filter(t=>t!==e),p.length===0&&H&&document.removeEventListener("keydown",M)})},Ue=Symbol("formContextKey"),xe=Symbol("formItemContextKey"),L="focus-trap.focus-after-trapped",g="focus-trap.focus-after-released",Te="focus-trap.focusout-prevented",W={cancelable:!0,bubbles:!1},Fe={cancelable:!0,bubbles:!1},q="focusAfterTrapped",J="focusAfterReleased",be=Symbol("elFocusTrap"),N=C(),I=C(0),k=C(0);let F=0;const Z=e=>{const t=[],s=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,{acceptNode:o=>{const c=o.tagName==="INPUT"&&o.type==="hidden";return o.disabled||o.hidden||c?NodeFilter.FILTER_SKIP:o.tabIndex>=0||o===document.activeElement?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});for(;s.nextNode();)t.push(s.currentNode);return t},Y=(e,t)=>{for(const s of e)if(!ye(s,t))return s},ye=(e,t)=>{if(getComputedStyle(e).visibility==="hidden")return!0;for(;e;){if(t&&e===t)return!1;if(getComputedStyle(e).display==="none")return!0;e=e.parentElement}return!1},_e=e=>{const t=Z(e),s=Y(t,e),o=Y(t.reverse(),e);return[s,o]},Ce=e=>e instanceof HTMLInputElement&&"select"in e,v=(e,t)=>{if(e&&e.focus){const s=document.activeElement;e.focus({preventScroll:!0}),k.value=window.performance.now(),e!==s&&Ce(e)&&t&&e.select()}};function z(e,t){const s=[...e],o=e.indexOf(t);return o!==-1&&s.splice(o,1),s}const Ie=()=>{let e=[];return{push:o=>{const c=e[0];c&&o!==c&&c.pause(),e=z(e,o),e.unshift(o)},remove:o=>{var c,i;e=z(e,o),(i=(c=e[0])==null?void 0:c.resume)==null||i.call(c)}}},Pe=(e,t=!1)=>{const s=document.activeElement;for(const o of e)if(v(o,t),document.activeElement!==s)return},G=Ie(),he=()=>I.value>k.value,b=()=>{N.value="pointer",I.value=window.performance.now()},Q=()=>{N.value="keyboard",I.value=window.performance.now()},Se=()=>(O(()=>{F===0&&(document.addEventListener("mousedown",b),document.addEventListener("touchstart",b),document.addEventListener("keydown",Q)),F++}),R(()=>{F--,F<=0&&(document.removeEventListener("mousedown",b),document.removeEventListener("touchstart",b),document.removeEventListener("keydown",Q))}),{focusReason:N,lastUserFocusTimestamp:I,lastAutomatedFocusTimestamp:k}),y=e=>new CustomEvent(Te,{...Fe,detail:e}),we=ue({name:"ElFocusTrap",inheritAttrs:!1,props:{loop:Boolean,trapped:Boolean,focusTrapEl:Object,focusStartEl:{type:[Object,String],default:"first"}},emits:[q,J,"focusin","focusout","focusout-prevented","release-requested"],setup(e,{emit:t}){const s=C();let o,c;const{focusReason:i}=Se();me(n=>{e.trapped&&!f.paused&&t("release-requested",n)});const f={paused:!1,pause(){this.paused=!0},resume(){this.paused=!1}},m=n=>{if(!e.loop&&!e.trapped||f.paused)return;const{key:r,altKey:a,ctrlKey:u,metaKey:d,currentTarget:D,shiftKey:$}=n,{loop:j}=e,te=r===X.tab&&!a&&!u&&!d,T=document.activeElement;if(te&&T){const h=D,[S,w]=_e(h);if(S&&w){if(!$&&T===w){const l=y({focusReason:i.value});t("focusout-prevented",l),l.defaultPrevented||(n.preventDefault(),j&&v(S,!0))}else if($&&[S,h].includes(T)){const l=y({focusReason:i.value});t("focusout-prevented",l),l.defaultPrevented||(n.preventDefault(),j&&v(w,!0))}}else if(T===h){const l=y({focusReason:i.value});t("focusout-prevented",l),l.defaultPrevented||n.preventDefault()}}};ie(be,{focusTrapRef:s,onKeydown:m}),_(()=>e.focusTrapEl,n=>{n&&(s.value=n)},{immediate:!0}),_([s],([n],[r])=>{n&&(n.addEventListener("keydown",m),n.addEventListener("focusin",A),n.addEventListener("focusout",K)),r&&(r.removeEventListener("keydown",m),r.removeEventListener("focusin",A),r.removeEventListener("focusout",K))});const P=n=>{t(q,n)},ee=n=>t(J,n),A=n=>{const r=E(s);if(!r)return;const a=n.target,u=n.relatedTarget,d=a&&r.contains(a);e.trapped||u&&r.contains(u)||(o=u),d&&t("focusin",n),!f.paused&&e.trapped&&(d?c=a:v(c,!0))},K=n=>{const r=E(s);if(!(f.paused||!r))if(e.trapped){const a=n.relatedTarget;!le(a)&&!r.contains(a)&&setTimeout(()=>{if(!f.paused&&e.trapped){const u=y({focusReason:i.value});t("focusout-prevented",u),u.defaultPrevented||v(c,!0)}},0)}else{const a=n.target;a&&r.contains(a)||t("focusout",n)}};async function U(){await V();const n=E(s);if(n){G.push(f);const r=n.contains(document.activeElement)?o:document.activeElement;if(o=r,!n.contains(r)){const u=new Event(L,W);n.addEventListener(L,P),n.dispatchEvent(u),u.defaultPrevented||V(()=>{let d=e.focusStartEl;fe(d)||(v(d),document.activeElement!==d&&(d="first")),d==="first"&&Pe(Z(n),!0),(document.activeElement===r||d==="container")&&v(n)})}}}function x(){const n=E(s);if(n){n.removeEventListener(L,P);const r=new CustomEvent(g,{...W,detail:{focusReason:i.value}});n.addEventListener(g,ee),n.dispatchEvent(r),!r.defaultPrevented&&(i.value=="keyboard"||!he()||n.contains(document.activeElement))&&v(o??document.body),n.removeEventListener(g,P),G.remove(f)}}return O(()=>{e.trapped&&U(),_(()=>e.trapped,n=>{n?U():x()})}),R(()=>{e.trapped&&x()}),{onKeydown:m}}});function Le(e,t,s,o,c,i){return de(e.$slots,"default",{handleKeydown:e.onKeydown})}var De=ae(we,[["render",Le],["__file","/home/runner/work/element-plus/element-plus/packages/components/focus-trap/src/focus-trap.vue"]]);export{De as E,be as F,Ke as a,Ee as b,ke as c,Ne as d,Ue as e,xe as f,le as i,Re as t,Ae as u};