import { useEffect } from 'react';
import { useDidMountEffect } from './useDidMountEffect.js';

var useCodeHook = function useCodeHook(_ref) {
  var inputRefs = _ref.inputRefs,
    codeLength = _ref.codeLength,
    disabled = _ref.disabled,
    autoFocus = _ref.autoFocus,
    originalValue = _ref.originalValue,
    getLastActiveIndex = _ref.getLastActiveIndex,
    handleFullCodeEnter = _ref.handleFullCodeEnter;
  useEffect(function () {
    inputRefs.current = inputRefs.current.slice(0, codeLength);
  }, [codeLength]);
  useEffect(function () {
    if (autoFocus && !disabled) {
      var lastActiveIndex = getLastActiveIndex();
      if (inputRefs !== null && inputRefs !== void 0 && inputRefs.current[lastActiveIndex]) {
        var _inputRefs$current$la;
        (_inputRefs$current$la = inputRefs.current[lastActiveIndex]) === null || _inputRefs$current$la === void 0 || _inputRefs$current$la.focus();
      }
    }
  }, [autoFocus]);
  useDidMountEffect(function () {
    if (handleFullCodeEnter && originalValue.length === codeLength) {
      handleFullCodeEnter(originalValue);
    }
  }, [originalValue, handleFullCodeEnter]);
};

export { useCodeHook };
