"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useTextInputCaretPosition;
var _reactNativeAztec = _interopRequireDefault(require("@wordpress/react-native-aztec"));
var _element = require("@wordpress/element");
/**
 * WordPress dependencies
 */

/**
 * Hook that listens to caret changes from AztecView TextInputs.
 *
 * @param {boolean} scrollEnabled Whether the scroll is enabled or not.
 * @return {[number]} Current caret's data.
 */
function useTextInputCaretPosition(scrollEnabled) {
  const [currentCaretData, setCurrentCaretData] = (0, _element.useState)();
  const onCaretChange = (0, _element.useCallback)(caret => {
    setCurrentCaretData(caret);
  }, []);
  (0, _element.useEffect)(() => {
    if (scrollEnabled) {
      _reactNativeAztec.default.InputState.addCaretChangeListener(onCaretChange);
    } else {
      _reactNativeAztec.default.InputState.removeCaretChangeListener(onCaretChange);
    }
    return () => {
      if (scrollEnabled) {
        _reactNativeAztec.default.InputState.removeCaretChangeListener(onCaretChange);
      }
    };
  }, [scrollEnabled, onCaretChange]);
  return [currentCaretData];
}
//# sourceMappingURL=use-text-input-caret-position.native.js.map