"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _utils = require("../utils");
var _ListOperationContext = _interopRequireDefault(require("./ListOperationContext"));
var _Popover = _interopRequireDefault(require("../Popover"));
var _Translate = _interopRequireDefault(require("../Translate"));
var _Button = _interopRequireDefault(require("../Button"));
var _Media = _interopRequireDefault(require("../Media"));
var _Icon = _interopRequireDefault(require("../Icon"));
var _Text = _interopRequireDefault(require("../Text"));
var _Paragraph = _interopRequireDefault(require("../Paragraph"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const toBold = value => value === undefined ? undefined : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.default, {
  bold: true,
  children: value
});
const getDefaultText = key => {
  let fallback;
  switch (key) {
    case 'cancelButton':
      fallback = 'Cancel';
      break;
    default:
      throw new Error('Unknown key');
  }
  const content = `ListOperation.${key}`;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
    content: content,
    fallback: fallback
  });
};
/**
 * `ListOperation` component is used in [Toolbar](#!/Toolbar) for performing
 * operations on multiple objects in [List](#!/List).
 * @since 2.4.0
 */
const ListOperation = _ref => {
  let {
    critical = false,
    noSelectionText,
    confirmationText,
    actionButtonText,
    cancelButtonText,
    children,
    ...props
  } = _ref;
  const {
    selectedRows,
    selectedRowTitle
  } = (0, _react.useContext)(_ListOperationContext.default);
  const [settings, setSettings] = (0, _react.useState)(null);
  const targetRef = (0, _react.useRef)(null);
  if (! /*#__PURE__*/(0, _react.isValidElement)(children)) {
    throw new Error('The "children" property must be an element.');
  }
  const toText = prop => {
    if (typeof prop === 'function') {
      return prop({
        selectedRows: toBold(selectedRows),
        selectedRowTitle: toBold(selectedRowTitle)
      });
    }
    return prop;
  };
  const handleClick = e => {
    targetRef.current = e.currentTarget;
    switch (true) {
      case selectedRows && critical:
        setSettings({
          message: toText(confirmationText),
          actionButtonText: toText(actionButtonText),
          actionButtonIntent: 'danger',
          cancelButtonText: toText(cancelButtonText) || getDefaultText('cancelButton')
        });
        break;
      case !selectedRows && !critical:
        setSettings({
          message: toText(noSelectionText),
          actionButtonText: toText(actionButtonText),
          actionButtonIntent: 'primary',
          cancelButtonText: toText(cancelButtonText) || getDefaultText('cancelButton')
        });
        break;
      case !selectedRows && critical:
        setSettings({
          message: toText(noSelectionText),
          icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.default, {
            intent: "info",
            name: "info-circle-filled"
          })
        });
        break;
      case selectedRows && !critical:
        (0, _utils.safeInvoke)(children.props.onClick, e);
        break;
    }
  };
  const handleCancel = () => {
    setSettings(null);
  };
  const handleAction = e => {
    setSettings(null);
    (0, _utils.safeInvoke)(children.props.onClick, e);
  };
  const target = /*#__PURE__*/(0, _react.cloneElement)(children, {
    ...props,
    onClick: handleClick
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Popover.default, {
    visible: !!settings,
    target: target,
    targetRef: targetRef,
    placement: "top",
    onClose: handleCancel,
    children: settings && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Media.default, {
      image: settings.icon,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Paragraph.default, {
        children: settings.message
      }), settings.actionButtonText && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
          onClick: handleAction,
          intent: settings.actionButtonIntent,
          children: settings.actionButtonText
        }), ` `, /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
          ghost: true,
          onClick: handleCancel,
          children: settings.cancelButtonText
        })]
      })]
    })
  });
};
var _default = exports.default = ListOperation;