import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef } from 'react';
import ReactInputMask 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__*/forwardRef(function (props, forwardedRef) {
  var readOnly = props.readOnly,
      disabled = props.disabled;

  var beforeMaskedValueChange = function beforeMaskedValueChange(_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 /*#__PURE__*/React.createElement(ReactInputMask, _extends({}, props, {
    mask: "99:99",
    maskChar: "0",
    beforeMaskedValueChange: 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))
    }));
  });
});
TimeInput.displayName = 'TimeInput';
export default TimeInput;