/* eslint-disable react/no-array-index-key */
import PropTypes from 'prop-types';
import CustomPropTypes from "../../../utils/prop-types/index.js";
import get from 'lodash/get';
import customizable from "../../../utils/css/customizable.js";
import getMarginClasses from "../../../utils/css/getMarginClasses.js";
import { useResponsiveValues } from "../../../hooks/breakpoint/index.js";
import cx from "../../../utils/css/cx.js";
import Node from './Node';
import defaultClasses from './styles.module.scss';
const customCls = cx.merge(defaultClasses, {
  customizable: ['RenderedContent']
});

/**
 * Render rich text content from KitchenSink with the option of providing your
 * own custom React components in place of the basic HTML tags. This can be
 * useful as a way to render either small snippets of text throughout an app or
 * complete sections of prose.
 *
 * ```js
 *
 * import { RenderedContent } from '@politico/interactive-style';
 * ```
 */
const RenderedContent = function RenderedContent(props) {
  const {
    active,
    source,
    unstyled,
    components,
    margin
  } = props;
  const classes = unstyled ? {} : defaultClasses;
  const marginValue = useResponsiveValues(margin);
  const margins = getMarginClasses(marginValue, {
    defaultSide: 'y',
    sides: ['y', 't', 'b']
  });
  if (!source) {
    return null;
  }
  const nodes = get(source, 'document.nodes');
  if (!nodes) {
    return null;
  }
  return <div className={customCls(customizable('RenderedContent'), {
    container: !unstyled
  }, margins)}>
      {nodes.map((node, i) => <Node key={i} data={node} context={{
      active,
      classes,
      components
    }} />)}
    </div>;
};
RenderedContent.defaultProps = {
  active: true,
  unstyled: false,
  source: {},
  components: {},
  margin: undefined
};
RenderedContent.propTypes = {
  /**
   * The content to render
   */
  source: PropTypes.shape({}),
  /**
   * Deactivate to unload lazy-loaded components
   */
  active: PropTypes.bool,
  /**
   * Activate lazy-loaded components
   */
  unstyled: PropTypes.bool,
  /**
   * A set of custom components to use to render various pieces of rich
   * content.
   */
  components: PropTypes.shape({
    paragraph: PropTypes.elementType,
    blockquote: PropTypes.elementType,
    'numbered-list': PropTypes.elementType,
    'bulleted-list': PropTypes.elementType,
    'list-item': PropTypes.elementType,
    image: PropTypes.elementType,
    link: PropTypes.elementType,
    strong: PropTypes.elementType,
    emphasis: PropTypes.elementType,
    underlined: PropTypes.elementType,
    small: PropTypes.elementType,
    tweet: PropTypes.elementType,
    iframe: PropTypes.elementType,
    code: PropTypes.elementType,
    youtube: PropTypes.elementType
  }),
  /**
   * The vertical margins of the RenderedContent. Margins can be specified as one
   * spacing value that gets used on both top and bottom, an explicit `y`
   * value, or completely separate `tb` values. The values correspond to levels
   * of our spacing scale (e.g., `"4"` corresponds to `$spacing-4`). Note that
   * `RenderedContent` does not support custom horizontal margins.
   */
  margin: CustomPropTypes.responsive(PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
    y: PropTypes.string
  }), PropTypes.shape({
    t: PropTypes.string,
    b: PropTypes.string
  })]))
};
export default RenderedContent;