import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { forwardRef, PureComponent } from 'react';
import InputMask from 'react-input-mask';
import Icon from '../icon';
import SingleLineInputBase from './SingleLineInputBase';
import { IconTimerSmallOutline } from '@teamleader/ui-icons';

var isValidTime = function isValidTime(input) {
  return /([0-1][0-9]|[2][0-3]):([0-5][0-9])/.test(input);
};

var TimeInput = /*#__PURE__*/function (_PureComponent) {
  _inherits(TimeInput, _PureComponent);

  var _super = _createSuper(TimeInput);

  function TimeInput() {
    var _this;

    _classCallCheck(this, TimeInput);

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

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "beforeMaskedValueChange", function (_ref, _ref2) {
      var newValue = _ref.value,
          selection = _ref.selection;
      var oldValue = _ref2.value;

      if (!isValidTime(newValue)) {
        return {
          value: oldValue,
          selection: selection
        };
      }

      return {
        value: newValue,
        selection: selection
      };
    });

    return _this;
  }

  _createClass(TimeInput, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          disabled = _this$props.disabled,
          readOnly = _this$props.readOnly,
          forwardedRef = _this$props.forwardedRef;
      return /*#__PURE__*/React.createElement(InputMask, _extends({}, this.props, {
        mask: "99:99",
        maskChar: "0",
        beforeMaskedValueChange: this.beforeMaskedValueChange
      }), function (inputProps) {
        return /*#__PURE__*/React.createElement(SingleLineInputBase, _extends({}, inputProps, {
          ref: forwardedRef,
          autoComplete: "off",
          readOnly: readOnly,
          disabled: disabled,
          prefix: /*#__PURE__*/React.createElement(Icon, {
            color: "neutral",
            tint: "darkest"
          }, /*#__PURE__*/React.createElement(IconTimerSmallOutline, null))
        }));
      });
    }
  }]);

  return TimeInput;
}(PureComponent);
/** @type {React.ComponentType<any>} */


var ForwardedTimeInput = /*#__PURE__*/forwardRef(function (props, ref) {
  return /*#__PURE__*/React.createElement(TimeInput, _extends({}, props, {
    forwardedRef: ref
  }));
});
ForwardedTimeInput.displayName = 'TimeInput';
export default ForwardedTimeInput;