"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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 classnames_1 = __importDefault(require("classnames"));
var react_router_1 = require("react-router");
var Icon_1 = require("./Icon");
var KeyDownable_1 = require("./KeyDownable");
var index_en_US_1 = __importDefault(require("../../lang/common/index-en_US"));
/*
 * TODO refactor
 * Refactoring strategy:
 * 1) Remove all router usage, if developer want to navigate with this menu - he should make it on his own.
 * 2) Remove window.scroll usage, each page should control page scroll position by itself
 * 3) Remove children usage - it's incorrect and react shows errors about it
 * 4) Rename sub prop to something else (like compact)
 * 5) Move 'isActive' flag outside each item to root prop (like in tabs)
 * 6) a11y - add ariaLabel for each menu config for correct navigation
 */
var CLASS_NAME_MENU = 'rc-menu';
var FORCE_UPDATE_TIMEOUT = 300;
var Menu = /** @class */ (function (_super) {
    __extends(Menu, _super);
    function Menu() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.routerListener = null;
        _this.isItemSelected = function (menuItem) {
            var router = _this.context.router;
            if (menuItem.link && router && router.isActive(menuItem.link)) {
                return true;
            }
            if (menuItem.isActive) {
                return true;
            }
            if (Array.isArray(menuItem.children)) {
                return menuItem.children.some(_this.isItemSelected);
            }
            return false;
        };
        _this.handleLinkClick = function () {
            window.scrollTo(0, 0);
        };
        _this.renderRootMenuItem = function (menuItem, index) { return _this.renderMenuItem(menuItem, index); };
        _this.renderNestedMenuItem = function (menuItem, index) { return _this.renderMenuItem(menuItem, index, true); };
        return _this;
    }
    Menu.prototype.componentDidMount = function () {
        var _this = this;
        var router = this.context.router;
        if (router) {
            this.routerListener = router.listen(function () {
                // TODO refactor this weird shit
                setTimeout(function () {
                    _this.forceUpdate();
                }, FORCE_UPDATE_TIMEOUT);
            });
        }
    };
    Menu.prototype.componentWillUnmount = function () {
        if (this.routerListener) {
            this.routerListener();
        }
    };
    Menu.prototype.renderMenuItem = function (menuItem, index, nested) {
        var _this = this;
        if (nested === void 0) { nested = false; }
        var sub = this.props.sub;
        var name = menuItem.name, link = menuItem.link, testAutomationId = menuItem.testAutomationId, id = menuItem.id, children = menuItem.children, onClickHandler = menuItem.onClickHandler;
        var iconNode = this.renderIcon(menuItem.icon);
        var isCurrentMenuItem = this.isItemSelected(menuItem);
        var classNames = classnames_1.default('rc-menu__item', menuItem.className, {
            'rc-menu__item_with-icon': menuItem.icon,
            'rc-menu__item_nested': nested,
            'rc-menu__item_sub': sub,
            'rc-menu__item_active': isCurrentMenuItem,
            // TODO this class was returned for back compatibility with
            active: isCurrentMenuItem
        });
        var content;
        if (!onClickHandler) {
            content = (react_1.default.createElement(react_router_1.Link, { to: link, className: classNames, "data-test-automation-id": "item", role: "menuitem", onClick: this.handleLinkClick },
                iconNode,
                name,
                isCurrentMenuItem && (react_1.default.createElement("span", { className: "sr-only" },
                    ", ",
                    this.props.currentLabel))));
        }
        else {
            content = (react_1.default.createElement(KeyDownable_1.KeyDownable, { onClick: function (event) {
                    window.scrollTo(0, 0);
                    onClickHandler(event);
                    // TODO refactor this weird shit
                    setTimeout(function () {
                        _this.forceUpdate();
                    }, FORCE_UPDATE_TIMEOUT);
                } }, function (keyDownProps) { return (react_1.default.createElement("button", __assign({}, keyDownProps, { href: "javascript:;", className: classNames, "data-test-automation-id": "item", role: "menuitem" }),
                iconNode,
                name,
                isCurrentMenuItem && (react_1.default.createElement("span", { className: "sr-only" },
                    ", ",
                    _this.props.currentLabel)))); }));
        }
        return (react_1.default.createElement("li", { className: "rc-menu__item-wrapper", key: index, id: id, "data-test-automation-id": testAutomationId, role: "none" },
            content,
            this.renderChildren(children, isCurrentMenuItem)));
    };
    Menu.prototype.renderIcon = function (iconName) {
        if (!iconName) {
            return null;
        }
        var sub = this.props.sub;
        var className = classnames_1.default('rc-menu__icon', {
            'rc-menu__icon_sub': sub
        });
        return react_1.default.createElement(Icon_1.Icon, { name: iconName, className: className, "aria-hidden": "true" });
    };
    Menu.prototype.renderChildren = function (items, isParentActive) {
        if (!items || !isParentActive) {
            return null;
        }
        var className = classnames_1.default(CLASS_NAME_MENU, 'rc-menu_nested');
        return react_1.default.createElement("ul", { className: className }, items.map(this.renderNestedMenuItem));
    };
    Menu.prototype.render = function () {
        var _a = this.props, ariaLabel = _a.ariaLabel, sub = _a.sub, children = _a.children, config = _a.config;
        var className = classnames_1.default(CLASS_NAME_MENU, this.props.className, {
            'rc-menu_submenu': sub
        });
        var menuItems = config || children;
        return (react_1.default.createElement("nav", { "aria-label": ariaLabel },
            react_1.default.createElement("ul", { className: className, role: "menubar", "aria-label": ariaLabel, "data-test-automation-id": this.props.testAutomationId }, menuItems.map(this.renderRootMenuItem))));
    };
    Menu.contextTypes = {
        router: prop_types_1.default.object
    };
    Menu.propTypes = {
        config: prop_types_1.default.arrayOf(prop_types_1.default.shape({
            id: prop_types_1.default.string,
            name: prop_types_1.default.string,
            link: prop_types_1.default.string,
            children: prop_types_1.default.array,
            onClickHandler: prop_types_1.default.func,
            testAutomationId: prop_types_1.default.string
        })),
        children: prop_types_1.default.node,
        className: prop_types_1.default.string,
        sub: prop_types_1.default.bool,
        testAutomationId: prop_types_1.default.string,
        ariaLabel: prop_types_1.default.string,
        currentLabel: prop_types_1.default.string
    };
    Menu.defaultProps = {
        children: [],
        testAutomationId: null,
        className: null,
        config: null,
        sub: false,
        get ariaLabel() {
            return index_en_US_1.default.SIDE_BAR_MENU_ARIA_LABEL;
        },
        get currentLabel() {
            return index_en_US_1.default.CURRENT;
        }
    };
    return Menu;
}(react_1.default.PureComponent));
exports.Menu = Menu;
//# sourceMappingURL=Menu.js.map