"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.withIconSidebarItem = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _tooltip = require("../tooltip");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _truncateWithTooltip = require("../truncate-with-tooltip");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const DefaultLink = ({
  children,
  ...props
}) => /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
  rel: "noopener noreferrer",
  ...props,
  children: children
});
const withIconSidebarItem = (LinkComponent = DefaultLink) => {
  const IconSidebarItem = ({
    option,
    selected,
    isLimited,
    onSelect
  }) => {
    const isSelected = option.key === selected;
    const icon = isSelected && option.selectedIcon ? option.selectedIcon : option.icon;
    const getTitle = () => {
      if (option.title) {
        if (option.tooltipText) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            className: "icon-sidebar-text",
            children: option.title
          });
        }
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
          className: "icon-sidebar-text",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            children: option.title
          })
        });
      }
    };
    const onClick = e => {
      if (option.disabled) {
        e.preventDefault();
        e.stopPropagation();
        return;
      }

      // Prevent calling onSelect when link is opened in a new tab
      if (e.metaKey || e.ctrlKey) {
        return;
      }
      onSelect?.(option);
    };
    const content = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "icon-sidebar-button-icon-container",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
          icon: icon
        })
      }), getTitle(), option.divider && !isLimited && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "icon-sidebar-divider"
      })]
    });
    const props = {
      className: (0, _classnames.default)('icon-sidebar-button-container', option.className, option.disabled && 'disabled', isSelected && 'selected'),
      'test-id': option.testId,
      'aria-label': option.title && typeof option.title === 'string' ? option.title : option.ariaLabel
    };
    if (option.link) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        content: option.tooltipText,
        placement: option.tooltipPlacement,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(LinkComponent, {
            ...props,
            tabIndex: option.disabled ? -1 : 0,
            href: option.link,
            onClick: onClick,
            children: content
          })
        })
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
      content: option.tooltipText,
      placement: option.tooltipPlacement,
      children: onSelect ? /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        ...props,
        onClick: onClick,
        disabled: option.disabled,
        children: content
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        ...props,
        children: content
      })
    });
  };
  return IconSidebarItem;
};
exports.withIconSidebarItem = withIconSidebarItem;