"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

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

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

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

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

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

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

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

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

// Style

/* istanbul ignore next */
if (typeof window !== 'undefined') require("./style.scss");

var Dropdown = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Dropdown, _React$Component);

  var _super = _createSuper(Dropdown);

  function Dropdown(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Dropdown);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_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((0, _assertThisInitialized2["default"])(_this));
    return _this;
  }

  (0, _createClass2["default"])(Dropdown, [{
    key: "render",
    value: function render() {
      var _this2 = this;

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

      var clickEvents = _objectSpread({}, trigger === 'click' && {
        onClick: this.toggle
      });

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

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

      var DropdownClasses = [className, 'Dropdown', "Dropdown_align=".concat(align), "Dropdown_justify=".concat(justify), "Dropdown_".concat(this.state.open ? 'opened' : 'closed')].filter(function (e) {
        return e;
      }).join(' ');
      return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, hoverEvents, {
        className: DropdownClasses,
        onMouseLeave: function onMouseLeave() {
          return !sticky && _this2.toggle(false);
        },
        style: this.props.style
      }), /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, clickEvents, {
        className: "Dropdown-toggle"
      }), button), /*#__PURE__*/_react["default"].createElement("div", {
        className: "Dropdown-content Dropdown-content_".concat(this.state.open ? 'opened' : 'closed')
      }, content));
    }
  }]);
  return Dropdown;
}(_react["default"].Component);

(0, _defineProperty2["default"])(Dropdown, "propTypes", {
  align: _propTypes["default"].oneOf(['start', 'top', 'center', 'bottom', 'end']),
  className: _propTypes["default"].string,
  justify: _propTypes["default"].oneOf(['start', 'left', 'center', 'right', 'end']),
  onToggle: _propTypes["default"].func,
  open: _propTypes["default"].bool,
  sticky: _propTypes["default"].bool,
  trigger: _propTypes["default"].oneOf(['click', 'hover'])
});
(0, _defineProperty2["default"])(Dropdown, "defaultProps", {
  align: 'start',
  justify: 'start',
  open: false,
  sticky: false,
  trigger: 'click'
});
var _default = Dropdown;
exports["default"] = _default;