'use strict';

var clsx = require('clsx');
var React = require('react');
var reactIntl = require('react-intl');
var ProcessIndicator = require('../processIndicator/ProcessIndicator.js');
var Button_messages = require('./Button.messages.js');
var classMap = require('./classMap.js');
var jsxRuntime = require('react/jsx-runtime');
var legacyUtils = require('./legacyUtils/legacyUtils.js');
var control = require('../common/propsValues/control.js');
var size = require('../common/propsValues/size.js');

const Button = /*#__PURE__*/React.forwardRef(({
  as: component,
  block = false,
  children,
  className,
  disabled,
  loading = false,
  priority = control.Priority.PRIMARY,
  size: size$1 = size.Size.MEDIUM,
  type = control.ControlType.ACCENT,
  onClick,
  ...rest
}, reference) => {
  const intl = reactIntl.useIntl();
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  legacyUtils.logDeprecationNotices({
    size: size$1,
    type
  });
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  const newType = legacyUtils.establishNewType(type);
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  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
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  classMap.typeClassMap[newType],
  // @ts-expect-error fix when refactor `typeClassMap` to TypeScript
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  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: reference,
    className: classes,
    onClick: handleClick(onClick),
    ...props,
    "aria-live": loading ? 'polite' : 'off',
    "aria-busy": loading,
    "aria-label": loading ? intl.formatMessage(Button_messages.loadingAriaLabel) : rest['aria-label'],
    children: [children, loading && /*#__PURE__*/jsxRuntime.jsx(ProcessIndicator, {
      size: processIndicatorSize(),
      className: "btn-loader",
      "data-testid": "ButtonProgressIndicator"
    })]
  });
});

module.exports = Button;
//# sourceMappingURL=Button.js.map
