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, { PureComponent } from 'react';
import { withTheme } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import MuiMenu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Switch from '@material-ui/core/Switch';
import ActionLanguage from '@material-ui/icons/Language';
import NavigationMenu from '@material-ui/icons/Menu';
import { acceptedLanguages } from '../localization/';
var cn = {
  root: style({
    zIndex: null
  }),
  button: style({
    marginLeft: 20
  }),
  toggle: style({
    filter: 'contrast(40%)'
  }),
  blank: style({
    flex: 1
  })
};

var getCn = function getCn(props) {
  return {
    leftIcon: style({
      marginTop: 0,
      marginLeft: -14,
      display: props.showAll ? 'block' : 'none'
    }),
    contrastColor: style({
      color: props.theme.palette.primary.contrastText
    })
  };
};

var Menu = (_dec = withTheme(), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(Menu, _PureComponent);

  function Menu() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Menu);

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

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleSetTitle", function (event) {
      _this.setState({
        overrideTitle: event.data.value
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleLanguage", function (event) {
      _this.setState({
        anchorEl: event.currentTarget
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleCloseMenu", function () {
      _this.setState({
        anchorEl: null
      });
    });

    return _this;
  }

  _createClass(Menu, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.props.globalEvent.on('message.menuTitle', this.handleSetTitle);
    }
  }, {
    key: "render",
    value: function render() {
      var dcn = getCn(this.props);
      var _this$props = this.props,
          localization = _this$props.localization,
          setLocalization = _this$props.setLocalization;
      return React.createElement(AppBar, {
        className: cn.root,
        position: "relative"
      }, React.createElement(Toolbar, null, React.createElement(IconButton, {
        className: dcn.leftIcon,
        onClick: this.props.toggleSidebar
      }, React.createElement(NavigationMenu, null)), this.state.overrideTitle, React.createElement("div", {
        className: cn.blank
      }), React.createElement(FormControlLabel, {
        control: React.createElement(Switch, {
          checked: this.props.showAll,
          onChange: this.props.toggleShowAll,
          className: cn.toggle
        }),
        label: this.props.showAll ? '' : localization.menu.showAll,
        className: dcn.contrastColor
      }), React.createElement(IconButton, {
        tooltip: localization.menu.language,
        onClick: this.handleLanguage
      }, React.createElement(ActionLanguage, null)), React.createElement(MuiMenu, {
        anchorEl: this.state.anchorEl,
        open: !!this.state.anchorEl,
        onClose: this.handleCloseMenu
      }, acceptedLanguages.map(function (lang) {
        return React.createElement(MenuItem, {
          key: lang.accept[0],
          onClick: function onClick() {
            return setLocalization(lang.accept[0]);
          }
        }, lang.native);
      }))));
    }
  }]);

  return Menu;
}(PureComponent), _defineProperty(_class2, "propTypes", {
  theme: PropTypes.object.isRequired,
  toggleSidebar: PropTypes.func.isRequired,
  openFileDialog: PropTypes.func.isRequired,
  localization: PropTypes.object.isRequired,
  setLocalization: PropTypes.func.isRequired,
  showAll: PropTypes.bool.isRequired,
  toggleShowAll: PropTypes.func.isRequired,
  globalEvent: PropTypes.object.isRequired
}), _temp)) || _class);
export { Menu as default };