"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

var _constants = require("../../constants");

var _Popover = _interopRequireDefault(require("../Popover"));

var _Text = _interopRequireDefault(require("../Text"));

var _Translate = _interopRequireDefault(require("../Translate"));

var _Focuser = _interopRequireDefault(require("../Overlay/Focuser"));

var _utils = require("../utils");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
const SpotPopup = ({
  baseClassName = `${_constants.CLS_PREFIX}spot-popup`,
  className,
  pulsar,
  pulsarRef,
  title,
  placement,
  image,
  children,
  buttons,
  meta,
  onClose,
  size,
  locale,
  ...props
}) => {
  const footerRef = (0, _react.useRef)(null); // rerendering is necessary to fix a popover arrow

  const [, setReady] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    setReady(true);

    if (footerRef.current) {
      setTimeout(() => {
        new _Focuser.default().focusLastDescendant(footerRef.current);
      });
    }
  }, []);

  if (typeof image === 'string') {
    image = /*#__PURE__*/_react.default.createElement("img", {
      src: image,
      alt: ""
    });
  }

  return /*#__PURE__*/_react.default.createElement(_Popover.default, (0, _extends2.default)({
    className: (0, _classnames.default)(baseClassName, className, {
      [`${baseClassName}--${size}`]: size
    }),
    title: title,
    target: pulsar,
    targetRef: pulsarRef,
    placement: placement,
    visible: true,
    onClose: () => (0, _utils.safeInvoke)(onClose),
    canCloseOnOutsideClick: false,
    autoFocus: false
  }, props), image && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(`${baseClassName}__media`, {
      [`${baseClassName}__media--compact`]: !title
    })
  }, image), children, buttons && /*#__PURE__*/_react.default.createElement("div", {
    className: `${baseClassName}__footer`,
    ref: footerRef
  }, meta && meta.total > 1 ? /*#__PURE__*/_react.default.createElement(_Text.default, {
    intent: "muted",
    fontSize: "sm"
  }, /*#__PURE__*/_react.default.createElement(_Translate.default, {
    namespace: "SpotPopup",
    content: "total",
    params: {
      current: meta.current + 1,
      total: meta.total
    },
    translators: locale,
    fallback: "%%current%% of %%total%%"
  })) : /*#__PURE__*/_react.default.createElement("span", null), /*#__PURE__*/_react.default.createElement("div", {
    className: `${baseClassName}__actions`
  }, buttons)));
};

var _default = SpotPopup;
exports.default = _default;