/**
 * @module
 * @prototype {Function} __keyboardHandler
 */

import { getOriginalEvent } from '../utils/index';
import { SmoothScrollbar } from '../smooth_scrollbar';

export { SmoothScrollbar };

/**
 * @method
 * @internal
 * Keypress event handler builder
 *
 * @param {Object} option
 */
let __keyboardHandler = function() {
    const { targets } = this;

    let getKeyDelta = (keyCode) => {
        // key maps [deltaX, deltaY, useSetMethod]
        let { size, offset, limit, movement } = this; // need real time data

        switch (keyCode) {
            case 32: // space
                return [0, 200];
            case 33: // pageUp
                return [0, -size.container.height + 40];
            case 34: // pageDown
                return [0, size.container.height - 40];
            case 35: // end
                return [0, Math.abs(movement.y) + limit.y - offset.y];
            case 36: // home
                return [0, -Math.abs(movement.y) - offset.y];
            case 37: // left
                return [-40, 0];
            case 38: // up
                return [0, -40];
            case 39: // right
                return [40, 0];
            case 40: // down
                return [0, 40];
            default:
                return null;
        }
    };

    const { container } = targets;

    let isFocused = false;

    this.__addEvent(container, 'focus', () => {
        isFocused = true;
    });

    this.__addEvent(container, 'blur', () => {
        isFocused = false;
    });

    this.__addEvent(container, 'keydown', (evt) => {
        if (!isFocused) return;

        evt = getOriginalEvent(evt);

        let delta = getKeyDelta(evt.keyCode || evt.which);

        if (!delta) return;

        const [x, y] = delta;

        if (this.__shouldPropagateMovement(x, y)) {
            container.blur();

            if (this.parents.length) {
                this.parents[0].focus();
            }

            return this.__updateThrottle();
        }

        if (this.__isOntoEdge('x', x) || this.__isOntoEdge('y', y)) {
            this.__autoLockMovement();
        }

        evt.preventDefault();

        const { speed } = this.options;
        this.__addMovement(x * speed, y * speed);
    });

    this.__addEvent(container, 'keyup', () => {
        this.__unlockMovement();
    });
};

Object.defineProperty(SmoothScrollbar.prototype, '__keyboardHandler', {
    value: __keyboardHandler,
    writable: true,
    configurable: true
});

@@ -1599,32 +1599,92 @@
 cused) return;%0A%0A
+        const %7B options, parents, movementLocked %7D = this;%0A%0A
         evt = ge
@@ -1713,18 +1713,20 @@
         
-le
+cons
 t delta 
@@ -2072,40 +2072,109 @@
     
-if (this.__isOntoEdge('x', x) %7C%7C
+evt.preventDefault();%0A%0A        this.__unlockMovement(); // handle for multi keypress%0A        if (x &&
  thi
@@ -2193,96 +2193,108 @@
 ge('
-y', y)) %7B%0A            this.__autoLockMovement();%0A        %7D%0A%0A        evt.preventDefault()
+x', x)) movementLocked.x = true;%0A        if (y && this.__isOntoEdge('y', y)) movementLocked.y = true
 ;%0A%0A 
@@ -2318,21 +2318,16 @@
 eed %7D = 
-this.
 options;

fix movement lock on multi keypress