"use strict";

exports.__esModule = true;
exports.default = void 0;
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _link = _interopRequireDefault(require("../link"));
var _icon = _interopRequireWildcard(require("../icon"));
var _buttonIcons = require("../../util/button-icons");
var _tooltip = _interopRequireDefault(require("../tooltip"));
var _types = _interopRequireDefault(require("./types"));
var _style = _interopRequireDefault(require("./style.css"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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 && {}.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(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const getButtonContent = (icon, content, hovered, hoverBackgroundColor, hoverColor) => {
  const {
    type,
    faIcon,
    position
  } = icon || {
    type: '',
    position: ''
  };
  const Icon = type && _buttonIcons.ICONS[type];
  if (!Icon && !faIcon) {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.buttonContent
    }, /*#__PURE__*/_react.default.createElement("span", {
      className: _style.default.label
    }, content));
  }
  const iconComponent = faIcon ? /*#__PURE__*/_react.default.createElement(_icon.default, {
    iconName: faIcon.name,
    iconColor: hovered && hoverColor ? hoverColor : faIcon.color ?? _icon.DEFAULT_ICON_COLOR,
    // eslint-disable-next-line no-nested-ternary
    backgroundColor: !faIcon?.backgroundColor ? null : hovered && hoverBackgroundColor ? hoverBackgroundColor : faIcon.backgroundColor,
    size: {
      faSize: faIcon.size,
      wrapperSize: faIcon.size
    },
    customStyle: faIcon.customStyle
  }) : /*#__PURE__*/_react.default.createElement(Icon, {
    className: _style.default.icon,
    theme: "coorpmanager"
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.buttonContent
  }, position === 'left' ? iconComponent : null, content ? /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.label
  }, content) : null, position === 'right' ? iconComponent : null);
};
const ButtonLink = props => {
  const {
    type,
    usage = 'button',
    label,
    content,
    hoverBackgroundColor,
    hoverColor,
    disabled = false,
    icon,
    'data-name': dataName,
    'data-testid': dataTestId = 'button-link',
    'aria-label': ariaLabel,
    tooltipText,
    tooltipPlacement = 'left',
    link,
    onClick = _noop2.default,
    onKeyDown = _noop2.default,
    className,
    customStyle,
    useTitle = true
  } = props;
  const styleButton = (0, _classnames.default)(link && _style.default.link, className, _style.default.button, !label && _style.default.iconButton, type === 'primary' && _style.default.primary, type === 'secondary' && _style.default.secondary, type === 'tertiary' && _style.default.tertiary, type === 'text' && _style.default.text, type === 'dangerous' && _style.default.dangerous, disabled && _style.default.disabled);
  const [hovered, setHovered] = (0, _react.useState)(false);
  const handleOnClick = (0, _react.useCallback)(event => onClick(event), [onClick]);
  const handleOnKeyDown = (0, _react.useCallback)(event => onKeyDown(event), [onKeyDown]);
  const handleMouseOver = (0, _react.useCallback)(() => {
    setHovered(true);
  }, [setHovered]);
  const handleMouseLeave = (0, _react.useCallback)(() => setHovered(false), [setHovered]);
  const TooltipContent = (0, _react.useCallback)(() => /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.tooltipContentWrapper
  }, tooltipText || ariaLabel), [tooltipText, ariaLabel]);
  const renderToolTip = () => {
    const closeToolTipInformationTextAriaLabel = tooltipText || ariaLabel;
    if (!closeToolTipInformationTextAriaLabel) return null;
    return /*#__PURE__*/_react.default.createElement(_tooltip.default, {
      fontSize: 12,
      anchorId: "button-icon",
      toolTipIsVisible: hovered,
      placement: tooltipPlacement,
      TooltipContent: TooltipContent,
      closeToolTipInformationTextAriaLabel: closeToolTipInformationTextAriaLabel
    });
  };
  const _customStyle = (0, _react.useMemo)(() => {
    return {
      ...customStyle,
      ...((hoverBackgroundColor || hoverColor) && hovered ? {
        backgroundColor: hoverBackgroundColor,
        color: hoverColor
      } : null)
    };
  }, [hoverBackgroundColor, hoverColor, hovered, customStyle]);
  if (link) {
    return /*#__PURE__*/_react.default.createElement(_link.default, _extends({}, link, useTitle && {
      title: ariaLabel || label
    }, {
      className: styleButton,
      style: customStyle,
      "data-name": dataName,
      "data-testid": dataTestId,
      "aria-label": ariaLabel || label,
      hoverColor: hoverColor,
      hoverBackgroundColor: hoverBackgroundColor,
      onMouseEnter: handleMouseOver,
      onMouseLeave: handleMouseLeave
    }), getButtonContent(icon, content ?? label, hovered, hoverBackgroundColor, hoverColor), renderToolTip());
  }
  return /*#__PURE__*/_react.default.createElement("button", _extends({}, useTitle && {
    title: ariaLabel || label
  }, ariaLabel && !label ? {
    'data-for': 'button-icon',
    'data-tip': hovered
  } : {}, {
    // eslint-disable-next-line react/button-has-type
    type: usage,
    "aria-label": ariaLabel || label,
    "data-name": dataName,
    "data-testid": dataTestId,
    style: _customStyle,
    className: styleButton,
    onClick: handleOnClick,
    onKeyDown: handleOnKeyDown,
    onMouseOver: handleMouseOver,
    onMouseLeave: handleMouseLeave,
    tabIndex: 0,
    disabled: disabled
  }), getButtonContent(icon, content ?? label, hovered, hoverBackgroundColor, hoverColor), renderToolTip());
};
ButtonLink.propTypes = process.env.NODE_ENV !== "production" ? _types.default : {};
var _default = exports.default = ButtonLink;
//# sourceMappingURL=index.js.map