"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../../events/add-pointer-event.mjs.js"),e=require("../../events/event-info.mjs.js"),s=require("../../utils/distance.mjs.js"),n=require("../../../../../motion-dom/dist/es/frameloop/frame.mjs.js"),o=require("../../../../../motion-dom/dist/es/gestures/utils/is-primary-pointer.mjs.js"),i=require("../../../../../motion-utils/dist/es/pipe.mjs.js"),r=require("../../../../../motion-utils/dist/es/time-conversion.mjs.js");const l=new Set(["auto","scroll"]);function a(t,e){return e?{point:e(t.point)}:t}function h(t,e){return{x:t.x-e.x,y:t.y-e.y}}function c(t,e){let{point:s}=t;return{point:s,delta:h(s,v(e)),offset:h(s,d(e)),velocity:m(e,.1)}}function d(t){return t[0]}function v(t){return t[t.length-1]}function m(t,e){if(t.length<2)return{x:0,y:0};let s=t.length-1,n=null;const o=v(t);for(;s>=0&&(n=t[s],!(o.timestamp-n.timestamp>r.secondsToMilliseconds(e)));)s--;if(!n)return{x:0,y:0};n===t[0]&&t.length>2&&o.timestamp-n.timestamp>2*r.secondsToMilliseconds(e)&&(n=t[1]);const i=r.millisecondsToSeconds(o.timestamp-n.timestamp);if(0===i)return{x:0,y:0};const l={x:(o.x-n.x)/i,y:(o.y-n.y)/i};return l.x===1/0&&(l.x=0),l.y===1/0&&(l.y=0),l}exports.PanSession=class{constructor(r,l){let{transformPagePoint:h,contextWindow:d=window,dragSnapToOrigin:v=!1,distanceThreshold:m=3,element:p}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(this.startEvent=null,this.lastMoveEvent=null,this.lastMoveEventInfo=null,this.lastRawMoveEventInfo=null,this.handlers={},this.contextWindow=window,this.scrollPositions=new Map,this.removeScrollListeners=null,this.onElementScroll=t=>{this.handleScroll(t.target)},this.onWindowScroll=()=>{this.handleScroll(window)},this.updatePoint=()=>{if(!this.lastMoveEvent||!this.lastMoveEventInfo)return;this.lastRawMoveEventInfo&&(this.lastMoveEventInfo=a(this.lastRawMoveEventInfo,this.transformPagePoint));const t=c(this.lastMoveEventInfo,this.history),e=null!==this.startEvent,o=s.distance2D(t.offset,{x:0,y:0})>=this.distanceThreshold;if(!e&&!o)return;const{point:i}=t,{timestamp:r}=n.frameData;this.history.push({...i,timestamp:r});const{onStart:l,onMove:h}=this.handlers;e||(l&&l(this.lastMoveEvent,t),this.startEvent=this.lastMoveEvent),h&&h(this.lastMoveEvent,t)},this.handlePointerMove=(t,e)=>{this.lastMoveEvent=t,this.lastRawMoveEventInfo=e,this.lastMoveEventInfo=a(e,this.transformPagePoint),n.frame.update(this.updatePoint,!0)},this.handlePointerUp=(t,e)=>{this.end();const{onEnd:s,onSessionEnd:n,resumeAnimation:o}=this.handlers;if(!this.dragSnapToOrigin&&this.startEvent||o&&o(),!this.lastMoveEvent||!this.lastMoveEventInfo)return;const i=c("pointercancel"===t.type?this.lastMoveEventInfo:a(e,this.transformPagePoint),this.history);this.startEvent&&s&&s(t,i),n&&n(t,i)},!o.isPrimaryPointer(r))return;this.dragSnapToOrigin=v,this.handlers=l,this.transformPagePoint=h,this.distanceThreshold=m,this.contextWindow=d||window;const u=a(e.extractEventInfo(r),this.transformPagePoint),{point:f}=u,{timestamp:w}=n.frameData;this.history=[{...f,timestamp:w}];const{onSessionStart:E}=l;E&&E(r,c(u,this.history)),this.removeListeners=i.pipe(t.addPointerEvent(this.contextWindow,"pointermove",this.handlePointerMove),t.addPointerEvent(this.contextWindow,"pointerup",this.handlePointerUp),t.addPointerEvent(this.contextWindow,"pointercancel",this.handlePointerUp)),p&&this.startScrollTracking(p)}startScrollTracking(t){let e=t.parentElement;for(;e;){const t=getComputedStyle(e);(l.has(t.overflowX)||l.has(t.overflowY))&&this.scrollPositions.set(e,{x:e.scrollLeft,y:e.scrollTop}),e=e.parentElement}this.scrollPositions.set(window,{x:window.scrollX,y:window.scrollY}),window.addEventListener("scroll",this.onElementScroll,{capture:!0}),window.addEventListener("scroll",this.onWindowScroll),this.removeScrollListeners=()=>{window.removeEventListener("scroll",this.onElementScroll,{capture:!0}),window.removeEventListener("scroll",this.onWindowScroll)}}handleScroll(t){const e=this.scrollPositions.get(t);if(!e)return;const s=t===window,o=s?{x:window.scrollX,y:window.scrollY}:{x:t.scrollLeft,y:t.scrollTop},i=o.x-e.x,r=o.y-e.y;0===i&&0===r||(s?this.lastMoveEventInfo&&(this.lastMoveEventInfo.point.x+=i,this.lastMoveEventInfo.point.y+=r):this.history.length>0&&(this.history[0].x-=i,this.history[0].y-=r),this.scrollPositions.set(t,o),n.frame.update(this.updatePoint,!0))}updateHandlers(t){this.handlers=t}end(){this.removeListeners&&this.removeListeners(),this.removeScrollListeners&&this.removeScrollListeners(),this.scrollPositions.clear(),n.cancelFrame(this.updatePoint)}};
//# sourceMappingURL=PanSession.mjs.js.map
