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, useRole, useHover, useFocus, offset, flip, arrow, autoUpdate, shift, safePolygon, useTransitionStatus, hide } from '@floating-ui/react';
import { generateId, isTouchScreen } from '../utils';

var useTooltip = function useTooltip(_ref) {
  var _ref$placement = _ref.placement,
      placement = _ref$placement === void 0 ? 'top' : _ref$placement,
      customId = _ref.customId,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'default' : _ref$size,
      _ref$color = _ref.color,
      color = _ref$color === void 0 ? 'dark' : _ref$color,
      _ref$defaultOpen = _ref.defaultOpen,
      defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen,
      open = _ref.open,
      asLabel = _ref.asLabel,
      onOpenChange = _ref.onOpenChange;

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

  var _React$useRef2 = React.useRef(!isTouchScreen()),
      enableTooltip = _React$useRef2.current;

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

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

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

  var variantParams = React.useMemo(function () {
    var offsetMainAxis = size === 'small' ? 6 : 10;
    var offsetAlignmentAxis = size === 'small' ? -6 : -8;
    var padding = 5;
    var arrowPadding = size === 'small' ? 6 : 16;
    return {
      offset: {
        mainAxis: offsetMainAxis,
        alignmentAxis: offsetAlignmentAxis
      },
      padding: padding,
      arrowPadding: arrowPadding
    };
  }, [size]);
  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: enableTooltip ? isOpen : false,
    onOpenChange: handleOpenChange,
    placement: placement,
    middleware: middleware,
    whileElementsMounted: autoUpdate
  });
  var hover = useHover(data.context, {
    move: false,
    mouseOnly: true,
    handleClose: safePolygon({
      blockPointerEvents: true
    })
  });
  var focus = useFocus(data.context, {
    enabled: enableTooltip
  });
  var dismiss = useDismiss(data.context);
  var role = useRole(data.context, {
    role: 'tooltip'
  });
  var interactions = useInteractions([hover, focus, role, dismiss]);

  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,
      asLabel: asLabel,
      isOpen: isOpen,
      setIsOpen: setIsOpen,
      arrowRef: arrowRef,
      arrowPadding: variantParams.arrowPadding,
      placement: placement,
      size: size,
      color: color,
      isMounted: isMounted,
      status: status,
      floatingPlacement: data.placement
    }, interactions), data);
  }, [id, asLabel, isOpen, setIsOpen, variantParams.arrowPadding, placement, size, color, isMounted, status, data, interactions]);
};

export default useTooltip;