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 _class, _temp;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import './style.scss';

import React from 'react';
import PropTypes from 'prop-types';

var Dropdown = (_temp = _class = function (_React$Component) {
  _inherits(Dropdown, _React$Component);

  function Dropdown(props) {
    _classCallCheck(this, Dropdown);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.toggle = function (val) {
      var value = [true, false].includes(val) ? val : !_this.state.open;
      _this.setState({ open: value });
      if (typeof _this.props.onToggle === 'function') _this.props.onToggle(value);
    };

    _this.state = {
      open: props.open || false
    };
    _this.toggle = _this.toggle.bind(_this);
    return _this;
  }

  Dropdown.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        align = _props.align,
        className = _props.className,
        justify = _props.justify,
        children = _props.children,
        sticky = _props.sticky,
        trigger = _props.trigger;

    var clickEvents = _extends({}, trigger === 'click' && { onClick: this.toggle });
    var hoverEvents = _extends({}, trigger === 'hover' && { onMouseEnter: function onMouseEnter(e) {
        return _this2.toggle(e, true);
      }, onClick: this.toggle });

    var _ref = Array.isArray(children) ? children : [undefined, children],
        _ref$ = _ref[0],
        button = _ref$ === undefined ? 'Toggle' : _ref$,
        content = _ref.slice(1);

    var DropdownClasses = [className, 'Dropdown', 'Dropdown_align=' + align, 'Dropdown_justify=' + justify, 'Dropdown_' + (this.state.open ? 'opened' : 'closed')].filter(function (e) {
      return e;
    }).join(' ');
    return React.createElement(
      'div',
      _extends({}, hoverEvents, {
        className: DropdownClasses,
        onMouseLeave: function onMouseLeave() {
          return !sticky && _this2.toggle(false);
        },
        style: this.props.style
      }),
      React.createElement(
        'div',
        _extends({}, clickEvents, { className: 'Dropdown-toggle' }),
        button
      ),
      React.createElement(
        'div',
        { className: 'Dropdown-content Dropdown-content_' + (this.state.open ? 'opened' : 'closed') },
        content
      )
    );
  };

  return Dropdown;
}(React.Component), _class.defaultProps = {
  align: 'start',
  justify: 'start',
  open: false,
  sticky: false,
  trigger: 'click'
}, _temp);
Dropdown.propTypes = process.env.NODE_ENV !== "production" ? {
  align: PropTypes.oneOf(['start', 'top', 'center', 'bottom', 'end']),
  className: PropTypes.string,
  justify: PropTypes.oneOf(['start', 'left', 'center', 'right', 'end']),
  onToggle: PropTypes.func,
  open: PropTypes.bool,
  sticky: PropTypes.bool,
  trigger: PropTypes.oneOf(['click', 'hover'])
} : {};


export default Dropdown;