'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 _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 _reactPerfectScrollbar = require('react-perfect-scrollbar');

var _reactPerfectScrollbar2 = _interopRequireDefault(_reactPerfectScrollbar);

require('react-perfect-scrollbar/dist/css/styles.css');

var _CBadge = require('./CBadge');

var _CBadge2 = _interopRequireDefault(_CBadge);

var _CSidebarNavTitle = require('./CSidebarNavTitle');

var _CSidebarNavTitle2 = _interopRequireDefault(_CSidebarNavTitle);

var _CSidebarNavDivider = require('./CSidebarNavDivider');

var _CSidebarNavDivider2 = _interopRequireDefault(_CSidebarNavDivider);

var _CSidebarNavDropdown = require('./CSidebarNavDropdown');

var _CSidebarNavDropdown2 = _interopRequireDefault(_CSidebarNavDropdown);

var _CSidebarNavItem = require('./CSidebarNavItem');

var _CSidebarNavItem2 = _interopRequireDefault(_CSidebarNavItem);

var _CSidebarNavLink = require('./CSidebarNavLink');

var _CSidebarNavLink2 = _interopRequireDefault(_CSidebarNavLink);

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 / 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 _react2.default.createElement(
      _CSidebarNavDropdown2.default,
      {
        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 _react2.default.createElement(
      _CSidebarNavItem2.default,
      {
        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 ? _react2.default.createElement(item.wrapper.element, item.wrapper.attributes, item.name) : item.name;
  };
  // nav list section title
  var navTitle = function navTitle(title, key) {
    return _react2.default.createElement(
      _CSidebarNavTitle2.default,
      { key: key, className: title.class },
      navWrapper(title)
    );
  };

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

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

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

    return _react2.default.createElement(
      _CSidebarNavItem2.default,
      {
        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 = (0, _classnames2.default)(className, 'c-sidebar-nav', 'h-100');

  //state

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

  // sidebar-nav root

  return _react2.default.createElement(
    _reactPerfectScrollbar2.default,
    _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: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
  children: _propTypes2.default.node,
  className: _propTypes2.default.string,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  navConfig: _propTypes2.default.any
} : {};

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

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