"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Focusable = require("../../common/Focusable");
var _ButtonCoreSt = require("./ButtonCore.st.css.js");
var _constants = require("./constants");
var _utils = require("./utils");
var _excluded = ["as", "suffixIcon", "prefixIcon", "children", "disabled", "focusableOnFocus", "focusableOnBlur", "href", "contentClassName", "contentRef", "onClick", "onKeyDown", "onKeyUp"];
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Button/ButtonCore/ButtonCore.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var _addAffix = (Affix, styleClass, dataHook) => Affix && /*#__PURE__*/React.cloneElement(Affix, {
  className: (0, _classnames.default)(_ButtonCoreSt.classes[styleClass], Affix.props.className),
  'data-hook': Affix.props['data-hook'] || Affix.props.dataHook || dataHook
});

/**
 * ButtonCore
 */
class ButtonCore extends React.Component {
  constructor() {
    super(...arguments);
    this.innerComponentRef = /*#__PURE__*/React.createRef();
  }
  focus() {
    if (this.innerComponentRef && this.innerComponentRef.focus) {
      this.innerComponentRef.focus();
    }
  }

  /**
   * Returns the size and position of the element
   */
  getBoundingClientRect() {
    if (this.innerComponentRef && this.innerComponentRef.getBoundingClientRect) {
      return this.innerComponentRef.getBoundingClientRect();
    }
    return;
  }
  render() {
    var _this$props = this.props,
      {
        as: Component,
        suffixIcon,
        prefixIcon,
        children,
        disabled,
        focusableOnFocus,
        focusableOnBlur,
        href,
        contentClassName,
        contentRef,
        onClick,
        onKeyDown,
        onKeyUp
      } = _this$props,
      rest = (0, _objectWithoutProperties2.default)(_this$props, _excluded);
    var htmlTabIndex = rest.tabIndex || 0;
    var htmlHref = disabled ? undefined : href;
    var reference = (0, _utils.isStatelessComponent)(Component) && typeof Component !== 'string' ? undefined : ref => this.innerComponentRef = ref;

    // Block action handlers when disabled (using aria-disabled instead of native disabled)
    var handleClick = disabled ? e => e.preventDefault() : onClick;
    var handleKeyDown = disabled ? e => {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
      }
    } : onKeyDown;
    if (!Component) {
      return null;
    }
    return /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({
      "data-hook": _constants.dataHooks.button
    }, rest, {
      onFocus: focusableOnFocus,
      onBlur: focusableOnBlur,
      onClick: handleClick,
      onKeyDown: handleKeyDown,
      onKeyUp: disabled ? undefined : onKeyUp,
      href: htmlHref,
      ref: reference,
      tabIndex: htmlTabIndex,
      "aria-disabled": disabled || undefined,
      className: (0, _ButtonCoreSt.st)(_ButtonCoreSt.classes.root, {
        disabled
      }, this.props.className),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 117,
        columnNumber: 7
      }
    }), prefixIcon && _addAffix(prefixIcon, 'prefix', _constants.dataHooks.prefixIcon), /*#__PURE__*/React.createElement("span", {
      className: (0, _ButtonCoreSt.st)(_ButtonCoreSt.classes.content, contentClassName),
      ref: contentRef,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 132,
        columnNumber: 9
      }
    }, children), suffixIcon && _addAffix(suffixIcon, 'suffix', _constants.dataHooks.suffixIcon));
  }
}
ButtonCore.defaultProps = {
  as: 'button',
  type: 'button'
};
ButtonCore.displayName = 'ButtonCore';
var _default = exports.default = (0, _Focusable.withFocusable)(ButtonCore);
//# sourceMappingURL=ButtonCore.js.map