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, { useContext } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { tagPropType, mapToCssModules } from './Shared/helper.js';
import { Target } from 'react-popper';
import CButton from './CButton';
import { Context } from './CDropdownCustom';

//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 = useContext(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 = mapToCssModules(classNames(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 || React.createElement(
    'span',
    { className: 'sr-only' },
    ariaLabel
  );

  var Tag = void 0;
  var isButton = false;

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

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

  return React.createElement(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: tagPropType,
  children: PropTypes.node,
  className: PropTypes.string,
  cssModule: PropTypes.object,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  caret: PropTypes.bool,
  color: PropTypes.string,
  disabled: PropTypes.bool,
  onClick: PropTypes.func,
  'aria-haspopup': PropTypes.bool,
  split: PropTypes.bool,
  togglerHtml: PropTypes.node,
  nav: PropTypes.bool,
  header: PropTypes.bool
} : {};

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

export default CDropdownToggle;