'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _CIcon = require('./CIcon');

var _CIcon2 = _interopRequireDefault(_CIcon);

var _CSidebar = require('./CSidebar');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

//component - CoreUI / CSidebarNavDropdown

var CSidebarNavDropdown = function CSidebarNavDropdown(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      innerRef = props.innerRef,
      icon = props.icon,
      fontIcon = props.fontIcon,
      name = props.name,
      show = props.show,
      toggle = props.toggle,
      toggleItem = props.toggleItem,
      route = props.route,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'innerRef', 'icon', 'fontIcon', 'name', 'show', 'toggle', 'toggleItem', 'route']);

  var _useState = (0, _react.useState)(show),
      isOpen = _useState[0],
      setIsOpen = _useState[1];

  var compData = (0, _react.useRef)({}).current;

  // context
  var context = (0, _react.useContext)(_CSidebar.Context);

  var dropdownMode = void 0;

  // methods

  var onClick = function onClick(e) {
    setIsOpen(!isOpen);
    toggle && toggle(e);
  };
  var onItemClick = function onItemClick(e) {
    toggleItem && toggleItem(e);
  };

  // computed

  if (context.dropdownMode) dropdownMode = context.dropdownMode;else dropdownMode = 'openActive';

  //watch
  //show
  (0, _react.useMemo)(function () {
    if (compData.nextRender) setIsOpen(show);
  }, [show]);
  //dropdownMode
  (0, _react.useMemo)(function () {
    var mode = dropdownMode;
    if (mode === 'close') {
      setIsOpen(false);
    } else if (mode === 'closeInactive' && route) {
      setIsOpen(route.fullPath.includes(route));
    } else if (mode === 'openActive' && !isOpen && route) {
      setIsOpen(route.fullPath.includes(route));
    }
  }, [route]);

  //effect
  (0, _react.useEffect)(function () {
    compData.nextRender = true;
  }, []);

  //render

  var classes = (0, _classnames2.default)(className, 'c-sidebar-nav-dropdown', isOpen ? 'c-show' : null);

  var iconProps = void 0;
  if ((typeof icon === 'undefined' ? 'undefined' : _typeof(icon)) === 'object') iconProps = icon;else iconProps = { name: icon };

  var iconClasses = (0, _classnames2.default)('c-sidebar-nav-icon', fontIcon ? fontIcon : null);

  return _react2.default.createElement(
    Tag,
    _extends({ className: classes }, attributes, { ref: innerRef }),
    _react2.default.createElement(
      'a',
      { className: 'c-sidebar-nav-dropdown-toggle', onClick: onClick },
      icon ? _react2.default.createElement(_CIcon2.default, _extends({ className: 'c-sidebar-nav-icon' }, iconProps)) : '',
      fontIcon ? _react2.default.createElement('i', { className: iconClasses }) : '',
      name
    ),
    _react2.default.createElement(
      'ul',
      { className: 'c-sidebar-nav-dropdown-items', onClick: onItemClick },
      children
    )
  );
};

CSidebarNavDropdown.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
  className: _propTypes2.default.string,
  children: _propTypes2.default.node,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  name: _propTypes2.default.string,
  icon: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]),
  fontIcon: _propTypes2.default.string,
  show: _propTypes2.default.bool,
  toggle: _propTypes2.default.func,
  toggleItem: _propTypes2.default.func,
  route: _propTypes2.default.string
} : {};

CSidebarNavDropdown.defaultProps = {
  tag: 'li'
};

exports.default = CSidebarNavDropdown;
module.exports = exports['default'];