// #region Imports
/**
 * Handles touch-specific optimizations for better mobile experience.
 *
 * @internal
 */
export class TouchOptimizationHandler {
    // #region Fields
    _config;
    _initialTouchPosition = null;
    _hasTouchMoved = false;
    // #endregion
    // #region Ctor
    /**
     * Create a new TouchOptimizationHandler.
     *
     * @param config - Configuration.
     */
    constructor(config) {
        this._config = this._applyDefaults(config);
    }
    // #endregion
    // #region Methods
    /**
     * Handle touch start.
     *
     * @param event - Touch or pointer event.
     * @returns Whether touch should be tracked.
     *
     * @public
     */
    onTouchStart(event) {
        const touch = this._getTouchPoint(event);
        this._initialTouchPosition = touch;
        this._hasTouchMoved = false;
        // Haptic feedback
        if (this._config.hapticFeedback) {
            this._triggerHaptic('light');
        }
        return true;
    }
    /**
     * Handle touch move.
     *
     * @param event - Touch or pointer event.
     * @returns Whether touch is considered a drag.
     *
     * @public
     */
    onTouchMove(event) {
        if (!this._initialTouchPosition) {
            return false;
        }
        const touch = this._getTouchPoint(event);
        const distance = this._getDistance(this._initialTouchPosition, touch);
        // Check if moved enough to be a drag
        if (!this._hasTouchMoved && distance > this._config.touchMoveThreshold) {
            this._hasTouchMoved = true;
            // Prevent scrolling once drag is initiated
            if (this._config.preventDefault) {
                event.preventDefault();
            }
            // Haptic feedback on drag start
            if (this._config.hapticFeedback) {
                this._triggerHaptic('medium');
            }
        }
        return this._hasTouchMoved;
    }
    /**
     * Handle touch end.
     *
     * @public
     */
    onTouchEnd() {
        // Haptic feedback on drop
        if (this._config.hapticFeedback && this._hasTouchMoved) {
            this._triggerHaptic('light');
        }
        this._initialTouchPosition = null;
        this._hasTouchMoved = false;
    }
    /**
     * Get touch start delay.
     *
     * @returns Delay in milliseconds.
     *
     * @public
     */
    getTouchStartDelay() {
        return this._config.touchStartDelay;
    }
    /**
     * Check if touch should prevent default.
     *
     * @returns Whether to prevent default.
     *
     * @public
     */
    shouldPreventDefault() {
        return this._config.preventDefault && this._hasTouchMoved;
    }
    // #endregion
    // #region Private Methods
    /**
     * Get touch point from event.
     *
     * @param event - Touch or pointer event.
     * @returns Touch position.
     *
     * @private
     */
    _getTouchPoint(event) {
        if ('touches' in event && event.touches.length > 0) {
            return {
                x: event.touches[0].clientX,
                y: event.touches[0].clientY
            };
        }
        else if ('clientX' in event) {
            return {
                x: event.clientX,
                y: event.clientY
            };
        }
        return {
            x: 0,
            y: 0
        };
    }
    /**
     * Calculate distance between two points.
     *
     * @param a - First point.
     * @param b - Second point.
     * @returns Distance.
     *
     * @private
     */
    _getDistance(a, b) {
        const dx = b.x - a.x;
        const dy = b.y - a.y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    /**
     * Trigger haptic feedback (if supported).
     *
     * @param intensity - Feedback intensity.
     *
     * @private
     */
    _triggerHaptic(intensity = 'medium') {
        // Check if Vibration API is available
        if (!navigator.vibrate) {
            return;
        }
        // Map intensity to vibration duration
        const duration = {
            light: 10,
            medium: 20,
            heavy: 30
        }[intensity];
        navigator.vibrate(duration);
    }
    /**
     * Apply default config values.
     *
     * @param config - User config.
     * @returns Full config.
     *
     * @private
     */
    _applyDefaults(config) {
        return {
            touchStartDelay: config?.touchStartDelay ?? 300,
            touchMoveThreshold: config?.touchMoveThreshold ?? 5,
            preventDefault: config?.preventDefault ?? true,
            hapticFeedback: config?.hapticFeedback ?? true
        };
    }
}
// #endregion
//# sourceMappingURL=TouchOptimizationHandler.js.map