"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const DRAWER_BODY_CLASSNAME = "ant-drawer-open";
const DRAWER_MASK_CLASSNAME = "ant-drawer-mask";
const MODAL_WRAP_CLASSNAME = "ant-modal-wrap";
const getOverlayAndMenuBodyElement = () => {
  var _a;
  const overlays = [
    ...document.getElementsByClassName(MODAL_WRAP_CLASSNAME),
    ...document.getElementsByClassName(DRAWER_MASK_CLASSNAME)
  ];
  const overlay = overlays == null ? void 0 : overlays[0];
  const menuBody = (overlay == null ? void 0 : overlay.className) === MODAL_WRAP_CLASSNAME ? overlay : (_a = document.getElementsByClassName(DRAWER_BODY_CLASSNAME)) == null ? void 0 : _a[0];
  return {
    focusMenu: () => menuBody == null ? void 0 : menuBody.focus(),
    overlayClassname: overlay == null ? void 0 : overlay.className,
    // If an overlay does not exist then the input is not
    // inside of a menu or modal and we do not want to
    // alter any event behavior
    overlayExists: !!overlay
  };
};
const onEsc = (focused, inputRef, event, handleFocused) => {
  var _a, _b;
  const { overlayExists } = getOverlayAndMenuBodyElement();
  if (!overlayExists)
    return;
  if (focused && event.key === "Escape") {
    event.stopPropagation();
    (_a = inputRef.current) == null ? void 0 : _a.blur();
    (_b = getOverlayAndMenuBodyElement()) == null ? void 0 : _b.focusMenu();
    handleFocused(false);
  }
};
const onInputNumberClick = (blurred, focused, inputRef, event, handleFocused) => {
  var _a;
  const { overlayClassname, overlayExists } = getOverlayAndMenuBodyElement();
  if (!overlayExists)
    return;
  const overlayClicked = event.target.className === overlayClassname;
  if (focused && (overlayClicked || event.target !== inputRef.current)) {
    if (overlayClicked) {
      event.stopPropagation();
    }
    handleFocused(false);
    if (!blurred)
      (_a = inputRef.current) == null ? void 0 : _a.blur();
  }
};
const onInputClick = (blurred, focused, inputRef, event, handleFocused) => {
  var _a, _b, _c;
  const { overlayClassname, overlayExists } = getOverlayAndMenuBodyElement();
  if (!overlayExists)
    return;
  const overlayClicked = event.target.className === overlayClassname;
  if (focused && (overlayClicked || !!((_a = inputRef.current) == null ? void 0 : _a.input) && event.target !== ((_b = inputRef.current) == null ? void 0 : _b.input))) {
    if (overlayClicked) {
      event.stopPropagation();
    }
    handleFocused(false);
    if (!blurred)
      (_c = inputRef.current) == null ? void 0 : _c.blur();
  }
};
const onClickSelect = (blurred, event, isOpen, hasOpened, setHasOpened) => {
  const targetClassname = event.target.className;
  const { overlayClassname, overlayExists } = getOverlayAndMenuBodyElement();
  if (!overlayExists)
    return;
  if (isOpen && targetClassname === overlayClassname) {
    event.stopPropagation();
  }
  if (hasOpened && !isOpen && targetClassname === overlayClassname) {
    event.stopPropagation();
    setHasOpened(false);
  }
  if (blurred && hasOpened && targetClassname !== overlayClassname)
    setHasOpened(false);
};
const onEscSelect = (focused, inputRef, event, handleFocused, setHasOpened) => {
  var _a;
  const { overlayExists, focusMenu } = getOverlayAndMenuBodyElement();
  if (!overlayExists)
    return;
  if (focused && event.key === "Escape") {
    event.stopPropagation();
    (_a = inputRef.current) == null ? void 0 : _a.blur();
    focusMenu();
    handleFocused(false);
    setHasOpened(false);
  }
};
const useInputNumberEscape = (ref, onBlur) => {
  const inputRef = React.useRef(null);
  React.useImperativeHandle(ref, () => inputRef == null ? void 0 : inputRef.current);
  const [focused, setFocused] = React.useState(false);
  const [blurred, setBlurred] = React.useState(false);
  React.useEffect(() => {
    const handleEsc = (event) => {
      onEsc(focused, inputRef, event, setFocused);
    };
    const handleClick = (event) => {
      onInputNumberClick(blurred, focused, inputRef, event, setFocused);
    };
    window.addEventListener("keydown", handleEsc, true);
    window.addEventListener("click", handleClick, true);
    return () => {
      window.removeEventListener("keydown", handleEsc, true);
      window.removeEventListener("click", handleClick, true);
    };
  }, [blurred, focused]);
  const handleBlur = (e, previousValue) => {
    setBlurred(true);
    onBlur == null ? void 0 : onBlur(e, previousValue);
  };
  const onFocus = () => {
    setFocused(true);
    setBlurred(false);
  };
  return { inputRef, onBlur: handleBlur, onFocus };
};
const useInputEscape = (ref, onBlur) => {
  const inputRef = React.useRef(null);
  const [focused, setFocused] = React.useState(false);
  const [blurred, setBlurred] = React.useState(false);
  React.useImperativeHandle(ref, () => inputRef == null ? void 0 : inputRef.current);
  React.useEffect(() => {
    const handleEsc = (event) => {
      onEsc(focused, inputRef, event, setFocused);
    };
    const handleClick = (event) => {
      onInputClick(blurred, focused, inputRef, event, setFocused);
    };
    document.addEventListener("keydown", handleEsc, true);
    document.addEventListener("click", handleClick, true);
    return () => {
      document.removeEventListener("keydown", handleEsc, true);
      document.removeEventListener("click", handleClick, true);
    };
  }, [focused, blurred]);
  const handleBlur = (e, previousValue) => {
    setBlurred(true);
    onBlur == null ? void 0 : onBlur(e, previousValue);
  };
  const onFocus = () => {
    setFocused(true);
    setBlurred(false);
  };
  return { inputRef, onBlur: handleBlur, onFocus };
};
const useSelectEscape = (containerRef, isOpen, ref, onBlur) => {
  const inputRef = React.useRef(null);
  const [focused, setFocused] = React.useState(false);
  const [blurred, setBlurred] = React.useState(false);
  const [hasOpened, setHasOpened] = React.useState(false);
  React.useImperativeHandle(ref, () => inputRef.current);
  React.useEffect(() => {
    if (isOpen)
      setHasOpened(true);
  }, [isOpen]);
  React.useEffect(() => {
    var _a;
    const handleClick = (event) => {
      onClickSelect(blurred, event, isOpen, hasOpened, setHasOpened);
    };
    const handleEsc = (event) => {
      onEscSelect(focused, inputRef, event, setFocused, setHasOpened);
    };
    (_a = containerRef.current) == null ? void 0 : _a.addEventListener("keydown", handleEsc);
    window.addEventListener("click", handleClick, true);
    return () => {
      var _a2;
      (_a2 = containerRef.current) == null ? void 0 : _a2.removeEventListener("keydown", handleEsc);
      window.removeEventListener("click", handleClick, true);
    };
  }, [blurred, containerRef, focused, setFocused, isOpen, hasOpened]);
  const handleBlur = (e, previousValue) => {
    setBlurred(true);
    setFocused(false);
    onBlur == null ? void 0 : onBlur(e, previousValue);
  };
  const onFocus = () => {
    setFocused(true);
  };
  return { inputRef, onBlur: handleBlur, onFocus };
};
exports.useInputEscape = useInputEscape;
exports.useInputNumberEscape = useInputNumberEscape;
exports.useSelectEscape = useSelectEscape;
