// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { isValidElement, cloneElement, Children } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Heading from '../Heading';
import Icon, { ICON_SIZE_12, ICON_SIZE_16, ICON_SIZE_24, ICON_SIZE_32, ICON_SIZE_48, ICON_SIZE_64, ICON_SIZE_96, ICON_SIZE_128, ICON_SIZE_192 } from '../Icon';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `Media` component is a combination of icon, text title and content.
 * This layout was so widely spread in our products so we have decided to create a special component for it.
 * Probably we should have named the component differently (not Media), but we didn't have better idea then ;)
 * @since 0.0.45
 */
const Media = _ref => {
  let {
    className,
    children,
    title,
    titleAddon,
    titleSize,
    image,
    imageSpacing,
    imageAlign,
    align,
    vertical = false,
    baseClassName = `${CLS_PREFIX}media`,
    ...props
  } = _ref;
  if (!imageSpacing && /*#__PURE__*/isValidElement(image) && image.type === Icon) {
    const map = {
      [ICON_SIZE_12]: 'sm',
      [ICON_SIZE_16]: 'sm',
      [ICON_SIZE_24]: 'md',
      [ICON_SIZE_32]: 'md',
      [ICON_SIZE_48]: 'lg',
      [ICON_SIZE_64]: 'xl',
      [ICON_SIZE_96]: 'xxl',
      [ICON_SIZE_128]: 'xxl',
      [ICON_SIZE_192]: 'xxl'
    };
    imageSpacing = map[image.props.size];
  }
  const renderTitle = title => {
    if (!title) {
      return null;
    }
    if (typeof title === 'string') {
      return /*#__PURE__*/_jsx(Heading, {
        level: 5,
        className: `${baseClassName}__title`,
        children: title
      });
    }
    if ( /*#__PURE__*/isValidElement(title)) {
      return /*#__PURE__*/cloneElement(title, {
        className: classNames(`${baseClassName}__title`, title.props.className)
      });
    }
    return null;
  };
  return /*#__PURE__*/_jsxs("div", {
    className: classNames(baseClassName, {
      [`${baseClassName}--${align}`]: align,
      [`${baseClassName}--vertical`]: vertical
    }, className),
    ...props,
    children: [image && /*#__PURE__*/_jsx("figure", {
      className: classNames(`${baseClassName}__figure`, {
        [`${baseClassName}__figure--${imageSpacing}`]: imageSpacing,
        [`${baseClassName}__figure--${imageAlign}`]: imageAlign
      }),
      children: image
    }), /*#__PURE__*/_jsxs("div", {
      className: `${baseClassName}__body`,
      children: [title || titleAddon ? /*#__PURE__*/_jsxs("div", {
        className: classNames(`${baseClassName}__header`, {
          [`${baseClassName}__header--${titleSize}`]: titleSize
        }),
        children: [renderTitle(title), titleAddon && /*#__PURE__*/_jsx("div", {
          className: `${baseClassName}__title-addon`,
          children: titleAddon
        })]
      }) : null, Children.toArray(children).length ? /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__content`,
        children: children
      }) : null]
    })]
  });
};
export default Media;