{"version":3,"sources":["../../../../src/sensors/useMouse/core.ts"],"sourcesContent":["import { observable } from \"@legendapp/state\";\nimport { type DeepMaybeObservable, type ReadonlyObservable, peek } from \"@usels/core\";\nimport type { ConfigurableEventFilter } from \"@usels/core\";\nimport { createFilterWrapper } from \"@usels/core/shared/filters\";\nimport { defaultWindow, type ConfigurableWindow } from \"@shared/configurable\";\nimport type { MaybeEventTarget } from \"../../types\";\nimport { createEventListener } from \"../../browser/useEventListener/core\";\n\nexport type UseMouseCoordType = \"page\" | \"client\" | \"screen\" | \"movement\";\n\nexport type UseMouseSourceType = \"mouse\" | \"touch\" | null;\n\nexport interface UseMouseOptions extends ConfigurableEventFilter, ConfigurableWindow {\n  /** Coordinate type. Default: \"page\" */\n  type?: UseMouseCoordType;\n  /** Track touch events. Default: true */\n  touch?: boolean;\n  /** Reset coordinates on touchend. Default: false */\n  resetOnTouchEnds?: boolean;\n  /** Event target. Default: window */\n  target?: MaybeEventTarget;\n  /** Initial coordinate values. Default: { x: 0, y: 0 } */\n  initialValue?: { x: number; y: number };\n}\n\nexport interface UseMouseReturn {\n  /** X coordinate */\n  x$: ReadonlyObservable<number>;\n  /** Y coordinate */\n  y$: ReadonlyObservable<number>;\n  /** Event source type: \"mouse\" | \"touch\" | null */\n  sourceType$: ReadonlyObservable<UseMouseSourceType>;\n}\n\n/**\n * Framework-agnostic mouse/touch position tracker.\n *\n * Monitors `mousemove` and optionally `touchstart`/`touchmove`/`touchend`\n * events on a target (default: window) and exposes reactive coordinates.\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function createMouse(options?: DeepMaybeObservable<UseMouseOptions>): UseMouseReturn {\n  // All options are mount-time-only — read once via peek(), no observable()\n  // wrapping needed. Avoiding observable() prevents Legend-State from linking\n  // the Ref$ target into its reactive tree, which would break React ref cleanup.\n  const raw = peek(options) as UseMouseOptions | undefined;\n\n  const type = raw?.type ?? \"page\";\n  const touch = raw?.touch ?? true;\n  const resetOnTouchEnds = raw?.resetOnTouchEnds ?? false;\n  const initial = raw?.initialValue ?? { x: 0, y: 0 };\n  const eventFilter = raw?.eventFilter as UseMouseOptions[\"eventFilter\"] | undefined;\n\n  // Resolve event target — read from raw options to preserve Ref$/Observable\n  // reference identity. createEventListener tracks it reactively via normalizeTargets.\n  const rawTarget = raw?.target as MaybeEventTarget | undefined;\n  const eventTarget: MaybeEventTarget = rawTarget ?? (defaultWindow as MaybeEventTarget) ?? null;\n\n  const x$ = observable(initial.x);\n  const y$ = observable(initial.y);\n  const sourceType$ = observable<UseMouseSourceType>(null);\n\n  // Coordinate extraction helper\n  const extractCoords = (e: MouseEvent | Touch): { x: number; y: number } => {\n    switch (type) {\n      case \"page\":\n        return { x: e.pageX, y: e.pageY };\n      case \"client\":\n        return { x: e.clientX, y: e.clientY };\n      case \"screen\":\n        return { x: e.screenX, y: e.screenY };\n      case \"movement\":\n      default:\n        return \"movementX\" in e\n          ? { x: (e as MouseEvent).movementX, y: (e as MouseEvent).movementY }\n          : { x: 0, y: 0 };\n    }\n  };\n\n  // mousemove — wrapped with createFilterWrapper if eventFilter is provided\n  const onMouseMove = (() => {\n    const raw = (e: MouseEvent) => {\n      const { x, y } = extractCoords(e);\n      x$.set(x);\n      y$.set(y);\n      sourceType$.set(\"mouse\");\n    };\n    if (!eventFilter) return raw;\n    return createFilterWrapper(eventFilter, raw);\n  })();\n\n  createEventListener(eventTarget, \"mousemove\", onMouseMove, { passive: true });\n\n  // touch events (conditional — null target to disable)\n  const touchTarget: MaybeEventTarget = touch ? eventTarget : null;\n\n  const onTouchMove = (() => {\n    const raw = (e: TouchEvent) => {\n      if (e.touches.length === 0) return;\n      const t = e.touches[0];\n      const { x, y } = extractCoords(t);\n      x$.set(x);\n      y$.set(y);\n      sourceType$.set(\"touch\");\n    };\n    if (!eventFilter) return raw;\n    return createFilterWrapper(eventFilter, raw);\n  })();\n\n  // touchend is a reset action — not filtered\n  const onTouchEnd = () => {\n    if (resetOnTouchEnds) {\n      x$.set(initial.x);\n      y$.set(initial.y);\n    }\n  };\n\n  createEventListener(touchTarget, \"touchstart\", onTouchMove, { passive: true });\n  createEventListener(touchTarget, \"touchmove\", onTouchMove, { passive: true });\n  createEventListener(touchTarget, \"touchend\", onTouchEnd, { passive: true });\n\n  return { x$, y$, sourceType$ };\n}\n"],"mappings":"AAAA,SAAS,kBAAkB;AAC3B,SAA4D,YAAY;AAExE,SAAS,2BAA2B;AACpC,SAAS,qBAA8C;AAEvD,SAAS,2BAA2B;AAAA;AAmC7B,SAAS,YAAY,SAAgE;AAI1F,QAAM,MAAM,KAAK,OAAO;AAExB,QAAM,OAAO,KAAK,QAAQ;AAC1B,QAAM,QAAQ,KAAK,SAAS;AAC5B,QAAM,mBAAmB,KAAK,oBAAoB;AAClD,QAAM,UAAU,KAAK,gBAAgB,EAAE,GAAG,GAAG,GAAG,EAAE;AAClD,QAAM,cAAc,KAAK;AAIzB,QAAM,YAAY,KAAK;AACvB,QAAM,cAAgC,aAAc,iBAAsC;AAE1F,QAAM,KAAK,WAAW,QAAQ,CAAC;AAC/B,QAAM,KAAK,WAAW,QAAQ,CAAC;AAC/B,QAAM,cAAc,WAA+B,IAAI;AAGvD,QAAM,gBAAgB,CAAC,MAAoD;AACzE,YAAQ,MAAM;AAAA,MACZ,KAAK;AACH,eAAO,EAAE,GAAG,EAAE,OAAO,GAAG,EAAE,MAAM;AAAA,MAClC,KAAK;AACH,eAAO,EAAE,GAAG,EAAE,SAAS,GAAG,EAAE,QAAQ;AAAA,MACtC,KAAK;AACH,eAAO,EAAE,GAAG,EAAE,SAAS,GAAG,EAAE,QAAQ;AAAA,MACtC,KAAK;AAAA,MACL;AACE,eAAO,eAAe,IAClB,EAAE,GAAI,EAAiB,WAAW,GAAI,EAAiB,UAAU,IACjE,EAAE,GAAG,GAAG,GAAG,EAAE;AAAA,IACrB;AAAA,EACF;AAGA,QAAM,eAAe,MAAM;AACzB,UAAMA,OAAM,CAAC,MAAkB;AAC7B,YAAM,EAAE,GAAG,EAAE,IAAI,cAAc,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