"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx2 = _interopRequireDefault(require("clsx"));

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

var _constant = require("../utils/constant");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

var _ButtonContext = _interopRequireDefault(require("./ButtonContext"));

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var Button = function Button(props) {
  var _clsx;

  var color = props.color,
      loading = props.loading,
      className = props.className,
      hairline = props.hairline,
      loadingText = props.loadingText;

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('button', prefixCls),
      bem = _createNamespace[0];

  var _useContext2 = (0, _react.useContext)(_ButtonContext.default),
      parent = _useContext2.parent;

  var size = _react.default.useMemo(function () {
    return props.size || (parent == null ? void 0 : parent.size) || 'normal';
  }, [parent == null ? void 0 : parent.size, props.size]);

  var type = _react.default.useMemo(function () {
    return props.type || (parent == null ? void 0 : parent.type) || 'default';
  }, [parent == null ? void 0 : parent.type, props.type]);

  var plain = _react.default.useMemo(function () {
    var _props$plain;

    return (_props$plain = props.plain) != null ? _props$plain : parent == null ? void 0 : parent.plain;
  }, [parent == null ? void 0 : parent.plain, props.plain]);

  var block = _react.default.useMemo(function () {
    var _props$block;

    return (_props$block = props.block) != null ? _props$block : parent == null ? void 0 : parent.block;
  }, [parent == null ? void 0 : parent.block, props.block]);

  var iconPosition = _react.default.useMemo(function () {
    return props.iconPosition || (parent == null ? void 0 : parent.iconPosition) || 'left';
  }, [parent == null ? void 0 : parent.iconPosition, props.iconPosition]);

  var disabled = _react.default.useMemo(function () {
    var _props$disabled;

    return (_props$disabled = props.disabled) != null ? _props$disabled : parent == null ? void 0 : parent.disabled;
  }, [parent == null ? void 0 : parent.disabled, props.disabled]);

  var nativeType = _react.default.useMemo(function () {
    return props.nativeType || (parent == null ? void 0 : parent.nativeType) || 'button';
  }, [parent == null ? void 0 : parent.nativeType, props.nativeType]);

  var tag = _react.default.useMemo(function () {
    return props.tag || (parent == null ? void 0 : parent.tag) || 'button';
  }, [parent == null ? void 0 : parent.tag, props.tag]);

  var TagElement = tag;
  var classes = (0, _clsx2.default)(className, bem([type, size, {
    plain: plain,
    loading: loading,
    disabled: disabled,
    hairline: hairline,
    block: block,
    round: props.round,
    square: props.square
  }]), (_clsx = {}, _clsx[_constant.BORDER_SURROUND] = hairline, _clsx), props.shadow && _constant.SHADOW + "--" + +props.shadow);

  var style = _extends({}, props.style);

  if (color) {
    style.color = plain ? color : _constant.WHITE;

    if (!plain) {
      // 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 onClick = function onClick(event) {
    if (!loading && !disabled && props.onClick) {
      props.onClick(event);
    }
  };

  var renderLoadingIcon = function renderLoadingIcon() {
    if (loading) {
      var _props$loadingSize = props.loadingSize,
          loadingSize = _props$loadingSize === void 0 ? '20px' : _props$loadingSize,
          loadingType = props.loadingType;
      return /*#__PURE__*/_react.default.createElement(_loading.default, {
        className: (0, _clsx2.default)(bem('loading')),
        size: loadingSize,
        type: loadingType,
        color: type === 'default' ? undefined : ''
      });
    }

    return null;
  };

  var renderIcon = function renderIcon() {
    if (props.loading) {
      return renderLoadingIcon();
    }

    if (props.icon) {
      return /*#__PURE__*/_react.default.cloneElement(props.icon, {
        className: (0, _clsx2.default)(bem('icon'))
      });
    }

    return null;
  };

  var renderText = function renderText() {
    var text;

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

    if (text) {
      return /*#__PURE__*/_react.default.createElement("span", {
        key: "text",
        className: (0, _clsx2.default)(bem('text'))
      }, text);
    }

    return null;
  };

  var renderContent = function renderContent() {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx2.default)(bem('content'))
    }, iconPosition === 'left' && renderIcon(), renderText(), iconPosition === 'right' && renderIcon());
  };

  return /*#__PURE__*/_react.default.createElement(TagElement, {
    className: classes,
    style: style,
    type: nativeType,
    onClick: onClick
  }, renderContent());
};

var _default = Button;
exports.default = _default;