'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('reakit-utils/warning');
var React = require('react');
require('reakit-utils/useId');
var __chunk_1 = require('../chunk-002fc018.js');
var useSealedState = require('reakit-utils/useSealedState');
require('../Hidden/HiddenState.js');
var DialogState = require('../Dialog/DialogState.js');
var Popper = _interopDefault(require('popper.js'));

function usePopoverState(initialState) {
  if (initialState === void 0) {
    initialState = {};
  }

  var _useSealedState = useSealedState.useSealedState(initialState),
      _useSealedState$place = _useSealedState.placement,
      sealedPlacement = _useSealedState$place === void 0 ? "bottom" : _useSealedState$place,
      _useSealedState$unsta = _useSealedState.unstable_flip,
      flip = _useSealedState$unsta === void 0 ? true : _useSealedState$unsta,
      _useSealedState$unsta2 = _useSealedState.unstable_shift,
      shift = _useSealedState$unsta2 === void 0 ? true : _useSealedState$unsta2,
      _useSealedState$unsta3 = _useSealedState.unstable_gutter,
      gutter = _useSealedState$unsta3 === void 0 ? 12 : _useSealedState$unsta3,
      _useSealedState$unsta4 = _useSealedState.unstable_preventOverflow,
      preventOverflow = _useSealedState$unsta4 === void 0 ? true : _useSealedState$unsta4,
      _useSealedState$unsta5 = _useSealedState.unstable_boundariesElement,
      boundariesElement = _useSealedState$unsta5 === void 0 ? "scrollParent" : _useSealedState$unsta5,
      _useSealedState$unsta6 = _useSealedState.unstable_fixed,
      fixed = _useSealedState$unsta6 === void 0 ? false : _useSealedState$unsta6,
      sealed = __chunk_1._objectWithoutPropertiesLoose(_useSealedState, ["placement", "unstable_flip", "unstable_shift", "unstable_gutter", "unstable_preventOverflow", "unstable_boundariesElement", "unstable_fixed"]);

  var popper = React.useRef(null);
  var referenceRef = React.useRef(null);
  var popoverRef = React.useRef(null);
  var arrowRef = React.useRef(null);

  var _React$useState = React.useState(sealedPlacement),
      originalPlacement = _React$useState[0],
      place = _React$useState[1];

  var _React$useState2 = React.useState(sealedPlacement),
      placement = _React$useState2[0],
      setPlacement = _React$useState2[1];

  var _React$useState3 = React.useState({}),
      popoverStyles = _React$useState3[0],
      setPopoverStyles = _React$useState3[1];

  var _React$useState4 = React.useState({}),
      arrowStyles = _React$useState4[0],
      setArrowStyles = _React$useState4[1];

  var dialog = DialogState.useDialogState(sealed);
  React.useLayoutEffect(function () {
    if (referenceRef.current && popoverRef.current) {
      popper.current = new Popper(referenceRef.current, popoverRef.current, {
        placement: originalPlacement,
        eventsEnabled: dialog.visible,
        positionFixed: fixed,
        modifiers: {
          applyStyle: {
            enabled: false
          },
          flip: {
            enabled: flip,
            padding: 16
          },
          shift: {
            enabled: shift
          },
          offset: {
            enabled: shift,
            offset: "0, " + gutter
          },
          preventOverflow: {
            enabled: preventOverflow,
            boundariesElement: boundariesElement
          },
          arrow: arrowRef.current ? {
            enabled: true,
            element: arrowRef.current
          } : undefined,
          updateStateModifier: {
            order: 900,
            enabled: true,
            fn: function fn(data) {
              setPlacement(data.placement);
              setPopoverStyles(data.styles);
              setArrowStyles(data.arrowStyles);
              return data;
            }
          }
        }
      });
    }

    return function () {
      if (popper.current) {
        popper.current.destroy();
      }
    };
  }, [originalPlacement, flip, shift, gutter, preventOverflow, boundariesElement, dialog.visible]);
  return __chunk_1._objectSpread({}, dialog, {
    unstable_referenceRef: referenceRef,
    unstable_popoverRef: popoverRef,
    unstable_arrowRef: arrowRef,
    unstable_popoverStyles: popoverStyles,
    unstable_arrowStyles: arrowStyles,
    unstable_originalPlacement: originalPlacement,
    placement: placement,
    place: React.useCallback(place, [])
  });
}
var keys = [].concat(DialogState.useDialogState.__keys, ["unstable_referenceRef", "unstable_popoverRef", "unstable_arrowRef", "unstable_popoverStyles", "unstable_arrowStyles", "unstable_originalPlacement", "placement", "place"]);
usePopoverState.__keys = keys;

exports.usePopoverState = usePopoverState;
