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

import { isValidElement, Children } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import { wrapFunction } from '../utils';
import Icon, { ICON_SIZE_16 } from '../Icon';
import Spinner from '../Spinner';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `Action` component is used mostly for navigation, to go between pages, and not for immediate action or submitting data.
 * @since 0.0.68
 */
const Action = _ref => {
  let {
    baseClassName = `${CLS_PREFIX}action`,
    component: Tag = 'a',
    icon,
    onClick,
    rightAddon,
    bottomAddon,
    children,
    progress = false,
    nowrap = false,
    outerProps = {},
    ...props
  } = _ref;
  const {
    className,
    component: RootTag = 'span',
    ...restOuterProps
  } = outerProps;
  icon = Icon.create(icon, {
    className: `${baseClassName}__icon`,
    size: ICON_SIZE_16
  });
  let size = ICON_SIZE_16;
  if ( /*#__PURE__*/isValidElement(icon) && icon.props.size) {
    ({
      size
    } = icon.props);
  }
  const tagProps = {
    ...props,
    onClick,
    className: classNames(props.className, `${baseClassName}__content`) || undefined
  };
  if (progress) {
    icon = /*#__PURE__*/_jsx(Icon, {
      size: size,
      className: `${baseClassName}__icon`,
      children: /*#__PURE__*/_jsx(Spinner, {})
    });
    tagProps.onClick = e => e.preventDefault();
    tagProps.tabIndex = -1;
  }

  // accessibility
  if (onClick) {
    tagProps.tabIndex = tagProps.tabIndex || 0;
    tagProps.onKeyDown = wrapFunction(tagProps.onKeyDown, e => {
      if (e.key === 'Enter') {
        e.preventDefault();
        e.currentTarget.click();
      }
    });
  }
  return /*#__PURE__*/_jsxs(RootTag, {
    className: classNames(baseClassName, {
      [`${baseClassName}--size-${size}`]: icon || progress,
      [`${baseClassName}--progress`]: progress,
      [`${baseClassName}--nowrap`]: nowrap,
      [`${baseClassName}--icon`]: !Children.toArray(children).length && !rightAddon && !bottomAddon
    }, className),
    ...restOuterProps,
    children: [/*#__PURE__*/_jsxs(Tag, {
      ...tagProps,
      children: [icon, children]
    }), rightAddon ? /*#__PURE__*/_jsx("span", {
      className: `${baseClassName}__right-addon`,
      children: rightAddon
    }) : null, bottomAddon ? /*#__PURE__*/_jsx("span", {
      className: `${baseClassName}__bottom-addon`,
      children: bottomAddon
    }) : null]
  });
};
export default Action;