{"version":3,"sources":["../../../../src/sensors/useOnElementRemoval/core.ts"],"sourcesContent":["import { observable } from \"@legendapp/state\";\nimport { get, createObserve } from \"@usels/core\";\nimport type { DeepMaybeObservable } from \"@usels/core\";\nimport { type ConfigurableDocumentOrShadowRoot, defaultDocument } from \"@shared/configurable\";\nimport type { MaybeEventTarget } from \"../../types\";\nimport { createMutationObserver } from \"../../elements/useMutationObserver/core\";\n\nexport type UseOnElementRemovalOptions = ConfigurableDocumentOrShadowRoot;\n\n/**\n * Framework-agnostic element-removal detector.\n *\n * Tracks the target element reactively (via `observe` on `get(target)`), then\n * watches the document/root for childList mutations. When a removed node is or\n * contains the tracked element, `callback` fires and the tracked reference is\n * cleared so the next mount → removal cycle fires again.\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function createOnElementRemoval(\n  target: MaybeEventTarget,\n  callback: (mutations: MutationRecord[]) => void,\n  options?: DeepMaybeObservable<UseOnElementRemovalOptions>\n): void {\n  const opts$ = observable(options);\n\n  // Latest non-null target element. Mirrors the original `useWhenever` behavior\n  // (only truthy transitions update the tracked ref). Cleared after detection\n  // so the next mount → removal cycle can fire again.\n  let trackedEl: Element | Document | Window | null = null;\n\n  createObserve(() => {\n    const el = get(target) as Element | Document | Window | null;\n    if (el) trackedEl = el;\n  });\n\n  const rootOpt = opts$.peek()?.document as MaybeEventTarget | undefined;\n  const root: MaybeEventTarget =\n    rootOpt ?? (defaultDocument as unknown as MaybeEventTarget) ?? null;\n\n  createMutationObserver(\n    root,\n    (mutations) => {\n      const el = trackedEl;\n      if (!el) return;\n\n      for (const mutation of mutations) {\n        for (let i = 0; i < mutation.removedNodes.length; i++) {\n          const removed = mutation.removedNodes[i];\n          if (removed === el || (removed instanceof Element && removed.contains(el as Node))) {\n            callback(mutations);\n            trackedEl = null;\n            return;\n          }\n        }\n      }\n    },\n    { childList: true, subtree: true }\n  );\n}\n"],"mappings":"AAAA,SAAS,kBAAkB;AAC3B,SAAS,KAAK,qBAAqB;AAEnC,SAAgD,uBAAuB;AAEvE,SAAS,8BAA8B;AAAA;AAahC,SAAS,uBACd,QACA,UACA,SACM;AACN,QAAM,QAAQ,WAAW,OAAO;AAKhC,MAAI,YAAgD;AAEpD,gBAAc,MAAM;AAClB,UAAM,KAAK,IAAI,MAAM;AACrB,QAAI,GAAI,aAAY;AAAA,EACtB,CAAC;AAED,QAAM,UAAU,MAAM,KAAK,GAAG;AAC9B,QAAM,OACJ,WAAY,mBAAmD;AAEjE;AAAA,IACE;AAAA,IACA,CAAC,cAAc;AACb,YAAM,KAAK;AACX,UAAI,CAAC,GAAI;AAET,iBAAW,YAAY,WAAW;AAChC,iBAAS,IAAI,GAAG,IAAI,SAAS,aAAa,QAAQ,KAAK;AACrD,gBAAM,UAAU,SAAS,aAAa,CAAC;AACvC,cAAI,YAAY,MAAO,mBAAmB,WAAW,QAAQ,SAAS,EAAU,GAAI;AAClF,qBAAS,SAAS;AAClB,wBAAY;AACZ;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,EAAE,WAAW,MAAM,SAAS,KAAK;AAAA,EACnC;AACF;","names":[]}