import { Fragment } from 'react';
import PropTypes from 'prop-types';
import CustomPropTypes from "../../../utils/prop-types/index.js";
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 useRenderedText from './hooks/useRenderedText';
import { MarkdownContext } from './context';
import defaultClasses from './styles.module.scss';
const customCls = cx.merge(defaultClasses, {
  customizable: ['MarkdownContent']
});

/**
 * Render a string of markdown as HTML 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 { MarkdownContent } from '@politico/interactive-style';
 * ```
 */
const MarkdownContent = function MarkdownContent(props) {
  const {
    source,
    components,
    unstyled,
    inline,
    linksExternal,
    tracking,
    margin
  } = props;
  const content = useRenderedText(source);
  const classes = unstyled ? {} : defaultClasses;
  const marginValue = useResponsiveValues(margin);
  const margins = getMarginClasses(marginValue, {
    defaultSide: 'y',
    sides: ['y', 't', 'b']
  });
  const Container = inline ? Fragment : 'div';
  const defaultContainerProps = {
    className: customCls(customizable('MarkdownContent'), classes.container, margins)
  };
  const containerProps = inline ? {} : defaultContainerProps;
  return <MarkdownContext.Provider value={{
    components,
    classes,
    inline,
    linksExternal,
    tracking
  }}>
      <Container {...containerProps}>
        {content}
      </Container>
    </MarkdownContext.Provider>;
};
MarkdownContent.defaultProps = {
  source: '',
  components: {},
  unstyled: false,
  inline: false,
  linksExternal: true,
  tracking: {},
  margin: undefined
};
MarkdownContent.propTypes = {
  /**
   * A string of markdown content to render.
   */
  source: PropTypes.string,
  /**
   * A set of custom components to use to render various pieces of markdown
   * content.
   */
  components: PropTypes.shape({
    'heading-one': PropTypes.elementType,
    'heading-two': PropTypes.elementType,
    'heading-three': PropTypes.elementType,
    'heading-four': PropTypes.elementType,
    'heading-five': PropTypes.elementType,
    'heading-six': PropTypes.elementType,
    paragraph: PropTypes.elementType,
    blockquote: PropTypes.elementType,
    'numbered-list': PropTypes.elementType,
    'bulleted-list': PropTypes.elementType,
    'list-item': PropTypes.elementType,
    rule: PropTypes.elementType,
    image: PropTypes.elementType,
    link: PropTypes.elementType,
    strong: PropTypes.elementType,
    emphasis: PropTypes.elementType,
    code: PropTypes.elementType
  }),
  /**
   * Set to `true` to disable all of the default styling.
   */
  unstyled: PropTypes.bool,
  /**
   * Set to `true` to render inline-only markdown
   */
  inline: PropTypes.bool,
  /**
   * Set to `false` to have links open in the same window
   */
  linksExternal: PropTypes.bool,
  /**
   * Metadata for tracking
   */
  tracking: PropTypes.shape({}),
  /**
   * The vertical margins of the MarkdownContent. 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
   * `MarkdownContent` 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 MarkdownContent;