function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

/*
Copyright (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import * as ReactIs from 'react-is';
import { getOverrides } from '../helpers/overrides';
import { StyledHeadingRoot, StyledHeadingContent, StyledHeadingContentRow, StyledHeadingEndEnhancerContainer, StyledHeadingEndEnhancerDescriptionContainer, StyledHeadingMainHeading, StyledHeadingSubHeading } from './styled-components';

function RenderNode(props) {
  const {
    component,
    ...restProps
  } = props;
  const Component = component;

  if (!Component) {
    return null;
  }

  if (typeof Component === 'string') {
    return Component;
  }

  if (ReactIs.isValidElementType(Component)) {
    return /*#__PURE__*/React.createElement(Component, restProps);
  }

  return Component;
}

function isMaxLinesValid(maxLines) {
  return maxLines === 1 || maxLines === 2;
}

const ListHeading = /*#__PURE__*/React.forwardRef((props, ref) => {
  const {
    overrides = {},
    maxLines
  } = props;
  const EndEnhancer = props.endEnhancer;
  const EndEnhancerDescription = props.endEnhancerDescription;
  const SubHeading = props.subHeading;
  const [Root, rootProps] = getOverrides(overrides.Root, StyledHeadingRoot);
  const [Content, contentProps] = getOverrides(overrides.Content, StyledHeadingContent);
  const [HeadingContainer, headingContainerProps] = getOverrides(overrides.HeadingContainer, StyledHeadingMainHeading);
  const [SubHeadingContainer, subHeadingContainerProps] = getOverrides(overrides.SubHeadingContainer, StyledHeadingSubHeading);
  const [EndEnhancerContainer, endEnhancerContainerProps] = getOverrides(overrides.EndEnhancerContainer, StyledHeadingEndEnhancerContainer);
  const [EndEnhancerDescriptionContainer, endEnhancerDescriptionContainerProps] = getOverrides(overrides.EndEnhancerDescriptionContainer, StyledHeadingEndEnhancerDescriptionContainer);
  const isEndEnhancerString = typeof EndEnhancer === 'string';

  if (process.env.NODE_ENV !== "production") {
    if (isEndEnhancerString && EndEnhancerDescription) {
      console.warn('endEnhancerDescription will not be rendered if endEnhancer is not a string');
    }

    if (maxLines && !isMaxLinesValid(maxLines)) {
      console.warn('maxLines must be 1 or 2.');
    }
  }

  return /*#__PURE__*/React.createElement(Root, _extends({
    ref: ref
  }, rootProps), /*#__PURE__*/React.createElement(Content, contentProps, /*#__PURE__*/React.createElement(StyledHeadingContentRow, null, /*#__PURE__*/React.createElement(HeadingContainer, _extends({
    $maxLines: isMaxLinesValid(maxLines) ? maxLines : 1
  }, headingContainerProps), /*#__PURE__*/React.createElement(RenderNode, {
    component: props.heading
  })), EndEnhancer && /*#__PURE__*/React.createElement(EndEnhancerContainer, _extends({
    $isText: isEndEnhancerString
  }, endEnhancerContainerProps), /*#__PURE__*/React.createElement(RenderNode, {
    component: EndEnhancer
  }))), (Boolean(SubHeading) || EndEnhancerDescription) && /*#__PURE__*/React.createElement(StyledHeadingContentRow, null, /*#__PURE__*/React.createElement(SubHeadingContainer, _extends({
    $maxLines: isMaxLinesValid(maxLines) ? maxLines : 1
  }, subHeadingContainerProps), /*#__PURE__*/React.createElement(RenderNode, {
    component: SubHeading
  })), Boolean(EndEnhancerDescription) && isEndEnhancerString && /*#__PURE__*/React.createElement(EndEnhancerDescriptionContainer, endEnhancerDescriptionContainerProps, /*#__PURE__*/React.createElement(RenderNode, {
    component: EndEnhancerDescription
  })))));
});
ListHeading.displayName = 'ListHeading';
export default ListHeading;