"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _Icon = _interopRequireWildcard(require("../Icon"));
var _Media = _interopRequireDefault(require("../Media"));
var _Heading = _interopRequireDefault(require("../Heading"));
var _Tooltip = _interopRequireDefault(require("../Tooltip"));
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const VIEW_SIMPLE = 'simple';
const VIEW_CARD = 'card';

/**
 * `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
 */
class Item extends _react.Component {
  renderTitle(title) {
    if (!title) {
      return null;
    }
    const {
      baseClassName,
      view
    } = this.props;
    let wrappable = false;
    if (typeof title === 'string') {
      title = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: title
      });
      wrappable = true;
    } else if (typeof title === 'object' && ! /*#__PURE__*/(0, _react.isValidElement)(title)) {
      title = /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
        ...title
      });
      wrappable = true;
    }
    if (wrappable && view === 'card') {
      title = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Heading.default, {
        level: 3,
        children: title
      });
    }
    if ( /*#__PURE__*/(0, _react.isValidElement)(title)) {
      title = /*#__PURE__*/(0, _react.cloneElement)(title, {
        className: (0, _classnames.default)(`${baseClassName}__title`, title.props.className)
      });
    }
    return title;
  }
  renderIcon(icon) {
    if (!icon) {
      return null;
    }
    if (typeof icon === 'string') {
      return _Icon.default.create({}, {
        size: _Icon.ICON_SIZE_32,
        [_Icon.NAMES.indexOf(icon) === -1 ? 'src' : 'name']: icon
      });
    }
    return _Icon.default.create(icon, {
      size: _Icon.ICON_SIZE_32
    });
  }
  render() {
    const {
      children,
      baseClassName,
      component: Tag,
      view,
      className,
      icon,
      title,
      description,
      footer,
      value,
      tooltip,
      disabled,
      hoverable,
      ...props
    } = this.props;
    if (props.onClick) {
      if (disabled) {
        props.onClick = e => e.preventDefault();
      } else {
        props.tabIndex = 0;
        props.onKeyDown = (0, _utils.wrapFunction)(props.onKeyDown, e => {
          if (e.key === 'Enter') {
            e.preventDefault();
            e.currentTarget.click();
          }
        });
      }
    }
    const item = /*#__PURE__*/(0, _jsxRuntime.jsxs)(Tag, {
      className: (0, _classnames.default)(baseClassName, {
        [`${baseClassName}--card`]: view === 'card',
        [`${baseClassName}--disabled`]: disabled,
        [`${baseClassName}--hoverable`]: hoverable
      }, className),
      ...props,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_Media.default, {
        image: this.renderIcon(icon),
        title: this.renderTitle(title),
        className: `${baseClassName}__media`,
        children: [description && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${baseClassName}__description`,
          children: description
        }), _react.Children.toArray(children).length ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${baseClassName}__content`,
          children: children
        }) : null]
      }), footer && /*#__PURE__*/(0, _jsxRuntime.jsx)("footer", {
        className: `${baseClassName}__footer`,
        children: footer
      })]
    });
    if (tooltip) {
      return (0, _utils.createProxyProps)(props => /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip.default, {
        title: tooltip,
        children: /*#__PURE__*/(0, _react.cloneElement)(item, props)
      }));
    }
    return item;
  }
}
Item.propTypes = {
  /**
   * Component to render as the root element. Useful when rendering a `Item` as `<a>` or `<Link>`.
   * @since 0.0.48
   */
  component: _propTypes.default.elementType,
  /**
   * View mode.
   * @since 2.1.0
   */
  view: _propTypes.default.oneOf([VIEW_SIMPLE, VIEW_CARD]),
  /**
   * Icon.
   * @since 0.0.48
   */
  icon: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object, _propTypes.default.element]),
  /**
   * Title.
   * @since 0.0.48
   */
  title: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object, _propTypes.default.element]),
  /**
   * Content for footer.
   * @since 2.1.0
   */
  footer: _propTypes.default.node,
  /**
   * Description. Renders as small grey text.
   * @since 2.1.0
   */
  description: _propTypes.default.node,
  /**
   * Content of the `Item`.
   * @since 0.0.48
   */
  children: _propTypes.default.node,
  /**
   * An unique value. It needs only in ItemList with selectable view.
   * @since 2.3.0
   */
  value: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
  /**
   * Tooltip for component.
   * @since 2.5.1
   */
  tooltip: _propTypes.default.node,
  /**
   * @ignore
   */
  className: _propTypes.default.string,
  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string,
  /**
   * Whether disabled or not.
   * @since 3.15.0
   */
  disabled: _propTypes.default.bool,
  /**
   * Add hover shadow and pointer cursor.
   * @since 3.26.0
   */
  hoverable: _propTypes.default.bool
};
Item.defaultProps = {
  component: 'div',
  view: VIEW_SIMPLE,
  hoverable: undefined,
  title: undefined,
  description: undefined,
  icon: undefined,
  footer: undefined,
  children: undefined,
  value: undefined,
  tooltip: undefined,
  className: undefined,
  disabled: undefined,
  baseClassName: `${_constants.CLS_PREFIX}item`
};
var _default = exports.default = Item;