// Copyright 1999-2025. WebPros International GmbH. All rights reserved.

import { cloneElement, isValidElement, Children } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Icon, { NAMES, ICON_SIZE_32 } from '../Icon';
import Media from '../Media';
import Heading from '../Heading';
import Tooltip from '../Tooltip';
import { createProxyProps, wrapFunction } from '../utils';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `Item` component is element of [ItemList](#!/ItemList) and it is presented as
 * a combination of icon, text title and content (similar to [Media](#!/Media)).
 * @since 0.0.48
 */
const Item = _ref => {
  let {
    children,
    baseClassName = `${CLS_PREFIX}item`,
    component,
    view = 'simple',
    className,
    icon,
    title,
    description,
    footer,
    value,
    tooltip,
    disabled,
    hoverable,
    ...props
  } = _ref;
  const Tag = component || 'div';
  const renderTitle = () => {
    if (!title) {
      return null;
    }
    let wrappable = false;
    if (typeof title === 'string') {
      title = /*#__PURE__*/_jsx("span", {
        children: title
      });
      wrappable = true;
    } else if (typeof title === 'object' && ! /*#__PURE__*/isValidElement(title)) {
      title = /*#__PURE__*/_jsx("a", {
        ...title
      });
      wrappable = true;
    }
    if (wrappable && view === 'card') {
      title = /*#__PURE__*/_jsx(Heading, {
        level: 3,
        children: title
      });
    }
    if (/*#__PURE__*/isValidElement(title)) {
      title = /*#__PURE__*/cloneElement(title, {
        className: classNames(`${baseClassName}__title`, title.props.className)
      });
    }
    return title;
  };
  const renderIcon = () => {
    if (!icon) {
      return null;
    }
    if (typeof icon === 'string') {
      return Icon.create({}, {
        size: ICON_SIZE_32,
        [NAMES.indexOf(icon) === -1 ? 'src' : 'name']: icon
      });
    }
    return Icon.create(icon, {
      size: ICON_SIZE_32
    });
  };
  if (props.onClick) {
    if (disabled) {
      props.onClick = e => e.preventDefault();
    } else {
      props.tabIndex = 0;
      props.onKeyDown = wrapFunction(props.onKeyDown, e => {
        if (e.key === 'Enter') {
          e.preventDefault();
          e.currentTarget.click();
        }
      });
    }
  }
  const item = /*#__PURE__*/_jsxs(Tag, {
    className: classNames(baseClassName, {
      [`${baseClassName}--card`]: view === 'card',
      [`${baseClassName}--disabled`]: disabled,
      [`${baseClassName}--hoverable`]: hoverable
    }, className),
    ...props,
    children: [/*#__PURE__*/_jsxs(Media, {
      image: renderIcon(),
      title: renderTitle(),
      className: `${baseClassName}__media`,
      children: [description && /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__description`,
        children: description
      }), Children.toArray(children).length ? /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__content`,
        children: children
      }) : null]
    }), footer && /*#__PURE__*/_jsx("footer", {
      className: `${baseClassName}__footer`,
      children: footer
    })]
  });
  if (tooltip) {
    return createProxyProps(props => /*#__PURE__*/_jsx(Tooltip, {
      title: tooltip,
      children: /*#__PURE__*/cloneElement(item, props)
    }));
  }
  return item;
};
export default Item;