"use strict";var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;exports.__esModule = true;exports.getButtonIconSizes = exports.ButtonIcon = void 0;var _react = _interopRequireWildcard(require("react"));


var _utils = require("../../lib/utils");
var _Emotion = require("../../lib/theming/Emotion");
var _ThemeContext = require("../../lib/theming/ThemeContext");



var _ButtonIcon = require("./ButtonIcon.styles");
var _LoadingButtonIcon = require("./LoadingButtonIcon");







var getButtonIconSizes = exports.getButtonIconSizes = function getButtonIconSizes(theme) {
  return {
    small: parseInt(theme.btnIconSizeSmall),
    medium: parseInt(theme.btnIconSizeMedium),
    large: parseInt(theme.btnIconSizeLarge)
  };
};

var useIcon = function useIcon(icon, size) {
  var theme = (0, _react.useContext)(_ThemeContext.ThemeContext);
  if (icon && (0, _utils.isKonturIcon)(icon)) {var _icon$props$size;
    var sizes = getButtonIconSizes(theme);
    return /*#__PURE__*/_react.default.cloneElement(icon, { size: (_icon$props$size = icon.props.size) != null ? _icon$props$size : sizes[size] });
  }

  return icon;
};

var ButtonIcon = exports.ButtonIcon = function ButtonIcon(_ref)







{var _cx;var icon = _ref.icon,use = _ref.use,position = _ref.position,hasChildren = _ref.hasChildren,_ref$loading = _ref.loading,loading = _ref$loading === void 0 ? false : _ref$loading,_ref$hasBothIcons = _ref.hasBothIcons,hasBothIcons = _ref$hasBothIcons === void 0 ? false : _ref$hasBothIcons,_ref$size = _ref.size,size = _ref$size === void 0 ? 'small' : _ref$size;
  var theme = (0, _react.useContext)(_ThemeContext.ThemeContext);
  var isLink = use === 'link';

  var getSizeIconClassName = function getSizeIconClassName() {
    switch (size) {
      case 'large':
        return [
        _ButtonIcon.styles.iconLarge(theme),
        position === 'left' ? _ButtonIcon.styles.iconLargeLeft(theme) : _ButtonIcon.styles.iconLargeRight(theme)];

      case 'medium':
        return [
        _ButtonIcon.styles.iconMedium(theme),
        position === 'left' ? _ButtonIcon.styles.iconMediumLeft(theme) : _ButtonIcon.styles.iconMediumRight(theme)];

      case 'small':
      default:
        return [
        _ButtonIcon.styles.iconSmall(theme),
        position === 'left' ? _ButtonIcon.styles.iconSmallLeft(theme) : _ButtonIcon.styles.iconSmallRight(theme)];

    }
  };

  var style = {
    display: 'inline-flex',
    alignItems: 'center'
  };

  var _icon = useIcon(icon, size);

  return /*#__PURE__*/(
    _react.default.createElement("span", {
      style: style,
      className: (0, _Emotion.cx)(_ButtonIcon.styles.icon(), getSizeIconClassName(), (_cx = {}, _cx[
      _ButtonIcon.styles.iconNoMargin()] = !hasChildren, _cx[
      _ButtonIcon.styles.iconLeftLink(theme)] = isLink && position === 'left', _cx[
      _ButtonIcon.styles.iconRightLink(theme)] = isLink && position === 'right', _cx)
      ) },

    loading && !hasBothIcons ? /*#__PURE__*/_react.default.createElement(_LoadingButtonIcon.LoadingButtonIcon, { isCentered: false, size: size }) : _icon
    ));

};