"use strict";

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

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

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 _lodash = _interopRequireDefault(require("lodash"));

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

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

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

var _Block = require("../../Block");

var _constants = require("../../../../utils/constants");

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

  function TimeNumberEditor() {
    var _this;

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

    _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleIncrease", function () {
      return _this.onChangeValue(true);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleDecrease", function () {
      return _this.onChangeValue(false);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onChangeValue", function (isIncrease) {
      var newValue = _this.getChangeValue(isIncrease);

      _this.props.onChange(newValue);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleKeyDownIncreaseButton", function (e) {
      if (e.key === _constants.KeyNames.Tab && e.shiftKey) {
        _this.props.onShiftTabAwait();
      }
    });
    return _this;
  }

  var _proto = TimeNumberEditor.prototype;

  _proto.getChangeValue = function getChangeValue(isIncrease) {
    if (isIncrease === void 0) {
      isIncrease = true;
    }

    var value = this.props.value;

    if (_lodash.default.isString(value)) {
      value = parseInt(value);
    }

    if (isIncrease) {
      value = value + 1;
    } else {
      value = value - 1;
    }

    return value;
  };

  _proto.render = function render() {
    var _this$props = this.props,
        value = _this$props.value,
        testAutomationId = _this$props.testAutomationId,
        increaseButtonLabel = _this$props.increaseButtonLabel,
        decreaseButtonLabel = _this$props.decreaseButtonLabel,
        valueDisplayFormat = _this$props.valueDisplayFormat;
    return _react.default.createElement(_Block.Block, {
      testAutomationId: testAutomationId
    }, _react.default.createElement(_Button.Button, {
      testAutomationId: "increase",
      bsStyle: "link",
      block: true,
      onClick: this.handleIncrease,
      onKeyDown: this.handleKeyDownIncreaseButton,
      title: increaseButtonLabel,
      "aria-label": increaseButtonLabel
    }, _react.default.createElement(_Icon.Icon, {
      name: "chevron-up-big"
    })), _react.default.createElement("div", {
      className: "text-center time-text"
    }, _react.default.createElement("strong", null, valueDisplayFormat(value))), _react.default.createElement(_Button.Button, {
      testAutomationId: "decrease",
      bsStyle: "link",
      block: true,
      onClick: this.handleDecrease,
      title: decreaseButtonLabel,
      "aria-label": decreaseButtonLabel
    }, _react.default.createElement(_Icon.Icon, {
      name: "chevron-down-big"
    })));
  };

  return TimeNumberEditor;
}(_react.default.PureComponent);

exports.TimeNumberEditor = TimeNumberEditor;
(0, _defineProperty2.default)(TimeNumberEditor, "propTypes", {
  increaseButtonLabel: _propTypes.default.string.isRequired,
  decreaseButtonLabel: _propTypes.default.string.isRequired,
  value: _propTypes.default.number,
  onChange: _propTypes.default.func.isRequired,
  onShiftTabAwait: _propTypes.default.func,
  testAutomationId: _propTypes.default.string.isRequired,
  valueDisplayFormat: _propTypes.default.func
});
(0, _defineProperty2.default)(TimeNumberEditor, "defaultProps", {
  value: 0,
  valueDisplayFormat: function valueDisplayFormat(value) {
    return value;
  },
  onShiftTabAwait: function onShiftTabAwait() {
    return true;
  }
});