// Copyright 1999-2025. WebPros International GmbH. All rights reserved.

import { cloneElement, forwardRef, isValidElement, useContext, useState } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Icon from '../Icon';
import Spinner from '../Spinner';
import Tooltip from '../Tooltip';
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"
};
import ResponsiveContext from '../ResponsiveContext';
import OnDarkContext from '../OnDarkContext';
import ButtonContext from './ButtonContext';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const renderIcon = _ref => {
  let {
    baseClassName,
    icon,
    size,
    empty
  } = _ref;
  return Icon.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__*/_jsx(Icon, {
      name: "chevron-down",
      size: size === 'lg' ? '16' : '12'
    });
  } else {
    caretElement = caret;
  }
  if (/*#__PURE__*/isValidElement(caretElement)) {
    caretElement = /*#__PURE__*/cloneElement(caretElement, {
      className: classNames(`${baseClassName}__caret`, caretElement.props.className)
    });
  }
  return caretElement;
};

// For ToolbarMenu backward compatibility purposes
export const BUTTON_PROP_NAMES = ['size', 'intent', 'ghost', 'selected', 'onToggle', 'state', 'disabled', 'tooltip', 'icon', 'caret', 'fill', 'children', 'component', 'className', 'baseClassName', 'arrow', 'onClick'];
const Button = /*#__PURE__*/forwardRef((_ref3, ref) => {
  let {
    baseClassName = `${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] = useState(false);
  const handleToggle = () => {
    setSelectedState(!selectedState);
    return !selectedState;
  };
  const isCollapsed = useContext(ResponsiveContext);
  const buttonContextConfig = useContext(ButtonContext);
  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__*/_jsxs(Tag, {
    ref: ref,
    className: classNames(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`]: useContext(OnDarkContext)
    }, className),
    disabled: hasAriaDisabled ? undefined : disabled,
    "aria-disabled": hasAriaDisabled ? 'true' : undefined,
    ...componentProps,
    ...props,
    children: [/*#__PURE__*/_jsxs("span", {
      className: `${baseClassName}__inner`,
      children: [state === 'loading' ? /*#__PURE__*/_jsx(Spinner, {
        className: `${baseClassName}__spinner`
      }) : renderIcon({
        baseClassName,
        icon,
        size,
        empty: !childrenElement
      }), /*#__PURE__*/_jsx("span", {
        children: childrenElement
      }), renderCaret({
        baseClassName,
        size,
        caret
      })]
    }), isArrowButton && !ghost && /*#__PURE__*/_jsx("span", {
      className: `${baseClassName}__arrow`
    })]
  });
  if (tooltipElement) {
    button = /*#__PURE__*/_jsx(Tooltip, {
      title: tooltipElement,
      children: button
    });
  }
  return button;
});
Button.displayName = 'Button';

/**
 * `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
 */
export default Button;