import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["label", "isActive", "isDisabled", "href", "rel", "target", "external", "className", "css", "style", "iconType", "icon", "iconProps", "extraAction", "onClick", "color", "showToolTip", "wrapText", "buttonRef", "toolTipText", "toolTipProps"],
  _excluded2 = ["iconType", "alwaysShow", "isDisabled"],
  _excluded3 = ["className", "css"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

import React from 'react';
import classNames from 'classnames';
import { getSecureRelForTarget, useEuiMemoizedStyles, cloneElementWithCss } from '../../services';
import { validateHref } from '../../services/security/href_validator';
import { useInnerText } from '../inner_text';
import { EuiIcon } from '../icon';
import { EuiListItemLayout } from '../list_item_layout/_list_item_layout';
import { EuiListGroupItemExtraAction } from './list_group_item_extra_action';
import { euiListGroupItemStyles, euiListGroupItemTooltipStyles } from './list_group_item.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var COLORS = ['primary', 'text', 'subdued'];
export var EuiListGroupItem = function EuiListGroupItem(_ref) {
  var _toolTipProps$anchorP;
  var label = _ref.label,
    _ref$isActive = _ref.isActive,
    isActive = _ref$isActive === void 0 ? false : _ref$isActive,
    _ref$isDisabled = _ref.isDisabled,
    _isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
    href = _ref.href,
    rel = _ref.rel,
    target = _ref.target,
    external = _ref.external,
    className = _ref.className,
    customCss = _ref.css,
    style = _ref.style,
    iconType = _ref.iconType,
    icon = _ref.icon,
    iconProps = _ref.iconProps,
    extraAction = _ref.extraAction,
    onClick = _ref.onClick,
    _ref$color = _ref.color,
    color = _ref$color === void 0 ? 'text' : _ref$color,
    _ref$showToolTip = _ref.showToolTip,
    showToolTip = _ref$showToolTip === void 0 ? false : _ref$showToolTip,
    wrapText = _ref.wrapText,
    buttonRef = _ref.buttonRef,
    toolTipText = _ref.toolTipText,
    toolTipProps = _ref.toolTipProps,
    rest = _objectWithoutProperties(_ref, _excluded);
  var isHrefValid = !href || validateHref(href);
  var isDisabled = _isDisabled || !isHrefValid;
  var iconNode;
  if (iconType) {
    iconNode = ___EmotionJSX(EuiIcon, _extends({
      color: "inherit" // forces the icon to inherit its parent color
    }, iconProps, {
      type: iconType,
      className: classNames('euiListGroupItem__icon', iconProps === null || iconProps === void 0 ? void 0 : iconProps.className),
      css: iconProps === null || iconProps === void 0 ? void 0 : iconProps.css
    }));
    if (icon) {
      console.warn('Both `iconType` and `icon` were passed to EuiListGroupItem but only one can exist. The `iconType` was used.');
    }
  } else if (icon) {
    iconNode = cloneElementWithCss(icon, {
      css: iconProps === null || iconProps === void 0 ? void 0 : iconProps.css,
      className: classNames('euiListGroupItem__icon', icon.props.className)
    });
  }
  var extraActionNode;
  if (extraAction) {
    var _iconType = extraAction.iconType,
      alwaysShow = extraAction.alwaysShow,
      actionIsDisabled = extraAction.isDisabled,
      _rest = _objectWithoutProperties(extraAction, _excluded2);
    extraActionNode = ___EmotionJSX(EuiListGroupItemExtraAction, _extends({
      color: color === 'subdued' ? 'text' : color,
      iconType: _iconType,
      alwaysShow: alwaysShow
    }, _rest, {
      isDisabled: actionIsDisabled,
      parentIsDisabled: isDisabled
    }));
  }

  // Only add the label as the title attribute if it's possibly truncated
  // Also ensure the value of the title attribute is a string
  var _useInnerText = useInnerText(),
    _useInnerText2 = _slicedToArray(_useInnerText, 2),
    ref = _useInnerText2[0],
    innerText = _useInnerText2[1];
  var shouldRenderTitle = !wrapText && !showToolTip;
  var labelProps = {
    ref: shouldRenderTitle ? ref : undefined,
    className: 'euiListGroupItem__label',
    title: shouldRenderTitle ? typeof label === 'string' ? label : innerText : undefined
  };
  var styles = useEuiMemoizedStyles(euiListGroupItemStyles);
  var cssInnerStyles = [styles.euiListGroupItem__inner, !isDisabled && !isActive && styles[color]];
  var itemProps = {};
  if (href && !isDisabled) {
    itemProps = _objectSpread({
      element: 'a',
      className: 'euiListGroupItem__button',
      css: cssInnerStyles,
      href: href,
      target: target,
      rel: getSecureRelForTarget({
        href: href,
        rel: rel,
        target: target
      }),
      external: external,
      onClick: onClick
    }, rest);
  } else if (href && isDisabled || onClick) {
    itemProps = _objectSpread({
      element: 'button',
      type: 'button',
      className: 'euiListGroupItem__button',
      css: cssInnerStyles,
      onClick: onClick,
      ref: buttonRef
    }, rest);
  } else {
    itemProps = _objectSpread({
      element: 'li',
      className: 'euiListGroupItem__text',
      css: cssInnerStyles
    }, rest);
  }
  var _itemProps = itemProps,
    itemClassName = _itemProps.className,
    itemCss = _itemProps.css,
    restItemProps = _objectWithoutProperties(_itemProps, _excluded3);
  var classes = classNames('euiListGroupItem', className, itemClassName);
  var cssStyles = [styles.euiListGroupItem, itemCss, customCss];
  var tooltipStyles = euiListGroupItemTooltipStyles;
  var _tooltipProps = showToolTip ? _objectSpread(_objectSpread({
    content: toolTipText !== null && toolTipText !== void 0 ? toolTipText : label,
    position: 'right'
  }, toolTipProps), {}, {
    anchorClassName: classNames('euiListGroupItem__tooltip', toolTipProps === null || toolTipProps === void 0 ? void 0 : toolTipProps.anchorClassName),
    anchorProps: _objectSpread(_objectSpread({}, toolTipProps === null || toolTipProps === void 0 ? void 0 : toolTipProps.anchorProps), {}, {
      css: [tooltipStyles.euiListGroupItem__tooltip, toolTipProps === null || toolTipProps === void 0 || (_toolTipProps$anchorP = toolTipProps.anchorProps) === null || _toolTipProps$anchorP === void 0 ? void 0 : _toolTipProps$anchorP.css]
    })
  }) : undefined;
  return ___EmotionJSX(EuiListItemLayout, _extends({
    className: classes,
    wrapperElement: "li",
    css: cssStyles,
    style: style
  }, restItemProps, {
    tooltipProps: _tooltipProps,
    showIndicator: false,
    isSelected: isActive,
    isDisabled: isDisabled,
    prepend: iconNode,
    extraAction: extraActionNode,
    textWrap: wrapText ? 'wrap' : 'truncate',
    textProps: labelProps
  }), label);
};