{"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 non-negative finite number\n * @throws {TypeError} onWindowResize: isAutoInit must be a boolean\r\n *\r\n * @example\n * onWindowResize(() => console.log(\"resized\"));\n * @example\n * // Recalculate a responsive grid after resize with a 200 ms debounce\n * const resize = onWindowResize(() => updateGridColumns(window.innerWidth), 200);\n * resize.removeListener();\n */\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\" || !Number.isFinite(delay) || delay < 0) {\n    throw new TypeError(\"onWindowResize: delay must be a non-negative finite number\");\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 => {\n    getWindow().removeEventListener(\"resize\", handler as EventListener);\n    const cancel = (fn as typeof fn & { cancel?: unknown; }).cancel;\n    if (typeof cancel === \"function\") {\n      cancel();\n    }\n  };\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","Number","isFinite","fn","getDebouncedFn","handler","e","addListener","getWindow","addEventListener","removeListener","removeEventListener","cancel"],"mappings":"oJA6BO,MAAMA,eAAiB,CAC5BC,GACAC,MAA4B,IAC5BC,WAAkC,QAMlC,UAAWF,KAAO,WAChB,MAAM,IAAIG,UAAU,yCAEtB,UAAWF,QAAU,WAAaG,OAAOC,SAASJ,QAAUA,MAAQ,EAClE,MAAM,IAAIE,UAAU,8DAEtB,UAAWD,aAAe,UACxB,MAAM,IAAIC,UAAU,gDAGtB,MAAMG,GAAKL,MAAQM,MAAAA,eAA0BP,GAAIC,OAASD,GAC1D,MAAMQ,QAAWC,IACfH,GAAGG,IAGL,MAAMC,YAAc,KAClBC,UAAAA,YAAYC,iBAAiB,SAAUJ,UAGzC,MAAMK,eAAiB,KACrBF,UAAAA,YAAYG,oBAAoB,SAAUN,SAC1C,MAAMO,OAAUT,GAAyCS,OACzD,UAAWA,SAAW,WACpBA,UAIJ,GAAIb,WACFQ,cAGF,MAAO,CACLF,gBACAE,wBACAG"}