import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

var _dec, _class, _class2, _temp;

import React from 'react';
import PropTypes from 'prop-types';
import { withTheme } from '@material-ui/core/styles';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import { Button } from '@material-ui/core';
import { ArrowDropDown } from '@material-ui/icons';
import { style } from 'typestyle';
import { includes } from 'lodash';

var getCn = function getCn(_ref) {
  var theme = _ref.theme;
  return {
    button: style({
      paddingRight: theme.spacing.unit
    })
  };
};

var PaperProps = {
  style: {
    maxHeight: 200
  }
};
var Dropdown = (_dec = withTheme(), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Dropdown, _React$Component);

  function Dropdown() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Dropdown);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Dropdown)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      open: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "button", React.createRef());

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleToggle", function () {
      return _this.setState(function (state) {
        return {
          open: !state.open
        };
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleSelect", function (item) {
      _this.setState({
        open: false
      });

      _this.props.onSelectChanged(item);
    });

    return _this;
  }

  _createClass(Dropdown, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var dcn = getCn(this.props);
      var value = includes(this.props.items, this.props.selected) ? this.props.selected : this.props.localization.common.clickToSelect;
      return React.createElement(React.Fragment, null, React.createElement(Button, {
        buttonRef: this.button,
        variant: "contained",
        color: "primary",
        size: "small",
        className: dcn.button,
        onClick: this.handleToggle
      }, value, React.createElement(ArrowDropDown, null)), React.createElement(Menu, {
        id: "lock-menu",
        anchorEl: this.button.current,
        open: this.state.open,
        onClose: this.handleToggle,
        PaperProps: PaperProps
      }, this.props.items.map(function (item, index) {
        return React.createElement(MenuItem, {
          key: index,
          selected: item === _this2.props.selected,
          onClick: function onClick() {
            return _this2.handleSelect(item);
          }
        }, item);
      })));
    }
  }]);

  return Dropdown;
}(React.Component), _defineProperty(_class2, "propTypes", {
  items: PropTypes.array.isRequired,
  selected: PropTypes.string.isRequired,
  onSelectChanged: PropTypes.func.isRequired,
  localization: PropTypes.object.isRequired
}), _defineProperty(_class2, "defaultProps", {
  items: [],
  selected: '',
  onSelectChanged: function onSelectChanged() {}
}), _temp)) || _class);
export { Dropdown as default };