"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

var _box = _interopRequireDefault(require("../box"));

var _loadingSpinner = _interopRequireDefault(require("../loadingSpinner"));

var _typography = require("../typography");

var _theme = _interopRequireDefault(require("./theme.css"));

var _excluded = ["color", "children", "className", "disabled", "element", "active", "fullWidth", "icon", "iconPlacement", "label", "level", "size", "type", "processing"];

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

var BUTTON_LEVELS;
exports.BUTTON_LEVELS = BUTTON_LEVELS;

(function (BUTTON_LEVELS) {
  BUTTON_LEVELS["outline"] = "outline";
  BUTTON_LEVELS["primary"] = "primary";
  BUTTON_LEVELS["secondary"] = "secondary";
  BUTTON_LEVELS["destructive"] = "destructive";
  BUTTON_LEVELS["link"] = "link";
  BUTTON_LEVELS["timer"] = "timer";
})(BUTTON_LEVELS || (exports.BUTTON_LEVELS = BUTTON_LEVELS = {}));

var textComponentMap = {
  tiny: _typography.UITextSmall,
  small: _typography.UITextBody,
  medium: _typography.UITextBody,
  large: _typography.UITextDisplay
};
var Button = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _cx;

  var _ref$color = _ref.color,
      color = _ref$color === void 0 ? 'teal' : _ref$color,
      children = _ref.children,
      className = _ref.className,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === void 0 ? false : _ref$disabled,
      _ref$element = _ref.element,
      element = _ref$element === void 0 ? 'button' : _ref$element,
      _ref$active = _ref.active,
      active = _ref$active === void 0 ? false : _ref$active,
      _ref$fullWidth = _ref.fullWidth,
      fullWidth = _ref$fullWidth === void 0 ? false : _ref$fullWidth,
      icon = _ref.icon,
      _ref$iconPlacement = _ref.iconPlacement,
      iconPlacement = _ref$iconPlacement === void 0 ? 'left' : _ref$iconPlacement,
      label = _ref.label,
      _ref$level = _ref.level,
      level = _ref$level === void 0 ? BUTTON_LEVELS.secondary : _ref$level,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'button' : _ref$type,
      _ref$processing = _ref.processing,
      processing = _ref$processing === void 0 ? false : _ref$processing,
      others = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var getSpinnerColor = function getSpinnerColor() {
    switch (level) {
      case BUTTON_LEVELS.secondary:
        return 'teal';

      case BUTTON_LEVELS.outline:
        return color === 'white' ? 'neutral' : color;

      case BUTTON_LEVELS.link:
        return 'aqua';

      default:
        return 'neutral';
    }
  };

  var getSpinnerTint = function getSpinnerTint() {
    switch (level) {
      case BUTTON_LEVELS.secondary:
        return 'darkest';

      case BUTTON_LEVELS.outline:
        return color === 'white' ? 'lightest' : 'darkest';

      case BUTTON_LEVELS.link:
        return 'dark';

      default:
        return 'lightest';
    }
  };

  var getSpinnerSize = function getSpinnerSize() {
    switch (size) {
      case 'tiny':
      case 'small':
        return 'small';

      default:
        return 'medium';
    }
  };

  var classNames = (0, _classnames.default)(_theme.default['reset-box-sizing'], _theme.default['reset-font-smoothing'], _theme.default['button-base'], _theme.default['button'], _theme.default[level], _theme.default[size], (_cx = {}, (0, _defineProperty2.default)(_cx, _theme.default['has-icon-only'], !children && !label || Array.isArray(children) && !children[0] && !label), (0, _defineProperty2.default)(_cx, _theme.default[color], level === BUTTON_LEVELS.outline), (0, _defineProperty2.default)(_cx, _theme.default['is-disabled'], disabled), (0, _defineProperty2.default)(_cx, _theme.default['is-full-width'], fullWidth), (0, _defineProperty2.default)(_cx, _theme.default['is-processing'], processing), (0, _defineProperty2.default)(_cx, _theme.default['is-active'], active), _cx), className);
  var Text = textComponentMap[size];
  return /*#__PURE__*/_react.default.createElement(_box.default, (0, _extends2.default)({}, others, {
    className: classNames,
    "data-teamleader-ui": "button",
    disabled: element === 'button' ? disabled : undefined,
    element: element,
    ref: ref,
    type: element === 'button' ? type : undefined
  }), icon && iconPlacement === 'left' && icon, (label || children) && /*#__PURE__*/_react.default.createElement(Text, {
    element: "span",
    maxLines: 1,
    marginLeft: icon && iconPlacement === 'left' ? 2 : undefined,
    marginRight: icon && iconPlacement === 'right' ? 2 : undefined
  }, label, children), icon && iconPlacement === 'right' && icon, processing && /*#__PURE__*/_react.default.createElement(_loadingSpinner.default, {
    className: _theme.default['spinner'],
    color: getSpinnerColor(),
    size: getSpinnerSize(),
    tint: getSpinnerTint()
  }));
});
Button.displayName = 'Button';
var _default = Button;
exports.default = _default;