function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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 ResizeObserverPolyfill from 'resize-observer-polyfill';
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';
if (!(globalThis !== null && globalThis !== void 0 && globalThis.ResizeObserver)) {
  globalThis.ResizeObserver = ResizeObserverPolyfill;
}
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://procore.github.io/core/core-react/latest/?path=/story/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'],
    ariaLabelledby = _ref2['aria-labelledby'],
    ariaLabel = _ref2['aria-label'],
    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,
    _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 (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 wrappedTriggerElement = /*#__PURE__*/React.cloneElement(triggerElement, _objectSpread({
    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: restoreFocusOnHide !== null && restoreFocusOnHide !== void 0 ? restoreFocusOnHide : focusScopeProps.restoreFocus,
    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