import { useEventListener } from './EventListener';
/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */ // To support MenuRef had to change to any. Could manually do <HTMLElement & MenuRef> but seems brittle
export function isEventSource(ref, event) {
  if (ref !== null && ref !== void 0 && ref.current && event && (event.target instanceof HTMLElement || event.target instanceof SVGElement)) {
    return ref.current.contains(event.target);
  } else {
    return false;
  }
}

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function useClickOutside(_ref) {
  var _ref$refs = _ref.refs,
    refs = _ref$refs === void 0 ? [] : _ref$refs,
    onClickOutside = _ref.onClickOutside,
    container = _ref.container;
  var handler = function handler(event) {
    if (onClickOutside && !refs.find(function (ref) {
      return isEventSource(ref, event);
    })) {
      onClickOutside(event);
    }
  };
  var options = {
    capture: true
  };
  var containerDocument = container === null || container === void 0 ? void 0 : container.ownerDocument;
  var scope = (containerDocument === null || containerDocument === void 0 ? void 0 : containerDocument.defaultView) || undefined;
  useEventListener({
    event: 'click',
    handler: handler,
    options: options,
    scope: scope
  });
  useEventListener({
    event: 'touchend',
    handler: handler,
    options: options,
    scope: scope
  });
}
//# sourceMappingURL=ClickOutside.js.map