"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
const SpotPopup = ({
  baseClassName = "".concat(_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, {
      ["".concat(baseClassName, "--").concat(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)("".concat(baseClassName, "__media"), {
      ["".concat(baseClassName, "__media--compact")]: !title
    })
  }, image), children, buttons && /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(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: "".concat(baseClassName, "__actions")
  }, buttons)));
};

var _default = SpotPopup;
exports.default = _default;