import { React, PropTypes, classnames } from '@gravityforms/libraries';
import { spacerClasses } from '@gravityforms/utils';

const { forwardRef } = React;

/**
 * @module Heading
 * @description The heading component.
 *
 * @since 1.1.15
 *
 * @param {object}                     props                  Component props.
 * @param {JSX.Element}                props.children         React element children.
 * @param {string}                     props.content          The text content.
 * @param {object}                     props.customAttributes Custom attributes for the component.
 * @param {string|Array|object}        props.customClasses    Custom classes for the component.
 * @param {string}                     props.size             The font size for the heading.
 * @param {string|number|Array|object} props.spacing          The spacing for the component, as a string, number, array, or object.
 * @param {string}                     props.tagName          The tag used for the heading, from `h1` to `h6`.
 * @param {string}                     props.type             The type of the heading, one of `regular` or `boxed`.
 * @param {string}                     props.weight           The font weight for the heading.
 * @param {object|null}                ref                    Ref to the component.
 *
 * @return {JSX.Element} The heading component.
 *
 * @example
 * import Heading from '@gravityforms/components/react/admin/elements/Heading';
 *
 * return <Heading tagName="h2">{ 'Heading text' }</Heading>;
 *
 */
const Heading = forwardRef( ( {
	children = null,
	content = '',
	customAttributes = {},
	customClasses = [],
	size = 'display-3xl',
	spacing = '',
	tagName = 'h1',
	type = 'regular',
	weight = 'semibold',
}, ref ) => {
	const headingAttributes = {
		className: classnames( {
			'gform-heading': true,
			'gform-text': true,
			[ `gform-typography--size-${ size }` ]: true,
			[ `gform-typography--weight-${ weight }` ]: true,
			[ `gform-heading--${ type }` ]: true,
			...spacerClasses( spacing ),
		}, customClasses ),
		ref,
		...customAttributes,
	};

	const Container = tagName;

	return (
		<Container { ...headingAttributes }>
			{ content }
			{ children }
		</Container>
	);
} );

Heading.propTypes = {
	children: PropTypes.oneOfType( [
		PropTypes.arrayOf( PropTypes.node ),
		PropTypes.node,
	] ),
	content: PropTypes.string,
	customAttributes: PropTypes.object,
	customClasses: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.array,
		PropTypes.object,
	] ),
	size: PropTypes.string,
	spacing: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.number,
		PropTypes.array,
		PropTypes.object,
	] ),
	tagName: PropTypes.string,
	type: PropTypes.string,
	weight: PropTypes.string,
};

Heading.displayName = 'Heading';

export default Heading;
