'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var clsx = require('clsx');
var React = require('react');
var reactIntl = require('react-intl');
require('../common/theme.js');
require('../common/direction.js');
var control = require('../common/propsValues/control.js');
require('../common/propsValues/breakpoint.js');
var size = require('../common/propsValues/size.js');
require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
require('../common/propsValues/monthFormat.js');
require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
require('../common/propsValues/sentiment.js');
require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
require('@transferwise/icons');
require('../common/closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');
var ProcessIndicator = require('../processIndicator/ProcessIndicator.js');
var Button_messages = require('../i18n/commonMessages/Button.messages.js');
var classMap = require('./classMap.js');
var legacyUtils = require('./legacyUtils/legacyUtils.js');

const LegacyButton = /*#__PURE__*/React.forwardRef(({
  as: component = 'button',
  block = false,
  children,
  className,
  disabled,
  loading = false,
  priority = control.Priority.PRIMARY,
  size: size$1 = size.Size.MEDIUM,
  type = control.ControlType.ACCENT,
  onClick,
  ...rest
}, ref) => {
  const intl = reactIntl.useIntl();
  legacyUtils.logDeprecationNotices({
    size: size$1,
    type
  });
  const newType = legacyUtils.establishNewType(type);
  const newPriority = legacyUtils.establishNewPriority(priority, type);
  const classes = clsx.clsx(`btn btn-${size$1}`, `np-btn np-btn-${size$1}`, {
    'btn-loading': loading,
    'btn-block np-btn-block': block,
    disabled
  },
  // @ts-expect-error fix when refactor `typeClassMap` to TypeScript
  classMap.typeClassMap[newType],
  // @ts-expect-error fix when refactor `typeClassMap` to TypeScript
  classMap.priorityClassMap[newPriority], className);
  function processIndicatorSize() {
    return ['sm', 'xs'].includes(size$1) ? 'xxs' : 'xs';
  }
  const Element = component ?? 'button';
  let props;
  if (Element === 'button') {
    const {
      htmlType = 'button',
      ...restProps
    } = rest;
    props = {
      ...restProps,
      disabled,
      'aria-disabled': loading,
      type: htmlType
    };
  } else {
    props = {
      ...rest,
      'aria-disabled': loading
    };
  }
  /**
   * Ensures that the button cannot be activated in loading or disabled mode,
   * when `aria-disabled` might be used over the `disabled` HTML attribute
   */
  const handleClick = handler => event => {
    if (disabled || loading) {
      event.preventDefault();
    } else if (typeof handler === 'function') {
      handler(event);
    }
  };
  return /*#__PURE__*/jsxRuntime.jsxs(Element, {
    ref: ref,
    className: classes,
    onClick: handleClick(onClick),
    ...props,
    "aria-live": loading ? 'polite' : 'off',
    "aria-busy": loading,
    "aria-label": loading ? intl.formatMessage(Button_messages.default.loadingAriaLabel) : rest['aria-label'],
    children: [children, loading && /*#__PURE__*/jsxRuntime.jsx(ProcessIndicator.default, {
      size: processIndicatorSize(),
      className: "btn-loader",
      "data-testid": "ButtonProgressIndicator"
    })]
  });
});

exports.default = LegacyButton;
//# sourceMappingURL=LegacyButton.js.map
