{"version":3,"sources":["../../src/useInView/utils.ts"],"names":["observerMap","RootIds","rootId","unsupportedValue","defaultFallbackInView","inView","getRootId","root","optionsToId","options","key","createObserver","id","instance","elements","thresholds","observer","entries","entry","_a","threshold","callback","observe","element","fallbackInView","bounds","callbacks"],"mappings":"8BAGA,MAAMA,EAAc,IAAI,IASlBC,EAA+C,IAAI,QAEzD,IAAIC,EAAS,EAETC,EAEG,MAAMC,EAAyBC,GAAsC,CAC1EF,EAAmBE,CACrB,EAEMC,EAAaC,GACZA,GAIDN,EAAQ,IAAIM,CAAI,IAIpBL,GAAU,EAEVD,EAAQ,IAAIM,EAAML,EAAO,SAAS,CAAC,GAE5BD,EAAQ,IAAIM,CAAI,GAXd,IAcEC,EAAeC,GACzB,OAAO,KAAKA,CAAO,EACjB,KAAK,EACL,OAAOC,GAAOD,EAAQC,CAAG,IAAM,MAAS,EACxC,IAAIA,GACI,GAAGA,KAAOA,IAAQ,OAASJ,EAAUG,EAAQ,IAAI,EAAIA,EAAQC,CAAG,GACxE,EACA,SAAS,EAERC,EAAkBF,GAAsC,CAC5D,MAAMG,EAAKJ,EAAYC,CAAO,EAE9B,IAAII,EAAWb,EAAY,IAAIY,CAAE,EAEjC,GAAI,CAACC,EAAU,CACb,MAAMC,EAAW,IAAI,IAErB,IAAIC,EAA2C,CAAC,EAEhD,MAAMC,EAAW,IAAI,qBAAqBC,GAAW,CACnDA,EAAQ,QAAQC,GAAS,CA1D/B,IAAAC,EA2DQ,MAAMd,EACJa,EAAM,gBACNH,EAAW,KAAKK,GAAaF,EAAM,mBAAqBE,CAAS,EAE9DX,EAAgB,iBAAmB,OAAQS,EAAc,WAAc,cACzEA,EAAc,UAAYb,IAG7Bc,EAAAL,EAAS,IAAII,EAAM,MAAM,IAAzB,MAAAC,EAA4B,QAAQE,GAAY,CAC9CA,EAAShB,EAAQa,CAAK,CACxB,EACF,CAAC,CACH,EAAGT,CAAO,EAEVM,EACEC,EAAS,aACR,MAAM,QAAQP,EAAQ,SAAS,EAAIA,EAAQ,UAAY,CAACA,EAAQ,WAAa,CAAC,GAEjFI,EAAW,CACT,GAAAD,EACA,SAAAI,EACA,SAAAF,CACF,EAEAd,EAAY,IAAIY,EAAIC,CAAQ,EAG9B,OAAOA,CACT,EAEaS,EAAU,CACrBC,EACAF,EACAZ,EAAoC,CAAC,EACrCe,EAAiBrB,IACG,CACpB,GAAI,OAAO,OAAO,sBAAyB,aAAeqB,IAAmB,OAAW,CACtF,MAAMC,EAASF,EAAQ,sBAAsB,EAE7C,OAAAF,EAASG,EAAgB,CACvB,eAAgBA,EAChB,OAAQD,EACR,kBAAmB,OAAOd,EAAQ,WAAc,SAAWA,EAAQ,UAAY,EAC/E,KAAM,EACN,mBAAoBgB,EACpB,iBAAkBA,EAClB,WAAYA,CACd,CAAC,EAEM,IAAG,GAIZ,KAAM,CAAE,GAAAb,EAAI,SAAAI,EAAU,SAAAF,CAAS,EAAIH,EAAeF,CAAO,EAGnDiB,EAAYZ,EAAS,IAAIS,CAAO,GAAK,CAAC,EAE5C,OAAKT,EAAS,IAAIS,CAAO,GACvBT,EAAS,IAAIS,EAASG,CAAS,EAGjCA,EAAU,KAAKL,CAAQ,EACvBL,EAAS,QAAQO,CAAO,EAEjB,IAAM,CAEXG,EAAU,OAAOA,EAAU,QAAQL,CAAQ,EAAG,CAAC,EAE3CK,EAAU,SAAW,IAEvBZ,EAAS,OAAOS,CAAO,EACvBP,EAAS,UAAUO,CAAO,GAGxBT,EAAS,OAAS,IACpBE,EAAS,WAAW,EACpBhB,EAAY,OAAOY,CAAE,EAEzB,CACF","sourcesContent":["import type { GeneralFunction } from '@asherng/common-types';\nimport type { ObserverInstanceCallback } from './types';\n\nconst observerMap = new Map<\n  string,\n  {\n    id: string;\n    observer: IntersectionObserver;\n    elements: Map<Element, Array<ObserverInstanceCallback>>;\n  }\n>();\n\nconst RootIds: WeakMap<Element | Document, string> = new WeakMap();\n\nlet rootId = 0;\n\nlet unsupportedValue: boolean | undefined = undefined;\n\nexport const defaultFallbackInView = (inView: boolean | undefined): void => {\n  unsupportedValue = inView;\n};\n\nconst getRootId = (root: IntersectionObserverInit['root']) => {\n  if (!root) {\n    return '0';\n  }\n\n  if (RootIds.has(root)) {\n    return RootIds.get(root);\n  }\n\n  rootId += 1;\n\n  RootIds.set(root, rootId.toString());\n\n  return RootIds.get(root);\n};\n\nexport const optionsToId = (options: IntersectionObserverInit): string =>\n  (Object.keys(options) as (keyof IntersectionObserverInit)[])\n    .sort()\n    .filter(key => options[key] !== undefined)\n    .map(key => {\n      return `${key}_${key === 'root' ? getRootId(options.root) : options[key]}`;\n    })\n    .toString();\n\nconst createObserver = (options: IntersectionObserverInit) => {\n  const id = optionsToId(options);\n\n  let instance = observerMap.get(id);\n\n  if (!instance) {\n    const elements = new Map<Element, Array<ObserverInstanceCallback>>();\n\n    let thresholds: number[] | readonly number[] = [];\n\n    const observer = new IntersectionObserver(entries => {\n      entries.forEach(entry => {\n        const inView =\n          entry.isIntersecting &&\n          thresholds.some(threshold => entry.intersectionRatio >= threshold);\n\n        if ((options as any).trackVisibility && typeof (entry as any).isVisible === 'undefined') {\n          (entry as any).isVisible = inView;\n        }\n\n        elements.get(entry.target)?.forEach(callback => {\n          callback(inView, entry);\n        });\n      });\n    }, options);\n\n    thresholds =\n      observer.thresholds ||\n      (Array.isArray(options.threshold) ? options.threshold : [options.threshold || 0]);\n\n    instance = {\n      id,\n      observer,\n      elements\n    };\n\n    observerMap.set(id, instance);\n  }\n\n  return instance;\n};\n\nexport const observe = <E extends Element>(\n  element: E,\n  callback: ObserverInstanceCallback,\n  options: IntersectionObserverInit = {},\n  fallbackInView = unsupportedValue\n): GeneralFunction => {\n  if (typeof window.IntersectionObserver === 'undefined' && fallbackInView !== undefined) {\n    const bounds = element.getBoundingClientRect();\n\n    callback(fallbackInView, {\n      isIntersecting: fallbackInView,\n      target: element,\n      intersectionRatio: typeof options.threshold === 'number' ? options.threshold : 0,\n      time: 0,\n      boundingClientRect: bounds,\n      intersectionRect: bounds,\n      rootBounds: bounds\n    });\n\n    return () => undefined;\n  }\n\n  // An observer with the same options can be reused, so lets use this fact\n  const { id, observer, elements } = createObserver(options);\n\n  // Register the callback listener for this element\n  const callbacks = elements.get(element) || [];\n\n  if (!elements.has(element)) {\n    elements.set(element, callbacks);\n  }\n\n  callbacks.push(callback);\n  observer.observe(element);\n\n  return () => {\n    // Remove the callback from the callback list\n    callbacks.splice(callbacks.indexOf(callback), 1);\n\n    if (callbacks.length === 0) {\n      // No more callback exists for element, so destroy it\n      elements.delete(element);\n      observer.unobserve(element);\n    }\n\n    if (elements.size === 0) {\n      observer.disconnect();\n      observerMap.delete(id);\n    }\n  };\n};\n"]}