"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
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"));
var _ButtonContext = _interopRequireDefault(require("./ButtonContext"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.
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 = _ref => {
  let {
    baseClassName,
    icon,
    size,
    empty
  } = _ref;
  return _Icon.default.create(oldIconNames[icon] || icon, {
    className: `${baseClassName}__icon`,
    size: size === 'lg' || empty ? '16' : '12'
  });
};
const renderCaret = _ref2 => {
  let {
    baseClassName,
    size,
    caret
  } = _ref2;
  let caretElement;
  if (typeof caret === 'boolean' && caret) {
    caretElement = /*#__PURE__*/(0, _jsxRuntime.jsx)(_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;
};
const Button = /*#__PURE__*/(0, _react.forwardRef)((_ref3, ref) => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}button`,
    className,
    component,
    size: propsSize,
    intent,
    ghost = false,
    selected: selectedProp,
    onToggle,
    state,
    icon,
    caret,
    fill = false,
    tooltip,
    children,
    arrow,
    disabled = false,
    ...props
  } = _ref3;
  const Tag = component || 'button';
  const [selectedState, setSelectedState] = (0, _react.useState)(false);
  const handleToggle = () => {
    setSelectedState(!selectedState);
    return !selectedState;
  };
  const isCollapsed = (0, _react.useContext)(_ResponsiveContext.default);
  const buttonContextConfig = (0, _react.useContext)(_ButtonContext.default);
  const size = propsSize || buttonContextConfig.defaultProps.size;
  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'] = `${selected}`;
  }
  const isArrowButton = arrow === 'forward' || arrow === 'backward';
  const hasAriaDisabled = Tag === 'button' && disabled && tooltip;
  let button = /*#__PURE__*/(0, _jsxRuntime.jsxs)(Tag, {
    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,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
      className: `${baseClassName}__inner`,
      children: [state === 'loading' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Spinner.default, {
        className: `${baseClassName}__spinner`
      }) : renderIcon({
        baseClassName,
        icon,
        size,
        empty: !childrenElement
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: childrenElement
      }), renderCaret({
        baseClassName,
        size,
        caret
      })]
    }), isArrowButton && !ghost && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: `${baseClassName}__arrow`
    })]
  });
  if (tooltipElement) {
    button = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip.default, {
      title: tooltipElement,
      children: button
    });
  }
  return button;
});
Button.displayName = 'Button';

// For ToolbarMenu backward compatibility purposes
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
};

/**
 * `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
 */
var _default = exports.default = Button;