"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _tslib = require("tslib");

var React = _interopRequireWildcard(require("react"));

var _one = require("remax/one");

var _classnames = _interopRequireDefault(require("classnames"));

var _utils = require("../_utils");

var _common = require("../common");

var _loading = _interopRequireDefault(require("../loading"));

var _icon = _interopRequireDefault(require("../icon"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var prefixCls = (0, _common.getPrefixCls)('btn');
var ButtonTypes = (0, _utils.tuple)('default', 'primary');

var AButton = function AButton(props) {
  var _classNames;

  var type = props.type,
      size = props.size,
      className = props.className,
      style = props.style,
      children = props.children,
      onTap = props.onTap,
      disabled = props.disabled,
      danger = props.danger,
      shape = props.shape,
      block = props.block,
      float = props.float,
      loading = props.loading,
      _props$loadingColor = props.loadingColor,
      loadingColor = _props$loadingColor === void 0 ? "#fdfdfd" : _props$loadingColor,
      loadingText = props.loadingText,
      plain = props.plain,
      hairline = props.hairline,
      look = props.look,
      color = props.color,
      icon = props.icon,
      ghost = props.ghost,
      _props$nativeType = props.nativeType,
      nativeType = _props$nativeType === void 0 ? 'button' : _props$nativeType,
      restProps = (0, _tslib.__rest)(props, ["type", "size", "className", "style", "children", "onTap", "disabled", "danger", "shape", "block", "float", "loading", "loadingColor", "loadingText", "plain", "hairline", "look", "color", "icon", "ghost", "nativeType"]);

  var handleTap = function handleTap(e) {
    if (disabled) {
      return;
    }

    if (loading) {
      return;
    }

    onTap === null || onTap === void 0 ? void 0 : onTap(e);
  };

  var renderIcon = function renderIcon(childrenElement) {
    var _a;

    var iconSize = '32px';

    if (size === 'small') {
      iconSize = '28px';
    }

    if (size === 'large') {
      iconSize = '36px';
    }

    var iconColor = '#1890FF';

    if (type === 'primary') {
      iconColor = '#FDFFFD';
    }

    if (plain) {
      if (type === 'primary') {
        iconColor = '#1890FF';
      }

      if (color) {
        iconColor = color;
      }
    }

    if (typeof icon === 'string') {
      return /*#__PURE__*/React.createElement(_icon.default, {
        type: icon,
        color: iconColor,
        size: iconSize,
        style: {
          verticalAlign: '-0.05em',
          marginRight: childrenElement ? '6px' : undefined
        }
      });
    }

    if ( /*#__PURE__*/React.isValidElement(icon)) {
      return Object.assign(Object.assign({}, icon), {
        props: Object.assign(Object.assign({}, icon === null || icon === void 0 ? void 0 : icon.props), {
          style: Object.assign({
            verticalAlign: '-0.05em',
            marginRight: childrenElement ? '6px' : undefined
          }, (_a = icon === null || icon === void 0 ? void 0 : icon.props) === null || _a === void 0 ? void 0 : _a.style)
        })
      });
    }

    return null;
  };

  var classes = (0, _classnames.default)(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(shape), shape), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _defineProperty(_classNames, "".concat(prefixCls, "-primary"), type === 'primary'), _defineProperty(_classNames, "".concat(prefixCls, "-ghost"), ghost), _defineProperty(_classNames, "".concat(prefixCls, "-plain"), plain), _defineProperty(_classNames, "".concat(prefixCls, "-hairline"), hairline), _defineProperty(_classNames, "".concat(prefixCls, "-block"), block), _defineProperty(_classNames, "".concat(prefixCls, "-float"), float), _defineProperty(_classNames, "".concat(prefixCls, "-float-").concat(float), float), _defineProperty(_classNames, "".concat(prefixCls, "-danger-default"), danger), _defineProperty(_classNames, "".concat(prefixCls, "-danger"), type === 'primary' && danger), _defineProperty(_classNames, "".concat(prefixCls, "-look-").concat(look), look), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), loading), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled || loading), _classNames));
  var childrenElement = loading && loadingText ? loadingText : children;
  var iconElement = renderIcon(childrenElement);
  return /*#__PURE__*/React.createElement(_one.Button, Object.assign({
    className: classes,
    // @ts-ignore
    type: nativeType,
    disabled: disabled,
    onTap: handleTap,
    style: Object.assign(Object.assign({}, style), {
      color: color,
      borderColor: color
    })
  }, restProps), /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-content")
  }, loading ? /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-loading-icon")
  }, /*#__PURE__*/React.createElement(_loading.default, {
    color: loadingColor,
    style: {
      verticalAlign: '-0.3em'
    }
  })) : iconElement, /*#__PURE__*/React.createElement(_one.Text, {
    className: "".concat(prefixCls, "-text")
  }, childrenElement), /*#__PURE__*/React.createElement(_one.Text, {
    className: "".concat(prefixCls, "-mask")
  })));
};

var _default = AButton;
exports.default = _default;