{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["export type TOnControls<TEvent extends Event = Event> = {\n  addListener: () => void;\n  handler: (event: TEvent) => void;\n  removeListener: () => void;\n};\n\nexport type TOnArgs = Parameters<typeof on>;\n\nexport type TOnReturn = ReturnType<typeof on>;\n\n/**\n * Creates a managed event listener with explicit add and remove controls.\n * @template TEvent\n * @param {EventTarget} target Event target\n * @param {string} type Event type\n * @param {(event: TEvent) => void} callback Event callback\n * @param {boolean|AddEventListenerOptions} [options={}] Native listener options\n * @param {boolean} [isAutoInit=true] Attach immediately\n * @returns {TOnControls<TEvent>} Listener controls\n * @throws {TypeError} on: arguments are invalid\n * @example\n * const listener = on(window, \"online\", () => sync());\n * listener.removeListener();\n * @example\n * // Attach a keyboard shortcut now and remove it during component cleanup\n * const escapeKey = on<KeyboardEvent>(document, \"keydown\", (event) => {\n *   if (event.key === \"Escape\") closeDialog();\n * });\n * escapeKey.removeListener();\n */\nexport const on = <TEvent extends Event = Event>(\n  target: EventTarget,\n  type: string,\n  callback: (event: TEvent) => void,\n  options: boolean | AddEventListenerOptions = {},\n  isAutoInit: boolean = true\n): TOnControls<TEvent> => {\n  if (!target || typeof target.addEventListener !== \"function\") {\n    throw new TypeError(\"on: target must be an EventTarget\");\n  }\n  if (typeof type !== \"string\" || type.length === 0) {\n    throw new TypeError(\"on: type must be a non-empty string\");\n  }\n  if (typeof callback !== \"function\") {\n    throw new TypeError(\"on: callback must be a function\");\n  }\n  if (typeof options !== \"boolean\" && (!options || typeof options !== \"object\" || Array.isArray(options))) {\n    throw new TypeError(\"on: options must be a boolean or listener options\");\n  }\n  if (typeof isAutoInit !== \"boolean\") {\n    throw new TypeError(\"on: isAutoInit must be a boolean\");\n  }\n\n  const handler = (event: TEvent): void => callback(event);\n  const addListener = (): void => {\n    target.addEventListener(type, handler as EventListener, options);\n  };\n  const removeListener = (): void => {\n    target.removeEventListener(type, handler as EventListener, options);\n  };\n  if (isAutoInit) {\n    addListener();\n  }\n  return { addListener, handler, removeListener };\n};\n"],"names":["on","target","type","callback","options","isAutoInit","addEventListener","TypeError","length","Array","isArray","handler","event","addListener","removeListener","removeEventListener"],"mappings":"MA8BaA,GAAK,CAChBC,OACAC,KACAC,SACAC,QAA6C,CAAA,EAC7CC,WAAsB,QAEtB,IAAKJ,eAAiBA,OAAOK,mBAAqB,WAChD,MAAM,IAAIC,UAAU,qCAEtB,UAAWL,OAAS,UAAYA,KAAKM,SAAW,EAC9C,MAAM,IAAID,UAAU,uCAEtB,UAAWJ,WAAa,WACtB,MAAM,IAAII,UAAU,mCAEtB,UAAWH,UAAY,aAAeA,gBAAkBA,UAAY,UAAYK,MAAMC,QAAQN,UAC5F,MAAM,IAAIG,UAAU,qDAEtB,UAAWF,aAAe,UACxB,MAAM,IAAIE,UAAU,oCAGtB,MAAMI,QAAWC,OAAwBT,SAASS,OAClD,MAAMC,YAAc,KAClBZ,OAAOK,iBAAiBJ,KAAMS,QAA0BP,UAE1D,MAAMU,eAAiB,KACrBb,OAAOc,oBAAoBb,KAAMS,QAA0BP,UAE7D,GAAIC,WACFQ,cAEF,MAAO,CAAEA,wBAAaF,gBAASG"}