"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 __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var lodash_1 = __importDefault(require("lodash"));
var constants_1 = require("../../utils/constants");
var KeyDownable = /** @class */ (function (_super) {
    __extends(KeyDownable, _super);
    function KeyDownable() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.onClick = function (e) {
            var onClick = _this.props.onClick;
            if (lodash_1.default.isFunction(onClick)) {
                onClick(e);
            }
        };
        _this.onKeyDown = function (e) {
            var _a = _this.props, preventDefaultKeyDown = _a.preventDefaultKeyDown, stopPropagation = _a.stopPropagation, onKeyDown = _a.onKeyDown;
            if (lodash_1.default.isFunction(onKeyDown)) {
                return onKeyDown(e);
            }
            if (e.keyCode === constants_1.KeyCodes.SPACE || e.keyCode === constants_1.KeyCodes.ENTER) {
                if (preventDefaultKeyDown) {
                    e.preventDefault();
                }
                if (stopPropagation) {
                    e.stopPropagation();
                }
                _this.onClick(e);
            }
        };
        /* prevent focus style always there after click */
        _this.onMouseDown = function (e) {
            var _a = _this.props, preventDefaultMouseDown = _a.preventDefaultMouseDown, onMouseDown = _a.onMouseDown;
            if (preventDefaultMouseDown) {
                e.preventDefault();
            }
            if (lodash_1.default.isFunction(onMouseDown)) {
                onMouseDown(e);
            }
        };
        return _this;
    }
    KeyDownable.prototype.render = function () {
        var children = this.props.children;
        var renderChildren = children;
        return renderChildren({
            tabIndex: this.props.tabIndex,
            onClick: this.onClick,
            onKeyDown: this.onKeyDown,
            onMouseDown: this.onMouseDown
        });
    };
    KeyDownable.propTypes = {
        children: prop_types_1.default.func.isRequired,
        preventDefaultKeyDown: prop_types_1.default.bool,
        preventDefaultMouseDown: prop_types_1.default.bool,
        tabIndex: prop_types_1.default.number,
        stopPropagation: prop_types_1.default.bool,
        onClick: prop_types_1.default.func,
        onKeyDown: prop_types_1.default.func,
        onMouseDown: prop_types_1.default.func
    };
    KeyDownable.defaultProps = {
        preventDefaultKeyDown: true,
        preventDefaultMouseDown: true,
        tabIndex: 0,
        stopPropagation: false,
        onClick: null,
        onKeyDown: null,
        onMouseDown: null
    };
    return KeyDownable;
}(react_1.default.Component));
exports.KeyDownable = KeyDownable;
//# sourceMappingURL=KeyDownable.js.map