import React from 'react';
import classNames from 'classnames';
import { Tooltip } from '../tooltip';
import { FontAwesomeIcon } from '../font-awesome-icon';
import { TruncateWithTooltip } from '../truncate-with-tooltip';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const DefaultLink = ({
  children,
  ...props
}) => /*#__PURE__*/_jsx("a", {
  rel: "noopener noreferrer",
  ...props,
  children: children
});
export 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__*/_jsx("span", {
            className: "icon-sidebar-text",
            children: option.title
          });
        }
        return /*#__PURE__*/_jsx(TruncateWithTooltip, {
          className: "icon-sidebar-text",
          children: /*#__PURE__*/_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__*/_jsxs(_Fragment, {
      children: [/*#__PURE__*/_jsx("div", {
        className: "icon-sidebar-button-icon-container",
        children: /*#__PURE__*/_jsx(FontAwesomeIcon, {
          icon: icon
        })
      }), getTitle(), option.divider && !isLimited && /*#__PURE__*/_jsx("div", {
        className: "icon-sidebar-divider"
      })]
    });
    const props = {
      className: classNames('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__*/_jsx(Tooltip, {
        content: option.tooltipText,
        placement: option.tooltipPlacement,
        children: /*#__PURE__*/_jsx("span", {
          children: /*#__PURE__*/_jsx(LinkComponent, {
            ...props,
            tabIndex: option.disabled ? -1 : 0,
            href: option.link,
            onClick: onClick,
            children: content
          })
        })
      });
    }
    return /*#__PURE__*/_jsx(Tooltip, {
      content: option.tooltipText,
      placement: option.tooltipPlacement,
      children: onSelect ? /*#__PURE__*/_jsx("button", {
        ...props,
        onClick: onClick,
        disabled: option.disabled,
        children: content
      }) : /*#__PURE__*/_jsx("div", {
        ...props,
        children: content
      })
    });
  };
  return IconSidebarItem;
};