text: "use strict";
  
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  
  function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  
  var __importStar = void 0 && (void 0).__importStar || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) {
      if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    }
    result["default"] = mod;
    return result;
  };
  
  var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
    return mod && mod.__esModule ? mod : {
      "default": mod
    };
  };
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var React = __importStar(require("react"));
  
  var KeyCode_1 = __importDefault(require("rc-util/lib/KeyCode"));
  
  var uiUtil_1 = require("../utils/uiUtil");
  
  function usePickerInput(_ref) {
    var open = _ref.open,
        isClickOutside = _ref.isClickOutside,
        triggerOpen = _ref.triggerOpen,
        forwardKeyDown = _ref.forwardKeyDown,
        blurToCancel = _ref.blurToCancel,
        onSubmit = _ref.onSubmit,
        onCancel = _ref.onCancel,
        _onFocus = _ref.onFocus,
        _onBlur = _ref.onBlur;
  
    var _React$useState = React.useState(false),
        _React$useState2 = _slicedToArray(_React$useState, 2),
        typing = _React$useState2[0],
        setTyping = _React$useState2[1];
  
    var _React$useState3 = React.useState(false),
        _React$useState4 = _slicedToArray(_React$useState3, 2),
        focused = _React$useState4[0],
        setFocused = _React$useState4[1];
    /**
     * We will prevent blur to handle open event when user click outside,
     * since this will repeat trigger `onOpenChange` event.
     */
  
  
    var preventBlurRef = React.useRef(false);
    var inputProps = {
      onMouseDown: function onMouseDown() {
        setTyping(true);
        triggerOpen(true);
      },
      onKeyDown: function onKeyDown(e) {
        switch (e.which) {
          case KeyCode_1.default.ENTER:
            {
              if (!open) {
                triggerOpen(true);
              } else {
                onSubmit();
                setTyping(true);
              }
  
              e.preventDefault();
              return;
            }
  
          case KeyCode_1.default.TAB:
            {
              if (typing && open && !e.shiftKey) {
                setTyping(false);
                e.preventDefault();
              } else if (!typing && open) {
                if (!forwardKeyDown(e) && e.shiftKey) {
                  setTyping(true);
                  e.preventDefault();
                }
              }
  
              return;
            }
  
          case KeyCode_1.default.ESC:
            {
              setTyping(true);
              onCancel();
              return;
            }
        }
  
        if (!open && ![KeyCode_1.default.SHIFT].includes(e.which)) {
          triggerOpen(true);
        } else if (!typing) {
          // Let popup panel handle keyboard
          forwardKeyDown(e);
        }
      },
      onFocus: function onFocus(e) {
        setTyping(true);
        setFocused(true);
  
        if (_onFocus) {
          _onFocus(e);
        }
      },
      onBlur: function onBlur(e) {
        if (preventBlurRef.current || !isClickOutside(document.activeElement)) {
          preventBlurRef.current = false;
          return;
        }
  
        if (blurToCancel) {
          setTimeout(function () {
            if (isClickOutside(document.activeElement)) {
              onCancel();
            }
          }, 0);
        } else {
          triggerOpen(false);
        }
  
        setFocused(false);
  
        if (_onBlur) {
          _onBlur(e);
        }
      }
    }; // Global click handler
  
    React.useEffect(function () {
      return uiUtil_1.addGlobalMouseDownEvent(function (_ref2) {
        var target = _ref2.target;
  
        if (open) {
          if (!isClickOutside(target)) {
            preventBlurRef.current = true; // Always set back in case `onBlur` prevented by user
  
            window.setTimeout(function () {
              preventBlurRef.current = false;
            }, 0);
          } else if (!focused) {
            triggerOpen(false);
          }
        }
      });
    });
    return [inputProps, {
      focused: focused,
      typing: typing
    }];
  }
  
  exports.default = usePickerInput;