"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _react2 = require("@floating-ui/react");

var _usePopoverContext = _interopRequireDefault(require("./usePopoverContext"));

var _excluded = ["className", "children", "padding", "maxWidth", "withArrow"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || 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 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) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var ARROW_SIZE = 24;
var ARROW_SVG_PATH = '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 PopoverElement = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _context$refs$floatin, _context$y, _context$x, _context$middlewareDa;

  var className = props.className,
      children = props.children,
      padding = props.padding,
      maxWidth = props.maxWidth,
      _props$withArrow = props.withArrow,
      withArrow = _props$withArrow === void 0 ? true : _props$withArrow,
      rest = _objectWithoutProperties(props, _excluded);

  var context = (0, _usePopoverContext.default)();
  var elementRef = (0, _react2.useMergeRefs)([context.refs.setFloating, ref]);
  if (context.hasArrow !== withArrow) context.setHasArrow(withArrow);
  var isShorterThanArrowWithPadding = ARROW_SIZE + 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 popover 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(ARROW_SIZE / 2, "px)") : null;
  var popoverClass = (0, _classnames.default)('sg-popover', className);
  return context.isMounted && /*#__PURE__*/React.createElement(_react2.FloatingPortal, null, /*#__PURE__*/React.createElement(_react2.FloatingFocusManager, {
    context: context.context,
    order: ['reference', 'content'],
    visuallyHiddenDismiss: true,
    initialFocus: -1
  }, /*#__PURE__*/React.createElement("div", _extends({
    ref: elementRef,
    className: popoverClass,
    "data-popover-id": context.id,
    "data-placement": context.floatingPlacement,
    "data-status": context.status,
    style: _objectSpread(_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), {}, {
      padding: padding,
      maxWidth: maxWidth
    })
  }, context.getFloatingProps(), rest), children, withArrow && /*#__PURE__*/React.createElement(_react2.FloatingArrow, {
    ref: context.arrowRef,
    className: "sg-popover__arrow",
    context: context.context,
    width: ARROW_SIZE,
    height: ARROW_SIZE,
    staticOffset: arrowOffset,
    d: ARROW_SVG_PATH
  }))));
});
var _default = PopoverElement;
exports.default = _default;