import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";

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; }

import * as React from 'react';
import { useFloating, useInteractions, useDismiss, useClick as useFloatingClick, useHover as useFloatingHover, useRole, useFocus, offset, flip, arrow, autoUpdate, shift, safePolygon, useTransitionStatus, hide } from '@floating-ui/react';
import { generateId } from '../utils';

var usePopover = function usePopover(_ref) {
  var _ref$placement = _ref.placement,
      placement = _ref$placement === void 0 ? 'top' : _ref$placement,
      customId = _ref.customId,
      _ref$defaultOpen = _ref.defaultOpen,
      defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen,
      open = _ref.open,
      _ref$useHover = _ref.useHover,
      useHover = _ref$useHover === void 0 ? true : _ref$useHover,
      _ref$useClick = _ref.useClick,
      useClick = _ref$useClick === void 0 ? true : _ref$useClick,
      _ref$role = _ref.role,
      role = _ref$role === void 0 ? 'dialog' : _ref$role,
      onOpenChange = _ref.onOpenChange;

  var _React$useRef = React.useRef(customId !== null && customId !== void 0 ? customId : "Popover_".concat(generateId())),
      id = _React$useRef.current;

  var arrowRef = React.useRef(null);
  var isControlled = open !== undefined;

  var _React$useState = React.useState(isControlled ? open : defaultOpen),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      isOpen = _React$useState2[0],
      setIsOpen = _React$useState2[1];

  var _React$useState3 = React.useState(true),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      hasArrow = _React$useState4[0],
      setHasArrow = _React$useState4[1];

  React.useEffect(function () {
    if (isControlled && open !== isOpen) {
      setIsOpen(open);
    }
  }, [open, isControlled, isOpen]); // Get position specitic params

  var variantParams = React.useMemo(function () {
    var offsetMainAxis = 8;
    var offsetAlignmentAxis = 0;
    var padding = 5;
    var arrowPadding = 16;

    if (hasArrow) {
      offsetMainAxis = 12;
      offsetAlignmentAxis = -8;
    }

    return {
      offset: {
        mainAxis: offsetMainAxis,
        alignmentAxis: offsetAlignmentAxis
      },
      padding: padding,
      arrowPadding: arrowPadding
    };
  }, [hasArrow]);
  var middleware = React.useMemo(function () {
    return [offset({
      mainAxis: variantParams.offset.mainAxis,
      alignmentAxis: variantParams.offset.alignmentAxis
    }), flip({
      fallbackAxisSideDirection: 'start',
      fallbackPlacements: ['top', 'bottom', 'left', 'right'] // If it doesn't fit anywhere, apply fallback positions

    }), shift({
      padding: variantParams.padding
    }), arrow({
      element: arrowRef,
      padding: variantParams.arrowPadding
    }), hide({
      strategy: 'referenceHidden'
    })];
  }, [variantParams.arrowPadding, variantParams.offset.alignmentAxis, variantParams.offset.mainAxis, variantParams.padding]);

  var handleOpenChange = function handleOpenChange(isOpen) {
    if (isControlled && onOpenChange) onOpenChange(isOpen);else setIsOpen(isOpen);
  };

  var data = useFloating({
    open: isOpen,
    onOpenChange: handleOpenChange,
    placement: placement,
    middleware: middleware,
    whileElementsMounted: autoUpdate
  });
  var hover = useFloatingHover(data.context, {
    enabled: useHover,
    move: false,
    mouseOnly: true,
    handleClose: safePolygon({
      blockPointerEvents: true
    })
  });
  var focus = useFocus(data.context);
  var click = useFloatingClick(data.context, {
    enabled: useClick
  });
  var dismiss = useDismiss(data.context);
  var roleInteractions = useRole(data.context, {
    role: role
  });
  var interactions = useInteractions([hover, focus, click, dismiss, roleInteractions]);

  var _useTransitionStatus = useTransitionStatus(data.context, {
    duration: {
      open: 260,
      close: 180
    }
  }),
      isMounted = _useTransitionStatus.isMounted,
      status = _useTransitionStatus.status;

  return React.useMemo(function () {
    return _objectSpread(_objectSpread({
      id: id,
      isOpen: isOpen,
      setIsOpen: setIsOpen,
      arrowRef: arrowRef,
      arrowPadding: variantParams.arrowPadding,
      hasArrow: hasArrow,
      setHasArrow: setHasArrow,
      placement: placement,
      isMounted: isMounted,
      status: status,
      role: role,
      floatingPlacement: data.placement
    }, interactions), data);
  }, [id, isOpen, variantParams.arrowPadding, hasArrow, placement, isMounted, status, data, interactions, role]);
};

export default usePopover;