"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ButtonContent = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _framerMotion = require("framer-motion");
var _ = require("../../../..");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 LOADING_ANIMATION_DURATION = 0.2;
var getSpinnerSize = function getSpinnerSize(size) {
  switch (size) {
    case 's':
      return 3;
    case 'm':
      return 5;
    default:
      return 6;
  }
};
var SpinnerAnimation = (0, _styledComponents.default)(_framerMotion.motion.span).withConfig({
  displayName: "ButtonContent__SpinnerAnimation",
  componentId: "sc-1oio540-0"
})(["position:absolute;left:0;top:0;height:100%;width:100%;display:flex;align-items:center;justify-content:center;"]);
var IconWrapper = _styledComponents.default.div.withConfig({
  displayName: "ButtonContent__IconWrapper",
  componentId: "sc-1oio540-1"
})(["align-self:center;"]);
var ButtonContent = exports.ButtonContent = function ButtonContent(props) {
  var children = props.children,
    colorFn = props.colorFn,
    icon = props.icon,
    iconPlacement = props.iconPlacement,
    loading = props.loading,
    variant = props.variant,
    size = props.size,
    _props$delayLoadingSp = props.delayLoadingSpinnerAnimation,
    delayLoadingSpinnerAnimation = _props$delayLoadingSp === void 0 ? true : _props$delayLoadingSp;
  var theme = (0, _styledComponents.useTheme)();
  var type;
  switch (size) {
    case 's':
      type = 'tertiary';
      break;
    case 'm':
      type = 'secondary';
      break;
    default:
      type = 'primary';
      break;
  }
  var content = _react.default.createElement(_react.default.Fragment, null, icon ? _react.default.createElement(_.Flexbox, {
    container: true,
    gap: variant === 'neutral' && size === 's' ? 1 : 2,
    justifyContent: "space-between",
    alignItems: "baseline"
  }, iconPlacement === 'left' && _react.default.createElement(IconWrapper, null, icon), _react.default.createElement(_.Flexbox, {
    item: true
  }, _react.default.createElement(_.Typography, {
    type: type,
    color: "inherit",
    weight: "bold",
    as: "div"
  }, children)), iconPlacement === 'right' && _react.default.createElement(IconWrapper, null, icon)) : _react.default.createElement(_.Typography, _extends({
    type: type,
    color: "inherit",
    weight: "bold"
  }, size === 's' ? {
    as: 'div'
  } : {}), children));
  if (variant === 'neutral') {
    return content;
  }
  return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_framerMotion.motion.span, {
    initial: {
      opacity: 1
    },
    animate: {
      opacity: loading ? 0 : 1
    },
    transition: {
      duration: LOADING_ANIMATION_DURATION
    }
  }, content), _react.default.createElement(_framerMotion.AnimatePresence, null, loading && _react.default.createElement(SpinnerAnimation, {
    initial: {
      opacity: 0
    },
    animate: {
      opacity: 1
    },
    exit: {
      opacity: 0
    },
    transition: {
      duration: LOADING_ANIMATION_DURATION
    }
  }, _react.default.createElement(_.Spinner, {
    id: "spinner-".concat(variant, "-").concat(size, "-").concat(colorFn && colorFn(theme)),
    color: variant === 'primary' ? function (t) {
      return t.color.buttonSpinner;
    } : colorFn || function (t) {
      return t.color.buttonSpinnerSecondary;
    },
    size: getSpinnerSize(size),
    delay: delayLoadingSpinnerAnimation
  }))));
};