'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 _helper = require('./Shared/helper.js');

var _reactPopper = require('react-popper');

var _CButton = require('./CButton');

var _CButton2 = _interopRequireDefault(_CButton);

var _CDropdownCustom = require('./CDropdownCustom');

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 / CDropdownToggle

var CDropdownToggle = function CDropdownToggle(props) {
  var className = props.className,
      color = props.color,
      cssModule = props.cssModule,
      innerRef = props.innerRef,
      caret = props.caret,
      split = props.split,
      nav = props.nav,
      header = props.header,
      tag = props.tag,
      togglerHtml = props.togglerHtml,
      attributes = _objectWithoutProperties(props, ['className', 'color', 'cssModule', 'innerRef', 'caret', 'split', 'nav', 'header', 'tag', 'togglerHtml']);

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

  var onClick = function onClick(e) {
    if (props.disabled) {
      e.preventDefault();
      return;
    }
    if (props.nav && !props.tag) {
      e.preventDefault();
    }
    if (props.onClick) {
      props.onClick(e);
    }
    context.toggle(e);
  };

  //render

  var ariaLabel = attributes['aria-label'] || 'Toggle Dropdown';

  var classes = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, {
    'dropdown-toggle': caret || split,
    'dropdown-toggle-split': split,
    'nav-link': nav && !header,
    'c-header-nav-link': nav && header
  }), cssModule);

  var children = togglerHtml || attributes.children || _react2.default.createElement(
    'span',
    { className: 'sr-only' },
    ariaLabel
  );

  var Tag = void 0;
  var isButton = false;

  if (nav && !tag) {
    Tag = 'a';
    attributes.href = '#';
  } else if (!tag) {
    Tag = _CButton2.default;
    attributes.color = color;
    attributes.cssModule = cssModule;
    isButton = true;
  } else {
    Tag = tag;
  }

  if (context.inNavbar) {
    if (isButton) return _react2.default.createElement(Tag, _extends({}, attributes, {
      className: classes,
      onClick: onClick,
      'aria-expanded': context.isOpen,
      children: children
    }));else return _react2.default.createElement(Tag, _extends({}, attributes, {
      className: classes,
      onClick: onClick,
      'aria-expanded': context.isOpen,
      children: children,
      ref: innerRef
    }));
  }

  return _react2.default.createElement(_reactPopper.Target, _extends({}, attributes, {
    className: classes,
    component: Tag,
    onClick: onClick,
    'aria-expanded': context.isOpen,
    children: children,
    ref: innerRef
  }));
};

CDropdownToggle.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _helper.tagPropType,
  children: _propTypes2.default.node,
  className: _propTypes2.default.string,
  cssModule: _propTypes2.default.object,
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  caret: _propTypes2.default.bool,
  color: _propTypes2.default.string,
  disabled: _propTypes2.default.bool,
  onClick: _propTypes2.default.func,
  'aria-haspopup': _propTypes2.default.bool,
  split: _propTypes2.default.bool,
  togglerHtml: _propTypes2.default.node,
  nav: _propTypes2.default.bool,
  header: _propTypes2.default.bool
} : {};

CDropdownToggle.defaultProps = {
  'aria-haspopup': true
};

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