"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getNonNativeButtonAttrs = getNonNativeButtonAttrs;
exports.getTabIndex = getTabIndex;
exports.shouldSubmitViaKeyDown = shouldSubmitViaKeyDown;
exports.useNonNativeButtonAttrs = useNonNativeButtonAttrs;
var _react = require("react");
function getTabIndex(isDisabled = false, tabIndex = 0) {
  if (isDisabled) {
    return -1;
  }
  return tabIndex;
}
function shouldSubmitViaKeyDown(e) {
  return e.key === ' ' || e.key === 'Enter';
}
function getNonNativeButtonAttrs({
  onKeyDown,
  onClick,
  tabIndex,
  isDisabled = false
}) {
  return {
    tabIndex: getTabIndex(isDisabled, tabIndex),
    role: 'button',
    'aria-disabled': isDisabled,
    onKeyDown(e) {
      if (shouldSubmitViaKeyDown(e) && !isDisabled) {
        onKeyDown?.(e);
      }
    },
    onClick(e) {
      if (!isDisabled) {
        onClick?.(e);
      }
    }
  };
}
function useNonNativeButtonAttrs({
  onKeyDown,
  onClick,
  tabIndex,
  isDisabled = false
}) {
  const attrs = (0, _react.useMemo)(() => getNonNativeButtonAttrs({
    isDisabled,
    tabIndex,
    onClick,
    onKeyDown
  }), [isDisabled, tabIndex, onClick, onKeyDown]);
  return attrs;
}