"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = 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");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

// TODO: Use SpotSpotProps['popup'] instead of copypaste

const SpotPopup = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}spot-popup`,
    className,
    pulsar,
    pulsarRef,
    title,
    placement,
    image,
    children,
    buttons,
    meta,
    onClose,
    size,
    locale,
    ...props
  } = _ref;
  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__*/(0, _jsxRuntime.jsx)("img", {
      src: image,
      alt: ""
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Popover.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,
    children: [image && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(`${baseClassName}__media`, {
        [`${baseClassName}__media--compact`]: !title
      }),
      children: image
    }), children, buttons && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: `${baseClassName}__footer`,
      ref: footerRef,
      children: [meta && meta.total > 1 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.default, {
        intent: "muted",
        fontSize: "sm",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
          namespace: "SpotPopup",
          content: "total",
          params: {
            current: meta.current + 1,
            total: meta.total
          },
          translators: locale,
          fallback: "%%current%% of %%total%%"
        })
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__actions`,
        children: buttons
      })]
    })]
  });
};
var _default = exports.default = SpotPopup;