import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

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 classNames from 'classnames';
import { FloatingPortal, useMergeRefs, FloatingArrow } from '@floating-ui/react';
import Text from '../text/Text';
import useTooltipContext from './useTooltipContext';
var ARROW_SIZE_DEFAULT = 24;
var ARROW_SIZE_SMALL = 12;
var ARROW_SVG_PATH_DEFAULT = 'M0 24C1.72106 24 3.38535 23.3843 4.69205 22.2642L11.026 16.8349C11.5868 16.3542 12.414 16.3533 12.9759 16.8327L19.3782 22.2961C20.667 23.3958 22.3057 24 24 24V24L0 24V24Z';
var ARROW_SVG_PATH_SMALL = 'M0 12C0.86053 12 1.69267 11.6922 2.34602 11.1321L5.51299 8.41745C5.79338 8.17711 6.20701 8.17665 6.48794 8.41637L9.6891 11.148C10.3335 11.6979 11.1529 12 12 12L0 12Z';
var TooltipElement = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _context$refs$floatin, _classNames, _context$y, _context$x, _context$middlewareDa;

  var className = props.className,
      label = props.label;
  var context = useTooltipContext();
  var elementRef = useMergeRefs([context.refs.setFloating, ref]);
  var arrowSize = context.size === 'small' ? ARROW_SIZE_SMALL : ARROW_SIZE_DEFAULT;
  var arrowSVGPath = context.size === 'small' ? ARROW_SVG_PATH_SMALL : ARROW_SVG_PATH_DEFAULT;
  var isShorterThanArrowWithPadding = arrowSize + 2 * context.arrowPadding > ((_context$refs$floatin = context.refs.floating.current) === null || _context$refs$floatin === void 0 ? void 0 : _context$refs$floatin.offsetHeight); // If the placement is right or left
  // and the tooltip isn't high enough to fit an arrow with the padding
  // position the arrow individually.
  // We don't care for top and bottom, because the component min width is enough to fit both arrow and it's padding.

  var arrowOffset = isShorterThanArrowWithPadding && (context.placement.includes('right') || context.placement.includes('left')) ? "calc(50% - ".concat(arrowSize / 2, "px)") : null;
  var tooltipClass = classNames('sg-tooltip', (_classNames = {}, _defineProperty(_classNames, "sg-tooltip--".concat(String(context.size)), context.size), _defineProperty(_classNames, "sg-tooltip--".concat(String(context.color)), context.color), _classNames), className);
  return context.isMounted && /*#__PURE__*/React.createElement(FloatingPortal, null, /*#__PURE__*/React.createElement("div", _extends({
    ref: elementRef,
    className: tooltipClass,
    "data-tooltip-id": context.id,
    "data-placement": context.floatingPlacement,
    "data-status": context.status,
    style: _objectSpread({
      position: context.strategy,
      top: (_context$y = context.y) !== null && _context$y !== void 0 ? _context$y : 0,
      left: (_context$x = context.x) !== null && _context$x !== void 0 ? _context$x : 0,
      visibility: (_context$middlewareDa = context.middlewareData.hide) !== null && _context$middlewareDa !== void 0 && _context$middlewareDa.referenceHidden ? 'hidden' : 'visible'
    }, props.style),
    "aria-hidden": "true"
  }, context.getFloatingProps(), {
    role: "none"
  }), /*#__PURE__*/React.createElement(Text, {
    className: "sg-tooltip__label",
    align: "to-center",
    color: context.color === 'dark' ? 'text-white' : 'text-black',
    size: "small"
  }, label), /*#__PURE__*/React.createElement(FloatingArrow, {
    ref: context.arrowRef,
    className: "sg-tooltip__arrow",
    context: context.context,
    width: arrowSize,
    height: arrowSize,
    staticOffset: arrowOffset,
    d: arrowSVGPath
  })));
});
export default TooltipElement;