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 { Popper } from 'react-popper';
import { Context } from './CDropdownCustom';

//component - CoreUI / CDropdownMenu

var CDropdownMenu = function CDropdownMenu(props) {
  var className = props.className,
      cssModule = props.cssModule,
      right = props.right,
      tag = props.tag,
      flip = props.flip,
      modifiers = props.modifiers,
      persist = props.persist,
      innerRef = props.innerRef,
      attributes = _objectWithoutProperties(props, ['className', 'cssModule', 'right', 'tag', 'flip', 'modifiers', 'persist', 'innerRef']);

  var noFlipModifier = { flip: { enabled: false } };

  var directionPositionMap = {
    up: 'top',
    left: 'left',
    right: 'right',
    down: 'bottom'
  };

  var context = useContext(Context);

  //render

  var classes = mapToCssModules(classNames(className, 'dropdown-menu', {
    'dropdown-menu-right': right,
    'show': context.isOpen
  }), cssModule);

  var Tag = tag;

  if (persist || context.isOpen && !context.inNavbar) {
    Tag = Popper;
    var position1 = directionPositionMap[context.direction] || 'bottom';
    var position2 = right ? 'end' : 'start';
    attributes.placement = position1 + '-' + position2;
    attributes.component = tag;
    attributes.modifiers = !flip ? _extends({}, modifiers, noFlipModifier) : modifiers;
  }

  return React.createElement(Tag, _extends({
    tabIndex: '-1',
    role: 'menu'
  }, attributes, {
    'aria-hidden': !context.isOpen,
    className: classes,
    'x-placement': attributes.placement,
    ref: innerRef
  }));
};

CDropdownMenu.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: tagPropType,
  children: PropTypes.node.isRequired,
  className: PropTypes.string,
  cssModule: PropTypes.object,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  right: PropTypes.bool,
  flip: PropTypes.bool,
  modifiers: PropTypes.object,
  persist: PropTypes.bool
} : {};

CDropdownMenu.defaultProps = {
  tag: 'div',
  flip: true
};

export default CDropdownMenu;