import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { 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';

/**
 * `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 = ({
  baseClassName = `${CLS_PREFIX}action`,
  component: Tag = 'a',
  icon,
  rightAddon,
  bottomAddon,
  children,
  progress = false,
  nowrap = false,
  outerProps = {},
  ...props
}) => {
  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);
  }

  if (progress) {
    icon = /*#__PURE__*/React.createElement(Icon, {
      size: size,
      className: `${baseClassName}__icon`
    }, /*#__PURE__*/React.createElement(Spinner, null));
  }

  const tagProps = { ...props,
    className: classNames(props.className, `${baseClassName}__content`) || undefined
  };

  if (tagProps.onClick) {
    tagProps.tabIndex = tagProps.tabIndex || 0;
    tagProps.onKeyDown = wrapFunction(tagProps.onKeyDown, e => {
      if (e.key === 'Enter') {
        e.preventDefault();
        e.currentTarget.click();
      }
    });
  }

  return /*#__PURE__*/React.createElement(RootTag, _extends({
    className: classNames(baseClassName, {
      [`${baseClassName}--size-${size}`]: icon || progress,
      [`${baseClassName}--progress`]: progress,
      [`${baseClassName}--nowrap`]: nowrap,
      [`${baseClassName}--icon`]: !Children.toArray(children).length && !rightAddon && !bottomAddon
    }, className)
  }, restOuterProps), /*#__PURE__*/React.createElement(Tag, tagProps, icon, children), rightAddon ? /*#__PURE__*/React.createElement("span", {
    className: `${baseClassName}__right-addon`
  }, rightAddon) : null, bottomAddon ? /*#__PURE__*/React.createElement("span", {
    className: `${baseClassName}__bottom-addon`
  }, bottomAddon) : null);
};

export default Action;