"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Button = void 0;
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _nativeProps = require("../../utils/native-props");
var _validate = require("../../utils/validate");
var _withDefaultProps = require("../../utils/with-default-props");
var _configProvider = require("../config-provider");
var _dotLoading = _interopRequireDefault(require("../dot-loading"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const defaultProps = {
  color: 'default',
  fill: 'solid',
  block: false,
  loading: false,
  loadingIcon: _react.default.createElement(_dotLoading.default, {
    color: 'currentColor'
  }),
  type: 'button',
  shape: 'default',
  size: 'middle'
};
const Button = exports.Button = (0, _react.forwardRef)((p, ref) => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const {
    getPrefixCls
  } = (0, _configProvider.useConfig)();
  const prefixCls = getPrefixCls('button', props.prefixCls);
  const [innerLoading, setInnerLoading] = (0, _react.useState)(false);
  const nativeButtonRef = (0, _react.useRef)(null);
  const loading = props.loading === 'auto' ? innerLoading : props.loading;
  const disabled = props.disabled || loading;
  (0, _react.useImperativeHandle)(ref, () => ({
    get nativeElement() {
      return nativeButtonRef.current;
    }
  }));
  const handleClick = e => (0, _tslib.__awaiter)(void 0, void 0, void 0, function* () {
    if (!props.onClick) return;
    const promise = props.onClick(e);
    if ((0, _validate.isPromise)(promise)) {
      try {
        setInnerLoading(true);
        yield promise;
        setInnerLoading(false);
      } catch (e) {
        setInnerLoading(false);
        throw e;
      }
    }
  });
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("button", {
    ref: nativeButtonRef,
    type: props.type,
    form: props.form,
    onClick: handleClick,
    className: (0, _classnames.default)(prefixCls, {
      [`${prefixCls}-${props.color}`]: props.color,
      [`${prefixCls}-block`]: props.block,
      [`${prefixCls}-disabled`]: disabled,
      [`${prefixCls}-fill-outline`]: props.fill === 'outline',
      [`${prefixCls}-fill-none`]: props.fill === 'none',
      [`${prefixCls}-mini`]: props.size === 'mini',
      [`${prefixCls}-small`]: props.size === 'small',
      [`${prefixCls}-large`]: props.size === 'large',
      [`${prefixCls}-loading`]: loading
    }, `${prefixCls}-shape-${props.shape}`),
    disabled: disabled,
    onMouseDown: props.onMouseDown,
    onMouseUp: props.onMouseUp,
    onTouchStart: props.onTouchStart,
    onTouchEnd: props.onTouchEnd
  }, loading ? _react.default.createElement("div", {
    className: `${prefixCls}-loading-wrapper`
  }, props.loadingIcon, props.loadingText) : _react.default.createElement("span", null, props.children)));
});