import { addPointerEvent } from '../../events/add-pointer-event.mjs';
import { getContextWindow } from '../../utils/get-context-window.mjs';
import { PanSession } from './PanSession.mjs';
import { Feature } from '../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/render/Feature.mjs';
import { noop } from '../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/noop.mjs';
import { frame } from '../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/frameloop/frame.mjs';

const asyncHandler = (handler) => (event, info) => {
    if (handler) {
        frame.update(() => handler(event, info), false, true);
    }
};
class PanGesture extends Feature {
    constructor() {
        super(...arguments);
        this.removePointerDownListener = noop;
    }
    onPointerDown(pointerDownEvent) {
        this.session = new PanSession(pointerDownEvent, this.createPanHandlers(), {
            transformPagePoint: this.node.getTransformPagePoint(),
            contextWindow: getContextWindow(this.node),
        });
    }
    createPanHandlers() {
        const { onPanSessionStart, onPanStart, onPan, onPanEnd } = this.node.getProps();
        return {
            onSessionStart: asyncHandler(onPanSessionStart),
            onStart: asyncHandler(onPanStart),
            onMove: asyncHandler(onPan),
            onEnd: (event, info) => {
                delete this.session;
                if (onPanEnd) {
                    frame.postRender(() => onPanEnd(event, info));
                }
            },
        };
    }
    mount() {
        this.removePointerDownListener = addPointerEvent(this.node.current, "pointerdown", (event) => this.onPointerDown(event));
    }
    update() {
        this.session && this.session.updateHandlers(this.createPanHandlers());
    }
    unmount() {
        this.removePointerDownListener();
        this.session && this.session.end();
    }
}

export { PanGesture };
