// #region Imports
import { emit, on } from '@breadstone/mosaik-elements';
import { ControllerBase } from './Abstracts/ControllerBase';
/**
 * Controller to recognize pan gestures, allowing users to drag or swipe elements on the screen.
 *
 * @public
 */
export class PanController extends ControllerBase {
    // #region Fields
    _startX;
    _startY;
    _currentX;
    _currentY;
    _isPointerDown;
    _pointerDownEventListenerSubscription;
    _pointerMoveEventListenerSubscription;
    _pointerUpEventListenerSubscription;
    _pointerCancelEventListenerSubscription;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `PanController` class.
     *
     * @public
     */
    constructor(host) {
        super(host);
        this._startX = 0;
        this._startY = 0;
        this._currentX = 0;
        this._currentY = 0;
        this._isPointerDown = false;
        this._pointerDownEventListenerSubscription = null;
        this._pointerMoveEventListenerSubscription = null;
        this._pointerUpEventListenerSubscription = null;
        this._pointerCancelEventListenerSubscription = null;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    hostConnected() {
        this._pointerDownEventListenerSubscription = on(this.host, 'pointerdown', (e) => this.onPointerDown(e));
    }
    /**
     * @public
     * @override
     */
    hostDisconnected() {
        this._pointerDownEventListenerSubscription?.dispose();
        this._pointerMoveEventListenerSubscription?.dispose();
        this._pointerUpEventListenerSubscription?.dispose();
        this._pointerCancelEventListenerSubscription?.dispose();
    }
    onPointerDown(event) {
        if (!this._isPointerDown) {
            this._isPointerDown = true;
            this._startX = event.clientX;
            this._startY = event.clientY;
            this._currentX = this._startX;
            this._currentY = this._startY;
            this._pointerMoveEventListenerSubscription = on(this.host, 'pointermove', (e) => this.onPointerMove(e));
            this._pointerUpEventListenerSubscription = on(this.host, 'pointerup', (e) => this.onPointerUp(e));
            this._pointerCancelEventListenerSubscription = on(this.host, 'pointercancel', (e) => this.onPointerUp(e));
            emit(this.host, 'panstart', {
                clientX: this._startX,
                clientY: this._startY
            });
        }
    }
    onPointerMove(event) {
        this._currentX = event.clientX;
        this._currentY = event.clientY;
        const deltaX = this._currentX - this._startX;
        const deltaY = this._currentY - this._startY;
        emit(this.host, 'panmove', {
            clientX: this._currentX,
            clientY: this._currentY,
            deltaX: deltaX,
            deltaY: deltaY
        });
    }
    onPointerUp(_event) {
        this._pointerMoveEventListenerSubscription?.dispose();
        this._pointerUpEventListenerSubscription?.dispose();
        this._pointerCancelEventListenerSubscription?.dispose();
        emit(this.host, 'panend', {
            clientX: this._currentX,
            clientY: this._currentY
        });
        this._isPointerDown = false;
    }
}
//# sourceMappingURL=PanController.js.map