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

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 from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import PerfectScrollbar from 'react-perfect-scrollbar';
import 'react-perfect-scrollbar/dist/css/styles.css';
import CBadge from './CBadge';
import CSidebarNavTitle from './CSidebarNavTitle';
import CSidebarNavDivider from './CSidebarNavDivider';
import CSidebarNavDropdown from './CSidebarNavDropdown';
import CSidebarNavItem from './CSidebarNavItem';
import CSidebarNavLink from './CSidebarNavLink';

//component - CoreUI / CSidebarNav

var CSidebarNav = function CSidebarNav(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      innerRef = props.innerRef,
      navConfig = props.navConfig,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'innerRef', 'navConfig']);

  var isExternal = function isExternal(url) {
    var link = url ? url.substring(0, 4) : '';
    return link === 'http';
  };

  // render items

  // nav dropdown
  var navDropdown = function navDropdown(item, key) {
    return React.createElement(
      CSidebarNavDropdown,
      {
        key: key,
        icon: item.icon,
        name: item.name,
        show: props.location.pathname.indexOf(item.url) > -1
      },
      navList(item.children)
    );
  };
  // nav item with nav link
  var navItem = function navItem(item, key) {

    var badgeProps = void 0;
    if (item.badge) {
      badgeProps = {
        text: item.badge.text,
        color: item.badge.variant
      };
    } else {
      badgeProps = {};
    }

    var disabled = false;
    if (item.attributes && item.attributes.disabled) {
      disabled = true;
    }

    return React.createElement(
      CSidebarNavItem,
      {
        key: key,
        icon: item.icon,
        to: item.url,
        badge: badgeProps,
        linkClassName: props.location && props.location.pathname.indexOf(item.url) > -1 ? 'c-active' : null,
        linkProps: { disabled: disabled }
      },
      item.name
    );
  };

  // simple wrapper for nav-title item
  var navWrapper = function navWrapper(item) {
    return item.wrapper && item.wrapper.element ? React.createElement(item.wrapper.element, item.wrapper.attributes, item.name) : item.name;
  };
  // nav list section title
  var navTitle = function navTitle(title, key) {
    return React.createElement(
      CSidebarNavTitle,
      { key: key, className: title.class },
      navWrapper(title)
    );
  };

  // nav list divider
  var navDivider = function navDivider(divider, key) {
    return React.createElement(CSidebarNavDivider, { key: key, className: divider.class });
  };

  // nav label with nav link
  var navLabel = function navLabel(item, key) {

    var classes = classNames(item.label.class ? item.label.class : null, item.label.variant ? 'text-' + item.label.variant : null);

    return React.createElement(
      CSidebarNavItem,
      {
        key: key,
        linkClassName: classes,
        linkProps: {
          icon: item.icon ? item.icon : 'user',
          to: item.url,
          label: true
        }
      },
      item.name
    );
  };

  // select nav type
  var navType = function navType(item, idx) {
    return item.title ? navTitle(item, idx) : item.divider ? navDivider(item, idx) : item.label ? navLabel(item, idx) : item.children ? navDropdown(item, idx) : navItem(item, idx);
  };

  // nav list
  var navList = function navList(items) {
    return items.map(function (item, index) {
      return navType(item, index);
    });
  };

  //render

  delete attributes.Tag;

  var navClasses = classNames(className, 'c-sidebar-nav', 'h-100');

  //state

  var isRtl = getComputedStyle(document.querySelector('html')).direction === 'rtl';

  // sidebar-nav root

  return React.createElement(
    PerfectScrollbar,
    _extends({ options: { suppressScrollX: !isRtl }, className: navClasses, ref: innerRef, component: Tag }, attributes),
    navConfig.items ? navList(navConfig.items) : null,
    children
  );
};

CSidebarNav.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  children: PropTypes.node,
  className: PropTypes.string,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  navConfig: PropTypes.any
} : {};

CSidebarNav.defaultProps = {
  tag: 'ul',
  navConfig: {}
};

export default CSidebarNav;