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; };

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; }

import React, { useState, useContext, useMemo, useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import CIcon from './CIcon';

import { Context } from './CSidebar';

//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 = useState(show),
      isOpen = _useState[0],
      setIsOpen = _useState[1];

  var compData = useRef({}).current;

  // context
  var context = useContext(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
  useMemo(function () {
    if (compData.nextRender) setIsOpen(show);
  }, [show]);
  //dropdownMode
  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
  useEffect(function () {
    compData.nextRender = true;
  }, []);

  //render

  var classes = classNames(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 = classNames('c-sidebar-nav-icon', fontIcon ? fontIcon : null);

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

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

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

export default CSidebarNavDropdown;