{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["import { getWindow } from \"ssr-window\";\r\nimport { getDebouncedFn } from \"../../fn/getDebouncedFn/index.ts\";\r\n\r\nexport type TOnWindowResizeArgs = Parameters<typeof onWindowResize>;\r\n\r\nexport type TOnWindowResizeReturn = ReturnType<typeof onWindowResize>;\r\n\r\n/**\r\n * Runs a callback on window resize. If `delay` is provided, the callback is debounced.\r\n *\r\n * @param {(e: Event) => void} cb Resize callback\r\n * @param {number} [delay=300] Debounce delay in ms; falsy to call immediately\r\n * @param {boolean} [isAutoInit=true] Attach immediately\r\n * @returns {{\r\n *   handler: (e: Event) => void;\r\n *   addListener: () => void;\r\n *   removeListener: () => void;\r\n * }}\r\n * @throws {TypeError} onWindowResize: cb must be a function\r\n * @throws {TypeError} onWindowResize: delay must be a number if provided\r\n * @throws {TypeError} onWindowResize: isAutoInit must be a boolean\r\n *\r\n * @example\r\n * onWindowResize(() => console.log(\"resized\"));\r\n */\r\nexport const onWindowResize = (\r\n  cb: (e: Event) => void,\r\n  delay: number | undefined = 300,\r\n  isAutoInit: boolean | undefined = true\r\n): {\r\n  handler: (e: Event) => void;\r\n  addListener: () => void;\r\n  removeListener: () => void;\r\n} => {\r\n  if (typeof cb !== \"function\") {\r\n    throw new TypeError(\"onWindowResize: cb must be a function\");\r\n  }\r\n  if (typeof delay !== \"number\" && typeof delay !== \"undefined\") {\r\n    throw new TypeError(\"onWindowResize: delay must be a number if provided\");\r\n  }\r\n  if (typeof isAutoInit !== \"boolean\") {\r\n    throw new TypeError(\"onWindowResize: isAutoInit must be a boolean\");\r\n  }\r\n\r\n  const fn = delay ? getDebouncedFn<typeof cb>(cb, delay) : cb;\r\n  const handler = (e: Event): void => {\r\n    fn(e);\r\n  };\r\n\r\n  const addListener = (): void => {\r\n    getWindow().addEventListener(\"resize\", handler as EventListener);\r\n  };\r\n\r\n  const removeListener = (): void => {\r\n    getWindow().removeEventListener(\"resize\", handler as EventListener);\r\n  };\r\n\r\n  if (isAutoInit) {\r\n    addListener();\r\n  }\r\n\r\n  return {\r\n    handler,\r\n    addListener,\r\n    removeListener,\r\n  };\r\n};\r\n"],"names":["onWindowResize","cb","delay","isAutoInit","TypeError","fn","getDebouncedFn","handler","e","addListener","getWindow","addEventListener","removeListener","removeEventListener"],"mappings":";;;;;;;;;;;;;;;;;;GAyBO,MAAMA,eAAiBA,CAC5BC,GACAC,MAA4B,IAC5BC,WAAkC,QAMlC,UAAWF,KAAO,WAChB,MAAM,IAAIG,UAAU,yCAEtB,UAAWF,QAAU,iBAAmBA,QAAU,YAChD,MAAM,IAAIE,UAAU,sDAEtB,UAAWD,aAAe,UACxB,MAAM,IAAIC,UAAU,gDAGtB,MAAMC,GAAKH,MAAQI,MAAAA,eAA0BL,GAAIC,OAASD,GAC1D,MAAMM,QAAWC,IACfH,GAAGG,IAGL,MAAMC,YAAcA,KAClBC,UAAAA,YAAYC,iBAAiB,SAAUJ,UAGzC,MAAMK,eAAiBA,KACrBF,UAAAA,YAAYG,oBAAoB,SAAUN,UAG5C,GAAIJ,WACFM,cAGF,MAAO,CACLF,gBACAE,wBACAG"}