{"version":3,"sources":["../../../../src/sensors/useDevicePixelRatio/core.ts"],"sourcesContent":["import { observable, type Observable } from \"@legendapp/state\";\nimport {\n  createSupported,\n  onMount,\n  type DeepMaybeObservable,\n  type ReadonlyObservable,\n  type Supportable,\n} from \"@usels/core\";\nimport { resolveWindowSource, type ConfigurableWindow } from \"@shared/configurable\";\n\nexport type UseDevicePixelRatioOptions = ConfigurableWindow;\n\nexport interface UseDevicePixelRatioReturn extends Supportable {\n  /** Current device pixel ratio */\n  pixelRatio$: ReadonlyObservable<number>;\n}\n\n/**\n * Framework-agnostic reactive `window.devicePixelRatio` tracker.\n *\n * Uses `matchMedia('(resolution: Ndppx)')` and re-registers the listener\n * whenever the DPR changes so callers always see the latest value.\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function createDevicePixelRatio(\n  options?: DeepMaybeObservable<UseDevicePixelRatioOptions>\n): UseDevicePixelRatioReturn {\n  const opts$ = observable(options);\n  const win$ = resolveWindowSource(opts$.window as unknown as Observable<unknown>);\n\n  const isSupported$ = createSupported(() => {\n    const win = win$.get();\n    return !!win && \"matchMedia\" in win;\n  });\n\n  const pixelRatio$ = observable<number>(win$.peek()?.devicePixelRatio ?? 1);\n\n  onMount(() => {\n    const win = win$.peek();\n    if (!win || !win.matchMedia) return;\n\n    let cleanup: (() => void) | undefined;\n\n    const update = () => {\n      pixelRatio$.set(win.devicePixelRatio ?? 1);\n      cleanup?.();\n      const media = win.matchMedia(`(resolution: ${win.devicePixelRatio}dppx)`);\n      const handler = () => update();\n      media.addEventListener(\"change\", handler, { once: true });\n      cleanup = () => media.removeEventListener(\"change\", handler);\n    };\n\n    update();\n\n    return () => {\n      cleanup?.();\n    };\n  });\n\n  return {\n    isSupported$,\n    pixelRatio$: pixelRatio$ as ReadonlyObservable<number>,\n  };\n}\n"],"mappings":"AAAA,SAAS,kBAAmC;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AACP,SAAS,2BAAoD;AAAA;AAgBtD,SAAS,uBACd,SAC2B;AAC3B,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,OAAO,oBAAoB,MAAM,MAAwC;AAE/E,QAAM,eAAe,gBAAgB,MAAM;AACzC,UAAM,MAAM,KAAK,IAAI;AACrB,WAAO,CAAC,CAAC,OAAO,gBAAgB;AAAA,EAClC,CAAC;AAED,QAAM,cAAc,WAAmB,KAAK,KAAK,GAAG,oBAAoB,CAAC;AAEzE,UAAQ,MAAM;AACZ,UAAM,MAAM,KAAK,KAAK;AACtB,QAAI,CAAC,OAAO,CAAC,IAAI,WAAY;AAE7B,QAAI;AAEJ,UAAM,SAAS,MAAM;AACnB,kBAAY,IAAI,IAAI,oBAAoB,CAAC;AACzC,gBAAU;AACV,YAAM,QAAQ,IAAI,WAAW,gBAAgB,IAAI,gBAAgB,OAAO;AACxE,YAAM,UAAU,MAAM,OAAO;AAC7B,YAAM,iBAAiB,UAAU,SAAS,EAAE,MAAM,KAAK,CAAC;AACxD,gBAAU,MAAM,MAAM,oBAAoB,UAAU,OAAO;AAAA,IAC7D;AAEA,WAAO;AAEP,WAAO,MAAM;AACX,gBAAU;AAAA,IACZ;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}