"use strict";

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

var _react = _interopRequireDefault(require("react"));

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

var _indexModule = _interopRequireDefault(require("./styles/index.module.less"));

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

var _shadow = _interopRequireDefault(require("../shadow"));

var _needWrap = _interopRequireDefault(require("../need-wrap"));

var _space = _interopRequireDefault(require("../space"));

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

var _excluded = ["children", "className", "shadow", "size", "type", "icon", "bold", "color", "style", "loadingText", "disabled", "iconPosition", "round", "block", "ghost", "loading"];

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var _default = function _default(_ref) {
  var children = _ref.children,
      className = _ref.className,
      _ref$shadow = _ref.shadow,
      shadow = _ref$shadow === void 0 ? false : _ref$shadow,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'normal' : _ref$size,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'default' : _ref$type,
      icon = _ref.icon,
      bold = _ref.bold,
      color = _ref.color,
      style = _ref.style,
      loadingText = _ref.loadingText,
      disabled = _ref.disabled,
      _ref$iconPosition = _ref.iconPosition,
      iconPosition = _ref$iconPosition === void 0 ? 'left' : _ref$iconPosition,
      _ref$round = _ref.round,
      round = _ref$round === void 0 ? false : _ref$round,
      _ref$block = _ref.block,
      block = _ref$block === void 0 ? true : _ref$block,
      _ref$ghost = _ref.ghost,
      ghost = _ref$ghost === void 0 ? false : _ref$ghost,
      _ref$loading = _ref.loading,
      loading = _ref$loading === void 0 ? false : _ref$loading,
      restProps = _objectWithoutProperties(_ref, _excluded);

  if (color) {
    style.color = ghost ? color : '#fff';

    if (!ghost) {
      // Use background instead of backgroundColor to make linear-gradient work
      style.background = color;
    } // hide border when color is linear-gradient


    if (color.indexOf('gradient') !== -1) {
      style.border = 0;
    } else {
      style.borderColor = color;
    }
  }

  var renderIcon = function renderIcon() {
    if (loading) {
      return /*#__PURE__*/_react.default.createElement(_icon.default, {
        name: 'loading2',
        color: type === 'default' ? '#999' : '#fff'
      });
    } else {
      return icon;
    }
  };

  var renderContent = function renderContent() {
    var text;

    if (loading) {
      text = loadingText;
    } else {
      text = children;
    }

    if (text) {
      return /*#__PURE__*/_react.default.createElement(_one.Text, null, text);
    }

    return null;
  };

  return /*#__PURE__*/_react.default.createElement(_needWrap.default, {
    wrap: _shadow.default,
    need: shadow
  }, /*#__PURE__*/_react.default.createElement(_one.Button, _objectSpread({
    "aria-label": typeof children === 'string' ? children : undefined,
    "aria-disabled": disabled,
    className: (0, _classnames.default)(className, _indexModule.default.button, _indexModule.default[size], _indexModule.default[type], block && _indexModule.default.block, loading && _indexModule.default.loading, ghost && _indexModule.default.ghost, round && _indexModule.default.round, bold && _indexModule.default.bold, disabled && _indexModule.default.disable, type === 'default' && shadow && _indexModule.default.noBorder, type === 'default' && shadow && _indexModule.default["".concat(type, "-noBorder")]),
    style: _objectSpread({}, style)
  }, restProps), /*#__PURE__*/_react.default.createElement(_space.default, {
    alignItems: 'center',
    justify: 'center'
  }, iconPosition === 'left' && renderIcon(), renderContent(), iconPosition === 'right' && renderIcon())));
};

exports.default = _default;