"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.ToolsDropdown = exports.ToolsMenu = void 0;

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Block = require("./Block");

var _TabItem = require("./TabItem");

var _classnames = _interopRequireDefault(require("classnames"));

var _NavDropdown = require("./NavDropdown");

var _Icon = require("./Icon");

var _ExternalLink = require("./ExternalLink");

var ToolsMenu =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(ToolsMenu, _React$Component);

  function ToolsMenu() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderMenuItem", function (item) {
      if (_this.props.isExternalLink(item.href)) {
        return _react.default.createElement(_ExternalLink.ExternalLink, {
          id: item.menuTitle,
          key: item.menuTitle,
          href: item.href,
          title: item.title,
          tabIndex: -1,
          onClick: item.onClick,
          role: "menuitem",
          onKeyDown: _this.props.onKeyDown,
          withIcon: true
        });
      }

      var router = _this.context.router;
      var isCurrentPage = router ? router.isActive(item.href) : false;
      var labelForScreenReader = isCurrentPage ? _react.default.createElement("span", {
        className: "sr-only"
      }, ", " + _this.props.current) : null;
      return _react.default.createElement(_TabItem.TabItem, {
        role: "menuitem",
        to: item.href,
        key: item.menuTitle,
        testAutomationId: item.menuTitle,
        tabIndex: -1,
        activeKey: item.menuTitle,
        className: (0, _classnames.default)({
          active: isCurrentPage
        }),
        onKeyDown: _this.props.onKeyDown,
        onSelect: _this.props.onSelect
      }, item.title, labelForScreenReader);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderCategory", function (category) {
      var title = category.title,
          items = category.items,
          id = category.id;
      return _react.default.createElement(_Block.Block, {
        key: id + "Category",
        testAutomationId: id + "Category",
        className: "tools-category",
        role: "group",
        "aria-labelledby": id + "CategoryTitle"
      }, _react.default.createElement("legend", {
        "data-test-automation-id": "categoryName",
        className: "tools-category-title",
        id: id + "CategoryTitle"
      }, title), items.map(_this.renderMenuItem));
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderColumn", function (categories, index) {
      return _react.default.createElement("div", {
        key: "column" + index,
        className: "tools-menu-column"
      }, categories.map(_this.renderCategory));
    });
    return _this;
  }

  var _proto = ToolsMenu.prototype;

  _proto.render = function render() {
    return _react.default.createElement("div", {
      "data-test-automation-id": "toolsMenu",
      className: "tools-menu"
    }, this.props.columns.map(this.renderColumn));
  };

  return ToolsMenu;
}(_react.default.Component);

exports.ToolsMenu = ToolsMenu;
(0, _defineProperty2.default)(ToolsMenu, "contextTypes", {
  router: _propTypes.default.object
});
(0, _defineProperty2.default)(ToolsMenu, "propTypes", {
  columns: _propTypes.default.array.isRequired,
  onKeyDown: _propTypes.default.func,
  onSelect: _propTypes.default.func,
  isExternalLink: _propTypes.default.func,
  current: _propTypes.default.string
});
(0, _defineProperty2.default)(ToolsMenu, "defaultProps", {
  onKeyDown: function onKeyDown(e) {},
  onSelect: function onSelect(e) {},
  isExternalLink: function isExternalLink(url) {
    return /^(?:https?:)?\/\//.test(url);
  },
  current: 'TRANSLATE:CURRENT'
});

var ToolsDropdown =
/*#__PURE__*/
function (_React$Component2) {
  (0, _inheritsLoose2.default)(ToolsDropdown, _React$Component2);

  function ToolsDropdown() {
    return _React$Component2.apply(this, arguments) || this;
  }

  var _proto2 = ToolsDropdown.prototype;

  _proto2.render = function render() {
    var _this$props = this.props,
        id = _this$props.id,
        title = _this$props.title,
        className = _this$props.className,
        items = _this$props.items,
        isExternalLink = _this$props.isExternalLink;

    var titleElement = _react.default.createElement("span", null, title, _react.default.createElement(_Icon.Icon, {
      name: "chevron-down-small"
    }));

    return _react.default.createElement(_NavDropdown.NavDropdown, {
      noCaret: true,
      role: "menu",
      id: id,
      key: id,
      className: className,
      "data-test-automation-id": id,
      activeKey: id,
      title: titleElement
    }, _react.default.createElement(ToolsMenu, {
      columns: items,
      isExternalLink: isExternalLink
    }));
  };

  return ToolsDropdown;
}(_react.default.Component);

exports.ToolsDropdown = ToolsDropdown;
(0, _defineProperty2.default)(ToolsDropdown, "contextTypes", {
  router: _propTypes.default.object
});
(0, _defineProperty2.default)(ToolsDropdown, "propTypes", {
  title: _propTypes.default.string.isRequired,
  id: _propTypes.default.string.isRequired,
  items: _propTypes.default.array.isRequired,
  className: _propTypes.default.string,
  isExternalLink: _propTypes.default.func
});
(0, _defineProperty2.default)(ToolsDropdown, "defaultProps", {
  className: '',
  isExternalLink: function isExternalLink(url) {
    return /^(?:https?:)?\/\//.test(url);
  }
});