import { clsx } from 'clsx';
import { forwardRef } from 'react';
import { useIntl } from 'react-intl';
import ProcessIndicator from '../processIndicator/ProcessIndicator.mjs';
import messages from './Button.messages.mjs';
import { typeClassMap, priorityClassMap } from './classMap.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';
import { logDeprecationNotices, establishNewType, establishNewPriority } from './legacyUtils/legacyUtils.mjs';
import { Priority, ControlType } from '../common/propsValues/control.mjs';
import { Size } from '../common/propsValues/size.mjs';

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

export { Button as default };
//# sourceMappingURL=Button.mjs.map
