'use strict';

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

exports.__esModule = true;
exports.HeaderToolbar = void 0;

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

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

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

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

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

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

var _Navbar = _interopRequireDefault(require("react-bootstrap/lib/Navbar"));

var _Row = _interopRequireDefault(require("react-bootstrap/lib/Row"));

var _Col = _interopRequireDefault(require("react-bootstrap/lib/Col"));

var _Icon = require("./Icon");

var _Button = require("./Button");

var _utils = require("../../utils");

var _index = require("../index");

var _FocusHelper = require("../utils/FocusHelper");

var EMPTY_DETAIL_SETTING_FOR_ARIA_LABEL = 'TRANSLATE:DETAIL_SETTING_FOR_ARIA_LABEL';

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

  function HeaderToolbar() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "focusHelper", new _FocusHelper.FocusHelper());
    return _this;
  }

  var _proto = HeaderToolbar.prototype;

  _proto.componentDidMount = function componentDidMount() {
    if (this.props.autoFocus) {
      this.focusHelper.focus();
    }
  };

  _proto.handleBackIconClick = function handleBackIconClick() {
    var history = this.context.history || (0, _index.getSilentRouterHistory)();
    var handleHistory = true;

    if (this.props.onBackButtonClick) {
      handleHistory = this.props.onBackButtonClick();
    }

    if (handleHistory === false) {
      return;
    }

    if (this.props.backLink) {
      history.push(this.props.backLink);
    } else {
      history.goBack();
    }
  };

  _proto.render = function render() {
    var buttonCol = this.props.buttonCol;
    var textCol = 12 - buttonCol * 2;
    var _this$props = this.props,
        title = _this$props.title,
        detailSettingForArialLabel = _this$props.detailSettingForArialLabel;
    return _react.default.createElement(_Navbar.default, {
      fluid: true,
      className: "header-toolbar",
      "data-test-automation-id": this.props.testAutomationId,
      tabIndex: 0,
      ref: this.focusHelper.setEl,
      "aria-label": (0, _utils.translate)(detailSettingForArialLabel, {
        detailName: title
      })
    }, _react.default.createElement(_Row.default, null, buttonCol > 0 && _react.default.createElement(_Col.default, {
      xs: buttonCol
    }, _react.default.createElement(_Navbar.default.Form, {
      pullLeft: true
    }, _react.default.createElement(_Button.Button, {
      bsStyle: "link",
      onClick: this.handleBackIconClick.bind(this),
      "data-test-automation-id": this.props.backButtonTestAutomationId
    }, _react.default.createElement(_Icon.Icon, {
      name: "chevron-left-big",
      className: "back-icon"
    }), (0, _utils.translate)(this.props.backText)))), _react.default.createElement(_Col.default, {
      xs: textCol
    }, _react.default.createElement(_Navbar.default.Header, null, _react.default.createElement(_Navbar.default.Brand, null, _react.default.createElement("h1", {
      "data-test-automation-id": "title"
    }, this.props.title)))), _react.default.createElement(_Col.default, {
      xsOffset: 8
    }, this.props.children)));
  };

  (0, _createClass2.default)(HeaderToolbar, null, [{
    key: "contextTypes",
    get: function get() {
      return {
        history: _propTypes.default.object,
        buttonCol: _propTypes.default.number
      };
    }
  }]);
  return HeaderToolbar;
}(_react.default.Component);

exports.HeaderToolbar = HeaderToolbar;
(0, _defineProperty2.default)(HeaderToolbar, "propTypes", {
  testAutomationId: _propTypes.default.string,
  backButtonTestAutomationId: _propTypes.default.string,
  title: _propTypes.default.string,
  buttonCol: _propTypes.default.number,
  backText: _propTypes.default.string,
  onBackButtonClick: _propTypes.default.func,
  backLink: _propTypes.default.string,
  children: _propTypes.default.node,
  autoFocus: _propTypes.default.bool,
  detailSettingForArialLabel: _propTypes.default.string
});
(0, _defineProperty2.default)(HeaderToolbar, "defaultProps", {
  testAutomationId: 'headerToolbar',
  backButtonTestAutomationId: 'buttonBack',
  title: '',
  buttonCol: 2,
  backText: 'TRANSLATE:BACK',
  backLink: '',
  onBackButtonClick: null,
  children: null,
  autoFocus: true,
  detailSettingForArialLabel: EMPTY_DETAIL_SETTING_FOR_ARIA_LABEL
});