// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { useContext, useState, useRef, cloneElement, isValidElement } from 'react';
import { safeInvoke } from '../utils';
import ListOperationContext from './ListOperationContext';
import Popover from '../Popover';
import Translate from '../Translate';
import Button from '../Button';
import Media from '../Media';
import Icon from '../Icon';
import Text from '../Text';
import Paragraph from '../Paragraph';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const toBold = value => value === undefined ? undefined : /*#__PURE__*/_jsx(Text, {
  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__*/_jsx(Translate, {
    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
  } = useContext(ListOperationContext);
  const [settings, setSettings] = useState(null);
  const targetRef = useRef(null);
  if (! /*#__PURE__*/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__*/_jsx(Icon, {
            intent: "info",
            name: "info-circle-filled"
          })
        });
        break;
      case selectedRows && !critical:
        safeInvoke(children.props.onClick, e);
        break;
    }
  };
  const handleCancel = () => {
    setSettings(null);
  };
  const handleAction = e => {
    setSettings(null);
    safeInvoke(children.props.onClick, e);
  };
  const target = /*#__PURE__*/cloneElement(children, {
    ...props,
    onClick: handleClick
  });
  return /*#__PURE__*/_jsx(Popover, {
    visible: !!settings,
    target: target,
    targetRef: targetRef,
    placement: "top",
    onClose: handleCancel,
    children: settings && /*#__PURE__*/_jsxs(Media, {
      image: settings.icon,
      children: [/*#__PURE__*/_jsx(Paragraph, {
        children: settings.message
      }), settings.actionButtonText && /*#__PURE__*/_jsxs("div", {
        children: [/*#__PURE__*/_jsx(Button, {
          onClick: handleAction,
          intent: settings.actionButtonIntent,
          children: settings.actionButtonText
        }), ` `, /*#__PURE__*/_jsx(Button, {
          ghost: true,
          onClick: handleCancel,
          children: settings.cancelButtonText
        })]
      })]
    })
  });
};
export default ListOperation;