function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import { arrow as floatingArrow, flip as floatingFlip, limitShift, offset as floatingOffset, shift as floatingShift, size as floatingSize } from '@floating-ui/react-dom';
import { FocusScope } from '@react-aria/focus';
import { useId } from '@react-aria/utils';
import React from 'react';
import { Arrow } from '../Overlay/OverlayArrow';
import { arrowSize } from '../Overlay/OverlayArrow.styles';
import { useOverlay } from '../Overlay/useOverlay';
import { Portal } from '../Portal';
import { useClickOutside } from '../_hooks/ClickOutside';
import { DelayedToggleContext, useDelayedToggle, useDelayedToggleContext } from '../_hooks/DelayedToggle';
import { Trigger, useTrigger } from '../_hooks/Trigger';
import { useVisibility } from '../_hooks/Visibility';
import { mergeRefs } from '../_utils/mergeRefs';
import { useOverlayTriggerA11y } from './a11yPresets';
var ClickOutside = function ClickOutside(_ref) {
  var container = _ref.container,
    onClickOutside = _ref.onClickOutside,
    refs = _ref.refs;
  useClickOutside({
    onClickOutside: onClickOutside,
    refs: refs,
    container: container
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null);
};

/**

 @since 10.19.0

 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-overlaytrigger--demo)

 */
export var OverlayTrigger = /*#__PURE__*/React.forwardRef(function OverlayTrigger(_ref2, ref) {
  var _ref2$afterHide = _ref2.afterHide,
    afterHide = _ref2$afterHide === void 0 ? function () {} : _ref2$afterHide,
    _ref2$afterShow = _ref2.afterShow,
    afterShow = _ref2$afterShow === void 0 ? function () {} : _ref2$afterShow,
    ariaDescribedby = _ref2['aria-describedby'],
    ariaDetails = _ref2['aria-details'],
    ariaLabel = _ref2['aria-label'],
    ariaLabelledby = _ref2['aria-labelledby'],
    autoFocus = _ref2.autoFocus,
    _ref2$beforeHide = _ref2.beforeHide,
    beforeHide = _ref2$beforeHide === void 0 ? function () {
      return true;
    } : _ref2$beforeHide,
    _ref2$beforeShow = _ref2.beforeShow,
    beforeShow = _ref2$beforeShow === void 0 ? function () {
      return true;
    } : _ref2$beforeShow,
    _ref2$canFlip = _ref2.canFlip,
    canFlip = _ref2$canFlip === void 0 ? true : _ref2$canFlip,
    children = _ref2.children,
    _ref2$clickOutsideIgn = _ref2.clickOutsideIgnoreRefs,
    clickOutsideIgnoreRefs = _ref2$clickOutsideIgn === void 0 ? [] : _ref2$clickOutsideIgn,
    containFocus = _ref2.containFocus,
    container = _ref2.container,
    _ref2$hideDelay = _ref2.hideDelay,
    hideDelay = _ref2$hideDelay === void 0 ? 100 : _ref2$hideDelay,
    hideKeys_ = _ref2.hideKeys,
    _ref2$initialIsVisibl = _ref2.initialIsVisible,
    initialIsVisible = _ref2$initialIsVisibl === void 0 ? false : _ref2$initialIsVisibl,
    overlay = _ref2.overlay,
    _ref2$padding = _ref2.padding,
    padding = _ref2$padding === void 0 ? 2 : _ref2$padding,
    _ref2$placement = _ref2.placement,
    placement = _ref2$placement === void 0 ? 'top' : _ref2$placement,
    _ref2$restoreFocusOnH = _ref2.restoreFocusOnHide,
    restoreFocusOnHide = _ref2$restoreFocusOnH === void 0 ? true : _ref2$restoreFocusOnH,
    _ref2$showDelay = _ref2.showDelay,
    showDelay = _ref2$showDelay === void 0 ? 0 : _ref2$showDelay,
    _ref2$showKeys = _ref2.showKeys,
    showKeys = _ref2$showKeys === void 0 ? ['ArrowDown', 'Down'] : _ref2$showKeys,
    _ref2$shrinkOverlay = _ref2.shrinkOverlay,
    shrinkOverlay = _ref2$shrinkOverlay === void 0 ? false : _ref2$shrinkOverlay,
    trackAriaExpanded = _ref2.trackAriaExpanded,
    _ref2$trigger = _ref2.trigger,
    trigger = _ref2$trigger === void 0 ? 'click' : _ref2$trigger,
    _ref2$arrow = _ref2.arrow,
    arrow = _ref2$arrow === void 0 ? false : _ref2$arrow,
    overlayApiRef = _ref2.overlayRef,
    role = _ref2.role,
    _ref2$passA11yPropsTo = _ref2.passA11yPropsToOverlay,
    passA11yPropsToOverlay = _ref2$passA11yPropsTo === void 0 ? false : _ref2$passA11yPropsTo;
  var isDialogRole = role === 'dialog';
  if (isDialogRole && (trigger === 'hover' || trigger !== null && trigger !== void 0 && trigger.includes('hover'))) {
    console.warn("@procore/core-react: OverlayTigger has role ".concat(role, " and a hover trigger, this may cause weird focus management and is unrecommended. Review autoFocus prop."));
  }
  var hideKeys = hideKeys_ !== null && hideKeys_ !== void 0 ? hideKeys_ : {
    // Note by default OverlayTrigger closes on tab. Dialog role changes the default.
    overlay: (containFocus !== null && containFocus !== void 0 ? containFocus : isDialogRole) ? ['Escape', 'Esc'] : ['Escape', 'Esc', 'Tab'],
    target: ['Escape', 'Esc']
  };
  var triggerElRef = React.useRef(null);
  var overlayElRef = React.useRef(null);
  var _React$useState = React.useState(null),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    referenceElement = _React$useState2[0],
    setReferenceElement = _React$useState2[1];
  var clickedOutsideRef = React.useRef(true);
  var visibility = useVisibility({
    afterHide: afterHide,
    afterShow: afterShow,
    initialIsVisible: initialIsVisible
  });
  React.useImperativeHandle(overlayApiRef, function () {
    return {
      show: visibility.show,
      hide: visibility.hide
    };
  });
  var triggers = React.useMemo(function () {
    return Array.isArray(trigger) ? trigger : [trigger];
  }, [trigger]);
  React.useEffect(function () {
    if (triggers.indexOf('none') >= 0 && visibility.isVisible) {
      visibility.hide();
    }
    if (triggers.indexOf('always') >= 0 && !visibility.isVisible) {
      visibility.show();
    }
  }, [triggers, visibility.isVisible]);

  // TODO delete in a separate branch for testing and rely only on FocusScope
  React.useEffect(function () {
    if ([true, 'core-react'].includes(restoreFocusOnHide) && !visibility.isVisible && !clickedOutsideRef.current) {
      triggerElRef.current && triggerElRef.current.focus();
    }
    clickedOutsideRef.current = false;
  }, [restoreFocusOnHide, triggerElRef, visibility.isVisible]);
  var delayedToggle = useDelayedToggle({
    beforeDisable: beforeHide,
    beforeEnable: beforeShow,
    disableDelay: hideDelay,
    enableDelay: showDelay,
    isEnabled: visibility.isVisible,
    onDisable: visibility.hide,
    onEnable: visibility.show
  });
  var triggerApi = useTrigger({
    isEnabled: visibility.isVisible,
    enable: delayedToggle.enable,
    disable: delayedToggle.disable,
    enableKeys: showKeys,
    disableKeys: hideKeys.target,
    trigger: trigger,
    triggerRef: triggerElRef
  });
  var triggerElement = typeof children === 'function' ? children(triggerApi) : children;
  var overlayId = useId();
  var _useOverlayTriggerA = useOverlayTriggerA11y({
      canPropOverlayUp: /*#__PURE__*/React.isValidElement(overlay) && passA11yPropsToOverlay,
      'aria-describedby': ariaDescribedby,
      'aria-details': ariaDetails,
      'aria-labelledby': ariaLabelledby,
      'aria-label': ariaLabel,
      id: overlayId,
      isOpen: visibility.isVisible,
      role: role
    }),
    wrapperProps = _useOverlayTriggerA.wrapperProps,
    overlayProps = _useOverlayTriggerA.overlayProps,
    portalProps = _useOverlayTriggerA.portalProps,
    triggerProps = _useOverlayTriggerA.triggerProps,
    focusScopeProps = _useOverlayTriggerA.focusScopeProps,
    labelProps = _useOverlayTriggerA.labelProps;
  var hasAriaExpanded = trackAriaExpanded ? {
    'aria-expanded': triggerApi.isVisible
  } : {};
  var wrappedTriggerElement = /*#__PURE__*/React.cloneElement(triggerElement, _objectSpread(_objectSpread({}, hasAriaExpanded), {}, {
    open: triggerApi.isVisible,
    ref: triggerElement.ref ? mergeRefs(triggerElement.ref, ref, triggerElRef, setReferenceElement) : mergeRefs(ref, triggerElRef, setReferenceElement)
  }, triggerProps));
  return /*#__PURE__*/React.createElement(DelayedToggleContext.Provider, {
    value: delayedToggle
  }, wrappedTriggerElement, visibility.isVisible && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ClickOutside, {
    container: container,
    onClickOutside: function onClickOutside(event) {
      clickedOutsideRef.current = true;
      delayedToggle.disable(event);
    },
    refs: [overlayElRef, triggerElRef].concat(_toConsumableArray(clickOutsideIgnoreRefs))
  }), /*#__PURE__*/React.createElement(Portal, _extends({
    container: container
  }, portalProps), /*#__PURE__*/React.createElement(Trigger, {
    isEnabled: visibility.isVisible,
    enable: delayedToggle.enable,
    disable: delayedToggle.disable,
    disableKeys: hideKeys.overlay,
    trigger: trigger === 'click' ? 'none' : trigger,
    triggerRef: overlayElRef
  }, /*#__PURE__*/React.createElement(TriggerOverlay, {
    autoFocus: autoFocus !== null && autoFocus !== void 0 ? autoFocus : focusScopeProps.autoFocus,
    containFocus: containFocus !== null && containFocus !== void 0 ? containFocus : focusScopeProps.contain,
    restoreFocus: [true, 'react-aria-focus-scope'].includes(restoreFocusOnHide),
    ref: overlayElRef,
    referenceElement: referenceElement,
    overlay: overlay,
    shrinkOverlay: shrinkOverlay,
    placement: placement,
    padding: padding,
    canFlip: canFlip,
    arrow: arrow,
    wrapperProps: wrapperProps,
    overlayCloneProps: overlayProps
  })))));
});
export function CloseOnFocus(_ref3) {
  var hide = _ref3.hide;
  return (
    /*#__PURE__*/
    // in IE, the hidden input's cursor appears right under the menu
    // this positions it arbitrarily away where it won't be noticed
    React.createElement("div", {
      style: {
        height: 0,
        position: 'absolute',
        top: 1000,
        width: 0
      }
    }, /*#__PURE__*/React.createElement("input", {
      onFocus: hide,
      style: {
        opacity: 0
      }
    }))
  );
}
export function useOverlayTriggerContext() {
  var _useDelayedToggleCont = useDelayedToggleContext(),
    show = _useDelayedToggleCont.enable,
    hide = _useDelayedToggleCont.disable,
    toggle = _useDelayedToggleCont.toggle;
  return {
    hide: hide,
    show: show,
    toggle: toggle
  };
}
export var TriggerOverlay = /*#__PURE__*/React.forwardRef(function (_ref4, ref) {
  var autoFocus = _ref4.autoFocus,
    containFocus = _ref4.containFocus,
    restoreFocus = _ref4.restoreFocus,
    placement = _ref4.placement,
    canFlip = _ref4.canFlip,
    arrow = _ref4.arrow,
    padding = _ref4.padding,
    shrinkOverlay = _ref4.shrinkOverlay,
    overlay = _ref4.overlay,
    referenceElement = _ref4.referenceElement,
    wrapperProps = _ref4.wrapperProps,
    overlayCloneProps = _ref4.overlayCloneProps;
  var arrowRef = React.useRef(null);
  var arrowPadding = arrowSize / 2 + 1;
  var middleware = [floatingOffset({
    mainAxis: arrow ? padding + arrowPadding : padding
  }), canFlip ? floatingFlip() : null, floatingShift({
    limiter: limitShift()
  }), !shrinkOverlay ? floatingSize({
    apply: function apply(_ref5) {
      var elements = _ref5.elements;
      Object.assign(elements.floating.style, {
        minWidth: "".concat(elements.reference.getBoundingClientRect().width, "px")
      });
    }
  }) : null, floatingArrow({
    element: arrowRef,
    padding: 6
  })].filter(function (middleware) {
    return middleware !== null;
  });
  var _useOverlay = useOverlay({
      middleware: middleware,
      placement: placement
    }),
    isPositioned = _useOverlay.isPositioned,
    overlayStyle = _useOverlay.overlayStyle,
    referenceRefCallback = _useOverlay.referenceRef,
    overlayRef = _useOverlay.overlayRef,
    middlewareData = _useOverlay.middlewareData,
    currentPlacement = _useOverlay.placement;
  React.useEffect(function () {
    referenceRefCallback(referenceElement);
  }, [referenceElement, referenceRefCallback]);
  var mergedRefs = mergeRefs(ref, overlayRef);

  // TODO could just move clone and clone props to parent
  if (! /*#__PURE__*/React.isValidElement(overlay) && overlayCloneProps) {
    console.warn('PLEASE REPORT ISSUE @procore/core-react: OverlayTrigger TriggerOverlay is not a valid React element but wants to clone "overlay" with props. Cannot pass props to the element.');
  }
  var shownStyles = _objectSpread(_objectSpread({}, overlayStyle), {}, {
    left: isPositioned ? overlayStyle.left : '-99999px',
    top: isPositioned ? overlayStyle.top : '-99999px'
  });
  return /*#__PURE__*/React.createElement(FocusScope, {
    autoFocus: autoFocus,
    contain: containFocus,
    restoreFocus: restoreFocus
  }, /*#__PURE__*/React.createElement("div", _extends({
    "data-qa": "core-overlay-trigger-overlay-wrapper",
    ref: mergedRefs,
    style: shownStyles
  }, wrapperProps), /*#__PURE__*/React.isValidElement(overlay) ? /*#__PURE__*/React.cloneElement(overlay, overlayCloneProps) : overlay, arrow && /*#__PURE__*/React.createElement(Arrow, {
    ref: arrowRef,
    arrow: middlewareData.arrow,
    placement: currentPlacement
  })));
});
//# sourceMappingURL=OverlayTrigger.js.map