import { useRef, useEffect } from 'react';
var defaultEvent = 'click';
function getTargetElement(target, defaultElement) {
  if (!target) {
    return defaultElement;
  }
  var targetElement;
  if (typeof target === 'function') {
    targetElement = target();
  } else if ('current' in target) {
    targetElement = target.current;
  } else {
    targetElement = target;
  }
  return targetElement;
}
export var useClickAway = function useClickAway(onClickAway, target) {
  var eventName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultEvent;
  var onClickAwayRef = useRef(onClickAway);
  onClickAwayRef.current = onClickAway;
  useEffect(function () {
    var handler = function handler(event) {
      var targets = Array.isArray(target) ? target : [target];
      if (targets.some(function (targetItem) {
        var targetElement = getTargetElement(targetItem);
        return !targetElement || (targetElement === null || targetElement === void 0 ? void 0 : targetElement.contains(event.target));
      })) {
        return;
      }
      onClickAwayRef.current(event);
    };
    document.addEventListener(eventName, handler);
    return function () {
      document.removeEventListener(eventName, handler);
    };
  }, [target, eventName]);
};