"use client"
import{f as y}from"./chunk-QSMIB34W.mjs";import{createContext as S,useCallback as h,useContext as K,useEffect as d,useId as L,useRef as f,useState as k}from"react";import{jsx as m}from"react/jsx-runtime";var u=S({listeners:[],setListeners:()=>{}});function x({children:i}){let[t,s]=k([]),r=h(e=>{let a=e.target,c=document.getElementById("modal-backdrop");if(e.metaKey||e.ctrlKey||a.tagName==="INPUT"||a.tagName==="TEXTAREA"||c)return;let o=t.filter(n=>n.enabled!==!1&&(Array.isArray(n.key)?n.key.includes(e.key):n.key===e.key));if(!o.length)return;let l=y(o,(n,b)=>(b.priority??0)-(n.priority??0)).slice(-1)[0];e.preventDefault(),l.callback(e)},[t]);return d(()=>(document.addEventListener("keydown",r),()=>document.removeEventListener("keydown",r)),[r]),m(u.Provider,{value:{listeners:t,setListeners:s},children:i})}function C(i,t,s={}){let r=L(),{setListeners:e}=K(u),a=f(t);d(()=>{a.current=t},[t]),d(()=>(console.log("key",i),e(c=>[...c.filter(o=>o.id!==r),{id:r,key:i,callback:a.current,...s}]),()=>e(c=>c.filter(o=>o.id!==r))),[JSON.stringify(i),s.enabled,s.priority])}export{u as a,x as b,C as c};
