'use strict';

import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import Navbar from 'react-bootstrap/lib/Navbar';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import { Icon } from './Icon';
import { Button } from './Button';
import { translate } from '../../utils';
import { getSilentRouterHistory } from '../index';
import { FocusHelper } from '../utils/FocusHelper';
var EMPTY_DETAIL_SETTING_FOR_ARIA_LABEL = 'TRANSLATE:DETAIL_SETTING_FOR_ARIA_LABEL';
export var HeaderToolbar =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "focusHelper", new 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 || 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.createElement(Navbar, {
      fluid: true,
      className: "header-toolbar",
      "data-test-automation-id": this.props.testAutomationId,
      tabIndex: 0,
      ref: this.focusHelper.setEl,
      "aria-label": translate(detailSettingForArialLabel, {
        detailName: title
      })
    }, React.createElement(Row, null, buttonCol > 0 && React.createElement(Col, {
      xs: buttonCol
    }, React.createElement(Navbar.Form, {
      pullLeft: true
    }, React.createElement(Button, {
      bsStyle: "link",
      onClick: this.handleBackIconClick.bind(this),
      "data-test-automation-id": this.props.backButtonTestAutomationId
    }, React.createElement(Icon, {
      name: "chevron-left-big",
      className: "back-icon"
    }), translate(this.props.backText)))), React.createElement(Col, {
      xs: textCol
    }, React.createElement(Navbar.Header, null, React.createElement(Navbar.Brand, null, React.createElement("h1", {
      "data-test-automation-id": "title"
    }, this.props.title)))), React.createElement(Col, {
      xsOffset: 8
    }, this.props.children)));
  };

  _createClass(HeaderToolbar, null, [{
    key: "contextTypes",
    get: function get() {
      return {
        history: PropTypes.object,
        buttonCol: PropTypes.number
      };
    }
  }]);

  return HeaderToolbar;
}(React.Component);

_defineProperty(HeaderToolbar, "propTypes", {
  testAutomationId: PropTypes.string,
  backButtonTestAutomationId: PropTypes.string,
  title: PropTypes.string,
  buttonCol: PropTypes.number,
  backText: PropTypes.string,
  onBackButtonClick: PropTypes.func,
  backLink: PropTypes.string,
  children: PropTypes.node,
  autoFocus: PropTypes.bool,
  detailSettingForArialLabel: PropTypes.string
});

_defineProperty(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
});