import { g as getDefaultExportFromCjs } from "./dateFns.js";
import React__default from "react";
var lib = {};
(function(exports) {
  Object.defineProperty(exports, "__esModule", { value: true });
  exports["default"] = void 0;
  var _react = _interopRequireWildcard(React__default);
  var _excluded = ["placeholder", "separator", "isLastChild", "inputStyle", "focus", "isDisabled", "hasErrored", "errorStyle", "focusStyle", "disabledStyle", "shouldAutoFocus", "isInputNum", "index", "value", "className", "isInputSecure"];
  function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function")
      return null;
    var cacheBabelInterop = /* @__PURE__ */ new WeakMap();
    var cacheNodeInterop = /* @__PURE__ */ new WeakMap();
    return (_getRequireWildcardCache = function _getRequireWildcardCache2(nodeInterop2) {
      return nodeInterop2 ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
  }
  function _interopRequireWildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
      return obj;
    }
    if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
      return { "default": obj };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
      return cache.get(obj);
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var key in obj) {
      if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
        var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
        if (desc && (desc.get || desc.set)) {
          Object.defineProperty(newObj, key, desc);
        } else {
          newObj[key] = obj[key];
        }
      }
    }
    newObj["default"] = obj;
    if (cache) {
      cache.set(obj, newObj);
    }
    return newObj;
  }
  function _extends() {
    _extends = Object.assign || function(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i];
        for (var key in source) {
          if (Object.prototype.hasOwnProperty.call(source, key)) {
            target[key] = source[key];
          }
        }
      }
      return target;
    };
    return _extends.apply(this, arguments);
  }
  function _objectWithoutProperties(source, excluded) {
    if (source == null)
      return {};
    var target = _objectWithoutPropertiesLoose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
      var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
      for (i = 0; i < sourceSymbolKeys.length; i++) {
        key = sourceSymbolKeys[i];
        if (excluded.indexOf(key) >= 0)
          continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key))
          continue;
        target[key] = source[key];
      }
    }
    return target;
  }
  function _objectWithoutPropertiesLoose(source, excluded) {
    if (source == null)
      return {};
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
      key = sourceKeys[i];
      if (excluded.indexOf(key) >= 0)
        continue;
      target[key] = source[key];
    }
    return target;
  }
  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }
  function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor)
        descriptor.writable = true;
      Object.defineProperty(target, descriptor.key, descriptor);
    }
  }
  function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps)
      _defineProperties(Constructor.prototype, protoProps);
    if (staticProps)
      _defineProperties(Constructor, staticProps);
    return Constructor;
  }
  function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
      throw new TypeError("Super expression must either be null or a function");
    }
    subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });
    if (superClass)
      _setPrototypeOf(subClass, superClass);
  }
  function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf2(o2, p2) {
      o2.__proto__ = p2;
      return o2;
    };
    return _setPrototypeOf(o, p);
  }
  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 _possibleConstructorReturn(self, call) {
    if (call && (_typeof(call) === "object" || typeof call === "function")) {
      return call;
    }
    return _assertThisInitialized(self);
  }
  function _assertThisInitialized(self) {
    if (self === void 0) {
      throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }
    return self;
  }
  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;
    }
  }
  function _getPrototypeOf(o) {
    _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf2(o2) {
      return o2.__proto__ || Object.getPrototypeOf(o2);
    };
    return _getPrototypeOf(o);
  }
  function _defineProperty(obj, key, value) {
    if (key in obj) {
      Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
    } else {
      obj[key] = value;
    }
    return obj;
  }
  function _typeof(obj) {
    "@babel/helpers - typeof";
    if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
      _typeof = function _typeof2(obj2) {
        return typeof obj2;
      };
    } else {
      _typeof = function _typeof2(obj2) {
        return obj2 && typeof Symbol === "function" && obj2.constructor === Symbol && obj2 !== Symbol.prototype ? "symbol" : typeof obj2;
      };
    }
    return _typeof(obj);
  }
  var BACKSPACE = 8;
  var LEFT_ARROW = 37;
  var RIGHT_ARROW = 39;
  var DELETE = 46;
  var SPACEBAR = 32;
  var isStyleObject = function isStyleObject2(obj) {
    return _typeof(obj) === "object";
  };
  var SingleOtpInput = function(_PureComponent) {
    _inherits(SingleOtpInput2, _PureComponent);
    var _super = _createSuper(SingleOtpInput2);
    function SingleOtpInput2(props) {
      var _this;
      _classCallCheck(this, SingleOtpInput2);
      _this = _super.call(this, props);
      _defineProperty(_assertThisInitialized(_this), "getClasses", function() {
        for (var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++) {
          classes[_key] = arguments[_key];
        }
        return classes.filter(function(c) {
          return !isStyleObject(c) && c !== false;
        }).join(" ");
      });
      _defineProperty(_assertThisInitialized(_this), "getType", function() {
        var _this$props = _this.props, isInputSecure = _this$props.isInputSecure, isInputNum = _this$props.isInputNum;
        if (isInputSecure) {
          return "password";
        }
        if (isInputNum) {
          return "tel";
        }
        return "text";
      });
      _this.input = _react["default"].createRef();
      return _this;
    }
    _createClass(SingleOtpInput2, [{ key: "componentDidMount", value: function componentDidMount() {
      var _this$props2 = this.props, focus = _this$props2.focus, shouldAutoFocus = _this$props2.shouldAutoFocus;
      var inputEl = this.input.current;
      if (inputEl && focus && shouldAutoFocus) {
        inputEl.focus();
      }
    } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) {
      var focus = this.props.focus;
      var inputEl = this.input.current;
      if (prevProps.focus !== focus && inputEl && focus) {
        inputEl.focus();
        inputEl.select();
      }
    } }, { key: "render", value: function render() {
      var _this$props3 = this.props, placeholder = _this$props3.placeholder, separator = _this$props3.separator, isLastChild = _this$props3.isLastChild, inputStyle = _this$props3.inputStyle, focus = _this$props3.focus, isDisabled = _this$props3.isDisabled, hasErrored = _this$props3.hasErrored, errorStyle = _this$props3.errorStyle, focusStyle = _this$props3.focusStyle, disabledStyle = _this$props3.disabledStyle;
      _this$props3.shouldAutoFocus;
      var isInputNum = _this$props3.isInputNum, index = _this$props3.index, value = _this$props3.value, className = _this$props3.className;
      _this$props3.isInputSecure;
      var rest = _objectWithoutProperties(_this$props3, _excluded);
      return _react["default"].createElement("div", { className, style: { display: "flex", alignItems: "center" } }, _react["default"].createElement("input", _extends({ "aria-label": "".concat(index === 0 ? "Please enter verification code. " : "").concat(isInputNum ? "Digit" : "Character", " ").concat(index + 1), autoComplete: "off", style: Object.assign({ width: "1em", textAlign: "center" }, isStyleObject(inputStyle) && inputStyle, focus && isStyleObject(focusStyle) && focusStyle, isDisabled && isStyleObject(disabledStyle) && disabledStyle, hasErrored && isStyleObject(errorStyle) && errorStyle), placeholder, className: this.getClasses(inputStyle, focus && focusStyle, isDisabled && disabledStyle, hasErrored && errorStyle), type: this.getType(), maxLength: "1", ref: this.input, disabled: isDisabled, value: value ? value : "" }, rest)), !isLastChild && separator);
    } }]);
    return SingleOtpInput2;
  }(_react.PureComponent);
  var OtpInput2 = function(_Component) {
    _inherits(OtpInput3, _Component);
    var _super2 = _createSuper(OtpInput3);
    function OtpInput3() {
      var _this2;
      _classCallCheck(this, OtpInput3);
      for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }
      _this2 = _super2.call.apply(_super2, [this].concat(args));
      _defineProperty(_assertThisInitialized(_this2), "state", { activeInput: 0 });
      _defineProperty(_assertThisInitialized(_this2), "getOtpValue", function() {
        return _this2.props.value ? _this2.props.value.toString().split("") : [];
      });
      _defineProperty(_assertThisInitialized(_this2), "getPlaceholderValue", function() {
        var _this2$props = _this2.props, placeholder = _this2$props.placeholder, numInputs = _this2$props.numInputs;
        if (typeof placeholder === "string") {
          if (placeholder.length === numInputs) {
            return placeholder;
          }
          if (placeholder.length > 0) {
            console.error("Length of the placeholder should be equal to the number of inputs.");
          }
        }
      });
      _defineProperty(_assertThisInitialized(_this2), "handleOtpChange", function(otp) {
        var onChange = _this2.props.onChange;
        var otpValue = otp.join("");
        onChange(otpValue);
      });
      _defineProperty(_assertThisInitialized(_this2), "isInputValueValid", function(value) {
        var isTypeValid = _this2.props.isInputNum ? !isNaN(parseInt(value, 10)) : typeof value === "string";
        return isTypeValid && value.trim().length === 1;
      });
      _defineProperty(_assertThisInitialized(_this2), "focusInput", function(input) {
        var numInputs = _this2.props.numInputs;
        var activeInput = Math.max(Math.min(numInputs - 1, input), 0);
        _this2.setState({ activeInput });
      });
      _defineProperty(_assertThisInitialized(_this2), "focusNextInput", function() {
        var activeInput = _this2.state.activeInput;
        _this2.focusInput(activeInput + 1);
      });
      _defineProperty(_assertThisInitialized(_this2), "focusPrevInput", function() {
        var activeInput = _this2.state.activeInput;
        _this2.focusInput(activeInput - 1);
      });
      _defineProperty(_assertThisInitialized(_this2), "changeCodeAtFocus", function(value) {
        var activeInput = _this2.state.activeInput;
        var otp = _this2.getOtpValue();
        otp[activeInput] = value[0];
        _this2.handleOtpChange(otp);
      });
      _defineProperty(_assertThisInitialized(_this2), "handleOnPaste", function(e) {
        e.preventDefault();
        var activeInput = _this2.state.activeInput;
        var _this2$props2 = _this2.props, numInputs = _this2$props2.numInputs, isDisabled = _this2$props2.isDisabled;
        if (isDisabled) {
          return;
        }
        var otp = _this2.getOtpValue();
        var nextActiveInput = activeInput;
        var pastedData = e.clipboardData.getData("text/plain").slice(0, numInputs - activeInput).split("");
        for (var pos = 0; pos < numInputs; ++pos) {
          if (pos >= activeInput && pastedData.length > 0) {
            otp[pos] = pastedData.shift();
            nextActiveInput++;
          }
        }
        _this2.setState({ activeInput: nextActiveInput }, function() {
          _this2.focusInput(nextActiveInput);
          _this2.handleOtpChange(otp);
        });
      });
      _defineProperty(_assertThisInitialized(_this2), "handleOnChange", function(e) {
        var value = e.target.value;
        if (_this2.isInputValueValid(value)) {
          _this2.changeCodeAtFocus(value);
        }
      });
      _defineProperty(_assertThisInitialized(_this2), "handleOnKeyDown", function(e) {
        if (e.keyCode === BACKSPACE || e.key === "Backspace") {
          e.preventDefault();
          _this2.changeCodeAtFocus("");
          _this2.focusPrevInput();
        } else if (e.keyCode === DELETE || e.key === "Delete") {
          e.preventDefault();
          _this2.changeCodeAtFocus("");
        } else if (e.keyCode === LEFT_ARROW || e.key === "ArrowLeft") {
          e.preventDefault();
          _this2.focusPrevInput();
        } else if (e.keyCode === RIGHT_ARROW || e.key === "ArrowRight") {
          e.preventDefault();
          _this2.focusNextInput();
        } else if (e.keyCode === SPACEBAR || e.key === " " || e.key === "Spacebar" || e.key === "Space") {
          e.preventDefault();
        }
      });
      _defineProperty(_assertThisInitialized(_this2), "handleOnInput", function(e) {
        if (_this2.isInputValueValid(e.target.value)) {
          _this2.focusNextInput();
        } else {
          if (!_this2.props.isInputNum) {
            var nativeEvent = e.nativeEvent;
            if (nativeEvent.data === null && nativeEvent.inputType === "deleteContentBackward") {
              e.preventDefault();
              _this2.changeCodeAtFocus("");
              _this2.focusPrevInput();
            }
          }
        }
      });
      _defineProperty(_assertThisInitialized(_this2), "renderInputs", function() {
        var activeInput = _this2.state.activeInput;
        var _this2$props3 = _this2.props, numInputs = _this2$props3.numInputs, inputStyle = _this2$props3.inputStyle, focusStyle = _this2$props3.focusStyle, separator = _this2$props3.separator, isDisabled = _this2$props3.isDisabled, disabledStyle = _this2$props3.disabledStyle, hasErrored = _this2$props3.hasErrored, errorStyle = _this2$props3.errorStyle, shouldAutoFocus = _this2$props3.shouldAutoFocus, isInputNum = _this2$props3.isInputNum, isInputSecure = _this2$props3.isInputSecure, className = _this2$props3.className;
        var inputs = [];
        var otp = _this2.getOtpValue();
        var placeholder = _this2.getPlaceholderValue();
        var dataCy = _this2.props["data-cy"];
        var dataTestId = _this2.props["data-testid"];
        var _loop = function _loop2(i2) {
          inputs.push(_react["default"].createElement(SingleOtpInput, { placeholder: placeholder && placeholder[i2], key: i2, index: i2, focus: activeInput === i2, value: otp && otp[i2], onChange: _this2.handleOnChange, onKeyDown: _this2.handleOnKeyDown, onInput: _this2.handleOnInput, onPaste: _this2.handleOnPaste, onFocus: function onFocus(e) {
            _this2.setState({ activeInput: i2 });
            e.target.select();
          }, onBlur: function onBlur() {
            return _this2.setState({ activeInput: -1 });
          }, separator, inputStyle, focusStyle, isLastChild: i2 === numInputs - 1, isDisabled, disabledStyle, hasErrored, errorStyle, shouldAutoFocus, isInputNum, isInputSecure, className, "data-cy": dataCy && "".concat(dataCy, "-").concat(i2), "data-testid": dataTestId && "".concat(dataTestId, "-").concat(i2) }));
        };
        for (var i = 0; i < numInputs; i++) {
          _loop(i);
        }
        return inputs;
      });
      return _this2;
    }
    _createClass(OtpInput3, [{ key: "render", value: function render() {
      var containerStyle = this.props.containerStyle;
      return _react["default"].createElement("div", { style: Object.assign({ display: "flex" }, isStyleObject(containerStyle) && containerStyle), className: !isStyleObject(containerStyle) ? containerStyle : "" }, this.renderInputs());
    } }]);
    return OtpInput3;
  }(_react.Component);
  _defineProperty(OtpInput2, "defaultProps", { numInputs: 4, onChange: function onChange(otp) {
    return console.log(otp);
  }, isDisabled: false, shouldAutoFocus: false, value: "", isInputSecure: false });
  var _default = OtpInput2;
  exports["default"] = _default;
})(lib);
var OtpInput = /* @__PURE__ */ getDefaultExportFromCjs(lib);
export { OtpInput as O };
