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 _ from 'lodash';
import PropTypes from 'prop-types';
import { Button } from '../../Button';
import { Icon } from '../../Icon';
import { Block } from '../../Block';
import { KeyNames } from '../../../../utils/constants';
export var TimeNumberEditor =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "handleIncrease", function () {
      return _this.onChangeValue(true);
    });

    _defineProperty(_assertThisInitialized(_this), "handleDecrease", function () {
      return _this.onChangeValue(false);
    });

    _defineProperty(_assertThisInitialized(_this), "onChangeValue", function (isIncrease) {
      var newValue = _this.getChangeValue(isIncrease);

      _this.props.onChange(newValue);
    });

    _defineProperty(_assertThisInitialized(_this), "handleKeyDownIncreaseButton", function (e) {
      if (e.key === 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 (_.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.createElement(Block, {
      testAutomationId: testAutomationId
    }, React.createElement(Button, {
      testAutomationId: "increase",
      bsStyle: "link",
      block: true,
      onClick: this.handleIncrease,
      onKeyDown: this.handleKeyDownIncreaseButton,
      title: increaseButtonLabel,
      "aria-label": increaseButtonLabel
    }, React.createElement(Icon, {
      name: "chevron-up-big"
    })), React.createElement("div", {
      className: "text-center time-text"
    }, React.createElement("strong", null, valueDisplayFormat(value))), React.createElement(Button, {
      testAutomationId: "decrease",
      bsStyle: "link",
      block: true,
      onClick: this.handleDecrease,
      title: decreaseButtonLabel,
      "aria-label": decreaseButtonLabel
    }, React.createElement(Icon, {
      name: "chevron-down-big"
    })));
  };

  return TimeNumberEditor;
}(React.PureComponent);

_defineProperty(TimeNumberEditor, "propTypes", {
  increaseButtonLabel: PropTypes.string.isRequired,
  decreaseButtonLabel: PropTypes.string.isRequired,
  value: PropTypes.number,
  onChange: PropTypes.func.isRequired,
  onShiftTabAwait: PropTypes.func,
  testAutomationId: PropTypes.string.isRequired,
  valueDisplayFormat: PropTypes.func
});

_defineProperty(TimeNumberEditor, "defaultProps", {
  value: 0,
  valueDisplayFormat: function valueDisplayFormat(value) {
    return value;
  },
  onShiftTabAwait: function onShiftTabAwait() {
    return true;
  }
});