"use strict";

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

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.regexp.to-string");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _construct = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/reflect/construct"));

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

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

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

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

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

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

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

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

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _Dialog = _interopRequireDefault(require("@material-ui/core/Dialog"));

var _DialogActions = _interopRequireDefault(require("@material-ui/core/DialogActions"));

var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));

var _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));

var _InputLabel = _interopRequireDefault(require("@material-ui/core/InputLabel"));

var _Input = _interopRequireDefault(require("@material-ui/core/Input"));

var _FormControl = _interopRequireDefault(require("@material-ui/core/FormControl"));

var _Select = _interopRequireDefault(require("@material-ui/core/Select"));

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

var _ToolbarButton = _interopRequireDefault(require("../internal/ToolbarButton"));

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 = (0, _construct.default)(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

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

var messages = (0, _reactIntl.defineMessages)({
  // Title of print settings dialog
  dialogTitle: {
    "id": "Nl8clw==",
    "defaultMessage": 'Print settings'
  },
  // Label for paper size selection field
  paperSize: {
    "id": "0eDMPQ==",
    "defaultMessage": 'Paper size'
  },
  // Button label to close print settings dialog
  close: {
    "id": "cVJ0Dg==",
    "defaultMessage": 'Close'
  },
  // Button label to print a report
  print: {
    "id": "krWEbA==",
    "defaultMessage": 'Print'
  }
});
/*:: type Props = {
  requestPrint: () => void,
  changePaperSize: (paperSize: PaperSize) => void,
  paperSize: PaperSize
}*/

/*:: type State = {
  dialogOpen: boolean
}*/

var PrintButton = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2.default)(PrintButton, _React$Component);

  var _super = _createSuper(PrintButton);

  function PrintButton() {
    var _context;

    var _this;

    (0, _classCallCheck2.default)(this, PrintButton);

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

    _this = _super.call.apply(_super, (0, _concat.default)(_context = [this]).call(_context, args));
    _this.state = {
      dialogOpen: false
    };

    _this.handleKeyDown = function (event
    /*: SyntheticKeyboardEvent<HTMLElement>*/
    ) {
      if (!(event.key === 'p' && event.metaKey)) return;
      event.preventDefault();
      window.addEventListener('keyup', _this.handleKeyUp);
    };

    _this.handleKeyUp = function (event
    /*: SyntheticKeyboardEvent<HTMLElement>*/
    ) {
      window.removeEventListener('keyup', _this.handleKeyUp);

      if (_this.state.dialogOpen) {
        _this.props.requestPrint();
      } else {
        _this.openDialog();
      }
    };

    _this.openDialog = function () {
      return _this.setState({
        dialogOpen: true
      });
    };

    _this.closeDialog = function () {
      return _this.setState({
        dialogOpen: false
      });
    };

    _this.handleChangePaperSize = function (e
    /*: SyntheticInputEvent<HTMLSelectElement>*/
    ) {
      // $FlowFixMe - Flow doesn't recognize value being one of options
      var value
      /*: PaperSize*/
      = e.currentTarget.value;

      _this.props.changePaperSize(value);
    };

    return _this;
  }

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

      var _this$props = this.props,
          requestPrint = _this$props.requestPrint,
          paperSize = _this$props.paperSize;
      var dialogOpen = this.state.dialogOpen;
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ToolbarButton.default, {
        onClick: this.openDialog
      }, /*#__PURE__*/_react.default.createElement(_Print.default, null), /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.print)), /*#__PURE__*/_react.default.createElement(_Dialog.default, {
        open: dialogOpen,
        onClose: this.closeDialog,
        fullWidth: true,
        maxWidth: "xs",
        className: "d-print-none"
      }, /*#__PURE__*/_react.default.createElement(_DialogTitle.default, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.dialogTitle)), /*#__PURE__*/_react.default.createElement(_DialogContent.default, null, /*#__PURE__*/_react.default.createElement(_FormControl.default, null, /*#__PURE__*/_react.default.createElement(_InputLabel.default, {
        htmlFor: "paper-size"
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.paperSize)), /*#__PURE__*/_react.default.createElement(_Select.default, {
        native: true,
        value: paperSize,
        onChange: this.handleChangePaperSize,
        input: /*#__PURE__*/_react.default.createElement(_Input.default, {
          id: "paper-size"
        })
      }, /*#__PURE__*/_react.default.createElement("option", {
        value: "a4"
      }, "A4 (210mm x 297mm)"), /*#__PURE__*/_react.default.createElement("option", {
        value: "letter"
      }, "Letter (8.5\" x 11\")")))), /*#__PURE__*/_react.default.createElement(_DialogActions.default, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
        onClick: this.closeDialog,
        color: "primary"
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.close)), /*#__PURE__*/_react.default.createElement(_Button.default, {
        onClick: function onClick() {
          _this2.closeDialog();

          requestPrint();
        },
        color: "primary"
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.print)))));
    }
  }]);
  return PrintButton;
}(_react.default.Component);

var _default = PrintButton;
exports.default = _default;
//# sourceMappingURL=PrintButton.js.map