"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _Icon = _interopRequireDefault(require("../Icon"));

var _Spinner = _interopRequireDefault(require("../Spinner"));

var _Tooltip = _interopRequireDefault(require("../Tooltip"));

var _ResponsiveContext = _interopRequireDefault(require("../ResponsiveContext"));

var _OnDarkContext = _interopRequireDefault(require("../OnDarkContext"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

/* eslint-disable react/no-deprecated */
const oldIconNames = {
  "customize-order": "arrows-four-directions",
  download: "arrow-down-tray",
  edit: "pencil",
  folder: "folder-closed",
  "globe-alt": "globe",
  "import": "arrow-down-in",
  invisible: "eye-closed",
  link: "chain",
  lock: "lock-closed",
  "ok-circle": "check-mark-circle",
  "question-circle": "question-mark-circle",
  "security-scan": "shield",
  settings: "sliders",
  suspend: "sleep",
  unlock: "lock-open",
  upload: "arrow-up-tray",
  visible: "eye"
};

const renderIcon = ({
  baseClassName,
  icon,
  size,
  empty
}) => _Icon.default.create(oldIconNames[icon] || icon, {
  className: `${baseClassName}__icon`,
  size: size === 'lg' || empty ? '16' : '12'
});

const renderCaret = ({
  baseClassName,
  size,
  caret
}) => {
  let caretElement;

  if (typeof caret === 'boolean' && caret) {
    caretElement = /*#__PURE__*/_react.default.createElement(_Icon.default, {
      name: "chevron-down",
      size: size === 'lg' ? '16' : '12'
    });
  } else {
    caretElement = caret;
  }

  if ( /*#__PURE__*/(0, _react.isValidElement)(caretElement)) {
    caretElement = /*#__PURE__*/(0, _react.cloneElement)(caretElement, {
      className: (0, _classnames.default)(`${baseClassName}__caret`, caretElement.props.className)
    });
  }

  return caretElement;
};
/**
 * `Button` component is used for executing actions, changing the application state, opening forms or dialog windows, and submitting data.
 * As a rule, you should not use buttons for navigation between individual pages (use `Link` instead).
 * @since 0.0.37
 */


const Button = /*#__PURE__*/(0, _react.forwardRef)(({
  baseClassName,
  className,
  component: Tag,
  size,
  intent,
  ghost,
  selected: selectedProp,
  onToggle,
  state,
  icon,
  caret,
  fill,
  tooltip,
  children,
  arrow,
  disabled,
  ...props
}, ref) => {
  const [selectedState, setSelectedState] = (0, _react.useState)(false);

  const handleToggle = () => {
    setSelectedState(!selectedState);
    return !selectedState;
  };

  const isCollapsed = (0, _react.useContext)(_ResponsiveContext.default);
  const componentProps = {};

  if (Tag === 'button') {
    componentProps.type = 'button';
  } else if (Tag === 'a') {
    componentProps.role = 'button';
    componentProps.tabIndex = 0;

    if (disabled) {
      props.href = undefined;
    }
  }

  if (props.onClick && disabled) {
    props.onClick = e => {
      e.preventDefault();
    };
  }

  let childrenElement = children;
  let tooltipElement = tooltip;

  if (isCollapsed && icon && children) {
    childrenElement = null;

    if (!tooltip) {
      tooltipElement = children;
    }
  }

  if (!props.onClick && onToggle) {
    props.onClick = e => {
      e.preventDefault();
      onToggle(handleToggle());
    };
  }

  const selected = selectedProp === undefined ? selectedState : selectedProp;

  if (selectedProp !== undefined || onToggle) {
    componentProps['aria-pressed'] = String(selected);
  }

  const isArrowButton = arrow === 'forward' || arrow === 'backward';
  const hasAriaDisabled = Tag === 'button' && disabled && tooltip;

  let button = /*#__PURE__*/_react.default.createElement(Tag, (0, _extends2.default)({
    ref: ref,
    className: (0, _classnames.default)(baseClassName, {
      [`${baseClassName}--${size}`]: size,
      [`${baseClassName}--${intent}`]: intent && !ghost,
      [`${baseClassName}--ghost`]: ghost,
      [`${baseClassName}--${state}`]: state,
      [`${baseClassName}--selected`]: selected,
      [`${baseClassName}--disabled`]: disabled,
      [`${baseClassName}--empty`]: !childrenElement,
      [`${baseClassName}--caret`]: caret,
      [`${baseClassName}--fill`]: fill,
      [`${baseClassName}--arrow ${baseClassName}--arrow-${arrow}`]: isArrowButton && !ghost,
      [`${baseClassName}--on-dark`]: (0, _react.useContext)(_OnDarkContext.default)
    }, className),
    disabled: hasAriaDisabled ? undefined : disabled,
    "aria-disabled": hasAriaDisabled ? 'true' : undefined
  }, componentProps, props), /*#__PURE__*/_react.default.createElement("span", {
    className: `${baseClassName}__inner`
  }, state === 'loading' ? /*#__PURE__*/_react.default.createElement(_Spinner.default, {
    className: `${baseClassName}__spinner`
  }) : renderIcon({
    baseClassName,
    icon,
    size,
    empty: !childrenElement
  }), /*#__PURE__*/_react.default.createElement("span", null, childrenElement), renderCaret({
    baseClassName,
    size,
    caret
  })), isArrowButton && !ghost && /*#__PURE__*/_react.default.createElement("span", {
    className: `${baseClassName}__arrow`
  }));

  if (tooltipElement) {
    button = /*#__PURE__*/_react.default.createElement(_Tooltip.default, {
      title: tooltipElement
    }, button);
  }

  return button;
});
Button.displayName = 'Button';
Button.propTypes = {
  /**
   * Button size.
   * @since 2.5.0
   */
  size: _propTypes.default.oneOf(['md', 'lg']),

  /**
   * Visual intent color to apply to component.
   * @since 0.0.42
   */
  intent: _propTypes.default.oneOf(['primary', 'secondary', 'success', 'warning', 'danger', 'info']),

  /**
   * Display button with transparent background.
   * @since 2.1.0
   */
  ghost: _propTypes.default.bool,

  /**
   * Arrow button.
   * @since 2.6.0
   */
  arrow: _propTypes.default.oneOf(['forward', 'backward']),

  /**
   * Selected state of toggle button. Used in controlled component mode.
   * @since 0.0.68
   */
  selected: _propTypes.default.bool,

  /**
   * Toggle button callback. Used in uncontrolled component mode.
   * @since 0.0.68
   */
  onToggle: _propTypes.default.func,

  /**
   * State of the button
   * @since 0.0.42
   */
  state: _propTypes.default.oneOf(['active', 'loading', 'hovered', 'focused']),

  /**
   * Is button disabled?
   * @since 0.0.42
   */
  disabled: _propTypes.default.bool,

  /**
   * Tooltip for component
   * @since 0.0.42
   */
  tooltip: _propTypes.default.node,

  /**
   * Name of icon or [Icon](#!/Icon) component
   * @since 0.0.42
   */
  icon: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object, _propTypes.default.element]),

  /**
   * Show caret
   * @since 0.0.42
   */
  caret: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.element]),

  /**
   * Dynamically expand the button to fill out the whole available width of its parent container.
   * @since 1.8.0
   */
  fill: _propTypes.default.bool,

  /**
   * Content of the button
   * @since 0.0.37
   */
  children: _propTypes.default.node,

  /**
   * Component to render as the root element. Useful when rendering a `Button` as `<a>` or `<Link>`.
   * @since 0.0.42
   */
  component: _propTypes.default.elementType,

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string,

  /**
   * @ignore
   */
  onClick: _propTypes.default.func
};
Button.defaultProps = {
  size: undefined,
  intent: undefined,
  ghost: false,
  selected: undefined,
  onToggle: undefined,
  state: undefined,
  disabled: false,
  tooltip: undefined,
  icon: undefined,
  caret: undefined,
  fill: false,
  children: undefined,
  component: 'button',
  className: undefined,
  baseClassName: `${_constants.CLS_PREFIX}button`,
  arrow: undefined,
  onClick: undefined
};
var _default = Button;
exports.default = _default;