"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var PropTypes = require("prop-types");
var React = require("react");
var ReactDOM = require("react-dom");
var KeyCode_1 = require("../utils/KeyCode");
var propTypes = {
    backKeys: PropTypes.arrayOf(PropTypes.oneOf(Object.values(KeyCode_1.default))),
    children: PropTypes.node,
    currentID: PropTypes.string,
    forceTabKey: PropTypes.bool,
    forwardKeys: PropTypes.arrayOf(PropTypes.oneOf(Object.values(KeyCode_1.default))),
    itemIDs: PropTypes.arrayOf(PropTypes.string).isRequired,
    noWrap: PropTypes.bool,
    onChange: PropTypes.func.isRequired,
};
/**
 * Wrapper component that enables keyboard navigation through a specified ordered list of items.
 * Emits navigation events with the ID of the next item to focus on - it's up to the client to move
 * focus as needed and update the currentID prop accordingly.
 */
var WithKeyboardNav = /** @class */ (function (_super) {
    __extends(WithKeyboardNav, _super);
    function WithKeyboardNav() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._element = null;
        _this._handleKeyDown = function (event) {
            var _a = _this.props, backKeys = _a.backKeys, forwardKeys = _a.forwardKeys, forceTabKey = _a.forceTabKey;
            if (!_.isString(event.key)) {
                return;
            }
            if (_.includes(forwardKeys, event.key)) {
                _this._move(1, event);
                return;
            }
            if (_.includes(backKeys, event.key)) {
                _this._move(-1, event);
                return;
            }
            if (forceTabKey && event.key === KeyCode_1.default.TAB) {
                _this._move(event.shiftKey ? -1 : 1, event);
                return;
            }
        };
        return _this;
    }
    WithKeyboardNav.prototype.componentDidMount = function () {
        this._element = ReactDOM.findDOMNode(this);
        if (this._element) {
            this._element.addEventListener("keydown", this._handleKeyDown);
        }
    };
    WithKeyboardNav.prototype.componentWillUnmount = function () {
        if (this._element) {
            this._element.removeEventListener("keydown", this._handleKeyDown);
        }
    };
    WithKeyboardNav.prototype.render = function () {
        return this.props.children;
    };
    WithKeyboardNav.prototype._move = function (amount, event) {
        var _a = this.props, currentID = _a.currentID, noWrap = _a.noWrap, onChange = _a.onChange;
        var itemIDs = _.compact(this.props.itemIDs);
        var currentIndex = itemIDs.indexOf(currentID);
        var numItems = itemIDs.length;
        var rawNextIndex = currentIndex + amount;
        var adjustedNextIndex = rawNextIndex;
        if (noWrap) {
            adjustedNextIndex = Math.max(0, Math.min(adjustedNextIndex, numItems - 1));
        }
        if (adjustedNextIndex < 0) {
            adjustedNextIndex = numItems + adjustedNextIndex;
        }
        adjustedNextIndex = adjustedNextIndex % numItems;
        if (adjustedNextIndex === currentIndex) {
            return;
        }
        onChange(itemIDs[adjustedNextIndex]);
        event.preventDefault();
    };
    WithKeyboardNav.propTypes = propTypes;
    WithKeyboardNav.defaultProps = {
        backKeys: [KeyCode_1.default.ARROW_LEFT_IE, KeyCode_1.default.ARROW_LEFT, KeyCode_1.default.ARROW_UP_IE, KeyCode_1.default.ARROW_UP],
        forwardKeys: [
            KeyCode_1.default.ARROW_DOWN_IE,
            KeyCode_1.default.ARROW_DOWN,
            KeyCode_1.default.ARROW_RIGHT_IE,
            KeyCode_1.default.ARROW_RIGHT,
        ],
    };
    return WithKeyboardNav;
}(React.PureComponent));
exports.default = WithKeyboardNav;
