'use strict';

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

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

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

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 _classnames = _interopRequireDefault(require("classnames"));

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

var MODE_ALL = 'MODE_ALL';
var MODE_SELECTED = 'MODE_SELECTED';

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

  function SelectedRecordsSwitcher() {
    return _React$Component.apply(this, arguments) || this;
  }

  var _proto = SelectedRecordsSwitcher.prototype;

  _proto.getShowSelectedCounter = function getShowSelectedCounter() {
    var _this$props = this.props,
        selectedCount = _this$props.selectedCount,
        maxCount = _this$props.maxCount;
    var className = (0, _classnames.default)({
      limitExceeded: maxCount && selectedCount > maxCount
    });
    return _react.default.createElement("span", {
      className: className
    }, "(", _react.default.createElement("span", {
      "data-test-automation-id": "count"
    }, selectedCount, maxCount ? '/' + maxCount : null), ")");
  };

  _proto.getShowElement = function getShowElement(mode) {
    var _this = this;

    var clickable = this.props.mode !== mode;
    var isModeAll = mode === MODE_ALL;
    var testAutomationId = isModeAll ? 'showAllButton' : 'showSelectedButton';
    var text = isModeAll ? (0, _utils.translate)(this.props.showAllText) : _react.default.createElement("span", null, (0, _utils.translate)(this.props.showSelectedText), " ", this.getShowSelectedCounter());

    if (clickable) {
      return _react.default.createElement("a", {
        href: "#",
        onClick: function onClick(e) {
          e.preventDefault();

          _this.props.onChange(mode);

          return false;
        },
        "data-test-automation-id": testAutomationId
      }, text);
    }

    return _react.default.createElement("strong", {
      className: "text-primary",
      "data-test-automation-id": testAutomationId
    }, text);
  };

  _proto.render = function render() {
    return _react.default.createElement("div", {
      "data-test-automation-id": this.props.testAutomationId,
      className: (0, _classnames.default)('store-selected-records-switcher', this.props.className)
    }, this.getShowElement(MODE_ALL), _react.default.createElement("span", {
      "aria-hidden": "true"
    }, "|"), this.getShowElement(MODE_SELECTED));
  };

  (0, _createClass2.default)(SelectedRecordsSwitcher, null, [{
    key: "MODE_ALL",
    get: function get() {
      return MODE_ALL;
    }
  }, {
    key: "MODE_SELECTED",
    get: function get() {
      return MODE_SELECTED;
    }
  }]);
  return SelectedRecordsSwitcher;
}(_react.default.Component);

exports.SelectedRecordsSwitcher = SelectedRecordsSwitcher;
(0, _defineProperty2.default)(SelectedRecordsSwitcher, "propTypes", {
  className: _propTypes.default.string,
  testAutomationId: _propTypes.default.string,
  showAllText: _propTypes.default.string,
  showSelectedText: _propTypes.default.string,
  selectedCount: _propTypes.default.number.isRequired,
  maxCount: _propTypes.default.number,
  mode: _propTypes.default.oneOf([MODE_ALL, MODE_SELECTED]).isRequired,
  onChange: _propTypes.default.func.isRequired
});
(0, _defineProperty2.default)(SelectedRecordsSwitcher, "defaultProps", {
  className: null,
  testAutomationId: null,
  maxCount: null,
  showAllText: 'TRANSLATE:SHOW_ALL',
  showSelectedText: 'TRANSLATE:SHOW_SELECTED'
});