'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _keys = require('babel-runtime/core-js/object/keys');

var _keys2 = _interopRequireDefault(_keys);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _VisibilityOff = require('@material-ui/icons/VisibilityOff');

var _VisibilityOff2 = _interopRequireDefault(_VisibilityOff);

var _Print = require('@material-ui/icons/Print');

var _Print2 = _interopRequireDefault(_Print);

var _reactIntl = require('react-intl');

var _Toolbar = require('../Toolbar');

var _Toolbar2 = _interopRequireDefault(_Toolbar);

var _HiddenFieldsMenu = require('./HiddenFieldsMenu');

var _HiddenFieldsMenu2 = _interopRequireDefault(_HiddenFieldsMenu);

var _prop_types = require('../../util/prop_types');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var messages = (0, _reactIntl.defineMessages)({
  hideFieldsLabel: {
    'id': 'reportToolbar.hideFieldsLabel',
    'defaultMessage': '{count, plural, =0 {Hide Fields} one {# Hidden Field} other {# Hidden Fields}}'
  },
  printLabel: {
    'id': 'reportToolbar.printLabel',
    'defaultMessage': 'Print'
  }
});

var ReportToolbar = function (_React$Component) {
  (0, _inherits3.default)(ReportToolbar, _React$Component);

  function ReportToolbar() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, ReportToolbar);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = ReportToolbar.__proto__ || (0, _getPrototypeOf2.default)(ReportToolbar)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      hideFieldsOpen: false
    }, _this.handleHideFieldsButtonClick = function (event) {
      _this.setState({
        hideFieldsOpen: !_this.state.hideFieldsOpen,
        hideFieldsButtonEl: event.currentTarget
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(ReportToolbar, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          fieldAnalysis = _props.fieldAnalysis,
          hiddenFields = _props.hiddenFields,
          onToggleFieldVisibility = _props.onToggleFieldVisibility,
          requestPrint = _props.requestPrint,
          onShowAll = _props.onShowAll,
          onHideAll = _props.onHideAll;

      var fields = getFieldList(fieldAnalysis, hiddenFields);
      var hiddenCount = (0, _keys2.default)(hiddenFields).filter(function (key) {
        return hiddenFields[key];
      }).length;
      return _react2.default.createElement(
        _Toolbar2.default,
        null,
        _react2.default.createElement(
          _Toolbar.ToolbarButton,
          { onClick: this.handleHideFieldsButtonClick },
          _react2.default.createElement(_VisibilityOff2.default, null),
          _react2.default.createElement(_reactIntl.FormattedMessage, (0, _extends3.default)({}, messages.hideFieldsLabel, { values: { count: hiddenCount } }))
        ),
        _react2.default.createElement(_HiddenFieldsMenu2.default, {
          anchorEl: this.state.hideFieldsButtonEl,
          open: this.state.hideFieldsOpen,
          fields: fields,
          onShowAll: onShowAll,
          onHideAll: onHideAll,
          onToggle: onToggleFieldVisibility,
          onRequestClose: function onRequestClose() {
            return _this2.setState({ hideFieldsOpen: false });
          } }),
        _react2.default.createElement(
          _Toolbar.ToolbarButton,
          { onClick: requestPrint },
          _react2.default.createElement(_Print2.default, null),
          'Print'
        )
      );
    }
  }]);
  return ReportToolbar;
}(_react2.default.Component);

ReportToolbar.defaultProps = {
  hiddenFields: {}
};

ReportToolbar.propTypes = {
  fieldAnalysis: _prop_types.fieldAnalysis.isRequired,
  hiddenFields: _propTypes2.default.objectOf(_propTypes2.default.bool),
  requestPrint: _propTypes2.default.func.isRequired
};

function getFieldList(fieldAnalysis, hiddenFields) {
  return (0, _keys2.default)(fieldAnalysis.properties).map(function (key) {
    return { key: key, hidden: !!hiddenFields[key] };
  });
}

exports.default = ReportToolbar;
//# sourceMappingURL=ReportToolbar.js.map