import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { isValidElement, cloneElement, Children } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
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 Heading from '../Heading';

/**
 * `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 = ({
  baseClassName,
  className,
  children,
  title,
  titleAddon,
  titleSize,
  image,
  imageSpacing,
  imageAlign,
  align,
  vertical,
  ...props
}) => {
  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__*/React.createElement(Heading, {
        level: 5,
        className: "".concat(baseClassName, "__title")
      }, title);
    }

    if ( /*#__PURE__*/isValidElement(title)) {
      return /*#__PURE__*/cloneElement(title, {
        className: classNames("".concat(baseClassName, "__title"), title.props.className)
      });
    }

    return null;
  };

  return /*#__PURE__*/React.createElement("div", _extends({
    className: classNames(baseClassName, {
      ["".concat(baseClassName, "--").concat(align)]: align,
      ["".concat(baseClassName, "--vertical")]: vertical
    }, className)
  }, props), image && /*#__PURE__*/React.createElement("figure", {
    className: classNames("".concat(baseClassName, "__figure"), {
      ["".concat(baseClassName, "__figure--").concat(imageSpacing)]: imageSpacing,
      ["".concat(baseClassName, "__figure--").concat(imageAlign)]: imageAlign
    })
  }, image), /*#__PURE__*/React.createElement("div", {
    className: "".concat(baseClassName, "__body")
  }, title || titleAddon ? /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(baseClassName, "__header"), {
      ["".concat(baseClassName, "__header--").concat(titleSize)]: titleSize
    })
  }, renderTitle(title), titleAddon && /*#__PURE__*/React.createElement("div", {
    className: "".concat(baseClassName, "__title-addon")
  }, titleAddon)) : null, Children.toArray(children).length ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(baseClassName, "__content")
  }, children) : null));
};

Media.propTypes = {
  /**
   * Title
   * @since 0.0.45
   */
  title: PropTypes.node,

  /**
   * Title addon
   * @since 0.0.68
   */
  titleAddon: PropTypes.node,

  /**
   * Title size
   * @deprecated Pass a component as a title prop
   * @since 0.0.68
   */
  titleSize: PropTypes.oneOf(['md', 'lg', 'xl']),

  /**
   * Image
   * @since 0.0.45
   */
  image: PropTypes.any,

  /**
   * Spacings between image and content
   * @since 0.3.0
   */
  imageSpacing: PropTypes.oneOf(['sm', 'md', 'lg', 'xl', 'xxl']),

  /**
   * Media object and content alignment
   * @since 3.19.0
   */
  align: PropTypes.oneOf(['start', 'center', 'end']),

  /**
   * Media object (image) alignment
   * @since 3.19.0
   */
  imageAlign: PropTypes.oneOf(['start', 'center', 'end']),

  /**
   * Media vertical direction
   * @since 3.19.0
   */
  vertical: PropTypes.bool,

  /**
   * Content of the `Media`.
   * @since 0.0.45
   */
  children: PropTypes.node,

  /**
   * @ignore
   */
  className: PropTypes.string,

  /**
   * @ignore
   */
  baseClassName: PropTypes.string
};
Media.defaultProps = {
  title: null,
  titleAddon: null,
  titleSize: null,
  image: null,
  imageSpacing: null,
  align: null,
  imageAlign: null,
  vertical: false,
  children: null,
  className: null,
  baseClassName: "".concat(CLS_PREFIX, "media")
};
export default Media;