"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Ripple = _interopRequireDefault(require("../Ripple"));

var _Button = _interopRequireDefault(require("../Button"));

var _utils = require("../utils");

var _useToggleCaret = _interopRequireDefault(require("../utils/useToggleCaret"));

var _Sidenav = require("../Sidenav/Sidenav");

var defaultProps = {
  as: _Button.default,
  classPrefix: 'dropdown-toggle'
};

var DropdownToggle = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      className = props.className,
      classPrefix = props.classPrefix,
      renderTitle = props.renderTitle,
      children = props.children,
      icon = props.icon,
      noCaret = props.noCaret,
      placement = props.placement,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "className", "classPrefix", "renderTitle", "children", "icon", "noCaret", "placement"]);
  var sidenav = (0, _react.useContext)(_Sidenav.SidenavContext);

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      prefix = _useClassNames.prefix,
      withClassPrefix = _useClassNames.withClassPrefix,
      merge = _useClassNames.merge;

  var classes = merge(className, withClassPrefix({
    'custom-title': typeof renderTitle === 'function',
    'no-caret': noCaret
  }));
  var inSidenav = !!sidenav; // Caret icon is down by default, when Dropdown is used in Sidenav.

  var Caret = (0, _useToggleCaret.default)(inSidenav ? 'bottomStart' : placement);

  if (renderTitle) {
    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, rest, {
      ref: ref,
      className: classes
    }), renderTitle(children), /*#__PURE__*/_react.default.createElement(_Ripple.default, null));
  }

  var buttonProps = Component === _Button.default ? {
    appearance: 'subtle'
  } : null;
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, buttonProps, rest, {
    ref: ref,
    className: classes
  }), icon, children, noCaret ? null : /*#__PURE__*/_react.default.createElement(Caret, {
    className: prefix('caret')
  }));
});

DropdownToggle.displayName = 'DropdownToggle';
DropdownToggle.defaultProps = defaultProps;
DropdownToggle.propTypes = {
  className: _propTypes.default.string,
  children: _propTypes.default.node,
  icon: _propTypes.default.node,
  classPrefix: _propTypes.default.string,
  noCaret: _propTypes.default.bool,
  as: _propTypes.default.elementType,
  renderTitle: _propTypes.default.func
};
var _default = DropdownToggle;
exports.default = _default;