"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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 _Heading = _interopRequireDefault(require("../Heading"));

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

/**
 * `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__*/(0, _react.isValidElement)(image) && image.type === _Icon.default) {
    const map = {
      [_Icon.ICON_SIZE_12]: 'sm',
      [_Icon.ICON_SIZE_16]: 'sm',
      [_Icon.ICON_SIZE_24]: 'md',
      [_Icon.ICON_SIZE_32]: 'md',
      [_Icon.ICON_SIZE_48]: 'lg',
      [_Icon.ICON_SIZE_64]: 'xl',
      [_Icon.ICON_SIZE_96]: 'xxl',
      [_Icon.ICON_SIZE_128]: 'xxl',
      [_Icon.ICON_SIZE_192]: 'xxl'
    };
    imageSpacing = map[image.props.size];
  }

  const renderTitle = title => {
    if (!title) {
      return null;
    }

    if (typeof title === 'string') {
      return /*#__PURE__*/_react.default.createElement(_Heading.default, {
        level: 5,
        className: "".concat(baseClassName, "__title")
      }, title);
    }

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

    return null;
  };

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

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

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

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

  /**
   * Image
   * @since 0.0.45
   */
  image: _propTypes.default.any,

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

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

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

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

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

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.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(_constants.CLS_PREFIX, "media")
};
var _default = Media;
exports.default = _default;