import{useCallback,useEffect,useLayoutEffect,useRef}from"react";function getOS(){let e=window.navigator.userAgent.toLowerCase(),t=null;return/(macintosh|macintel|macppc|mac68k|macos)/i.test(e)?t="macos":/(iphone|ipad|ipod)/i.test(e)?t="ios":/(win32|win64|windows|wince)/i.test(e)?t="windows":/android/.test(e)?t="android":!t&&/linux/.test(e)&&(t="linux"),t}const useKeyPress=(e,t=!1,n,s)=>{const o=getOS(),r=useRef(n);useLayoutEffect((()=>{r.current=n}));const a=useCallback((n=>{e.some((e=>n.key===e))&&(t?"ctrl"===t&&(n.ctrlKey||"macos"===o&&n.metaKey)?(1===e.length&&"s"===e[0]&&n.preventDefault(),r.current(n)):("shift"===t&&n.shiftKey||"alt"===t&&n.altKey||"meta"===t&&n.metaKey)&&r.current(n):r.current(n))}),[e]);useEffect((()=>{const e=s??[document];return e&&e.length&&e.forEach((e=>{e&&e.addEventListener("keydown",a)})),()=>{e&&e.length&&e.forEach((e=>{e&&e.removeEventListener("keydown",a)}))}}),[a,s])};export default useKeyPress;