import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classNames from 'classnames';
import { CloseGlyph, CaretDownGlyph } from '@wfp/icons-react';
import { settings } from '../../globals/js';
var prefix = settings.prefix;

var MainNavigationItem = /*#__PURE__*/function (_Component) {
  _inherits(MainNavigationItem, _Component);

  var _super = _createSuper(MainNavigationItem);

  function MainNavigationItem() {
    var _this;

    _classCallCheck(this, MainNavigationItem);

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

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "UNSAFE_componentWillReceiveProps", function (nextProps) {
      if (nextProps.menuItem === nextProps.activeMenuItem) {
        document.addEventListener('mousedown', _this.handleClickOutside);
      } else {
        document.removeEventListener('mousedown', _this.handleClickOutside);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "setWrapperRef", function (node) {
      _this.wrapperRef = node;
    });

    _defineProperty(_assertThisInitialized(_this), "handleClickOutside", function (e) {
      if (_this.wrapperRef && !_this.wrapperRef.contains(e.target)) {
        _this.props.onChangeSub('close');
      }
    });

    return _this;
  }

  _createClass(MainNavigationItem, [{
    key: "render",
    value: function render() {
      var _classNames, _classNames2, _classNames3;

      var _this$props = this.props,
          activeMenuItem = _this$props.activeMenuItem,
          className = _this$props.className,
          children = _this$props.children,
          menuItem = _this$props.menuItem,
          onChangeSub = _this$props.onChangeSub,
          subNavigation = _this$props.subNavigation,
          subNavWideAsContent = _this$props.subNavWideAsContent;
      var wrapperClasses = classNames(className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "--main-navigation__item"), true), _defineProperty(_classNames, "".concat(prefix, "--main-navigation__item--open"), menuItem === activeMenuItem), _defineProperty(_classNames, "".concat(prefix, "--content-width"), subNavigation && subNavWideAsContent), _classNames));
      var triggerClasses = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "--main-navigation__trigger"), true), _defineProperty(_classNames2, "".concat(prefix, "--main-navigation__trigger--has-sub"), subNavigation), _defineProperty(_classNames2, "".concat(prefix, "--main-navigation__trigger--open"), menuItem === activeMenuItem), _classNames2));
      var Icon = menuItem === activeMenuItem ? CloseGlyph : CaretDownGlyph;
      var childrenWithProps = subNavigation ? /*#__PURE__*/React.cloneElement(children, {
        children: /*#__PURE__*/React.createElement(React.Fragment, null, children.props.children, /*#__PURE__*/React.createElement(Icon, {
          className: "".concat(prefix, "--main-navigation__trigger__icon"),
          fill: "#FFFFFF",
          description: "expand icon"
        })),
        onClick: function onClick(e) {
          return onChangeSub('toggle', menuItem, e);
        }
      }) : children;
      var subClasses = classNames((_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefix, "--main-navigation__sub"), true), _defineProperty(_classNames3, "".concat(prefix, "--main-navigation__sub--open"), menuItem === activeMenuItem), _classNames3));
      return /*#__PURE__*/React.createElement("li", {
        className: wrapperClasses,
        ref: this.setWrapperRef
      }, /*#__PURE__*/React.createElement("div", {
        className: triggerClasses
      }, childrenWithProps), subNavigation && /*#__PURE__*/React.createElement("div", {
        className: subClasses,
        open: menuItem === activeMenuItem ? true : false
      }, subNavigation));
    }
  }]);

  return MainNavigationItem;
}(Component);

MainNavigationItem.propTypes = {
  /**
   * The CSS class name to be placed on the wrapping element.
   */
  className: PropTypes.string,
  children: PropTypes.element.isRequired,

  /**
   * If defined the `MainNaviationItem` will have a toggleable
   * SubNavigation. See the `SubNavigation` component for details on usage
   */
  subNavigation: PropTypes.node
};
export default MainNavigationItem;