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

const { forwardRef } = React;

/**
 * @module Label
 * @description A label component.
 *
 * @since 1.1.18
 *
 * @param {object}                     props                  Component props.
 * @param {JSX.Element|string}         props.children         React element children.
 * @param {object}                     props.customAttributes Custom attributes for the component.
 * @param {string|Array|object}        props.customClasses    Custom classes for the component.
 * @param {string}                     props.htmlFor          The for attribute for the label.
 * @param {string}                     props.icon             The icon for the label tooltip.
 * @param {string}                     props.iconPrefix       The icon prefix for the label.
 * @param {boolean}                    props.isVisible        If label is visible (true) or hidden (false).
 * @param {string}                     props.label            Label text.
 * @param {boolean}                    props.locked           If the label is locked (disables form element and puts an icon with optional tooltip next to label).
 * @param {string}                     props.lockedMessage    The message to display when the label is locked, in a tooltip. (optional)
 * @param {string}                     props.size             The font size for the label.
 * @param {string|number|Array|object} props.spacing          The spacing for the component, as a string, number, array, or object.
 * @param {string}                     props.weight           The font weight for the label.
 * @param {object|null}                ref                    Ref to the component.
 *
 * @return {JSX.Element} The Label component.
 *
 * @example
 * import Label from '@gravityforms/components/react/admin/elements/Label';
 *
 * return <Label htmlFor="input-id">{ 'Input label' }</Label>;
 *
 */
const Label = forwardRef( ( {
	children = null,
	customAttributes = {},
	customClasses = [],
	htmlFor = '',
	icon = 'lock',
	iconPrefix = 'gform-icon',
	isVisible = true,
	label = '',
	locked = false,
	lockedMessage = '',
	size = 'text-sm',
	spacing = '',
	weight = 'medium',
}, ref ) => {
	if ( ! label && ! children ) {
		return null;
	}

	const componentProps = {
		className: classnames( {
			'gform-label': true,
			[ `gform-typography--size-${ size }` ]: true,
			[ `gform-typography--weight-${ weight }` ]: true,
			'gform-visually-hidden': ! isVisible,
			...spacerClasses( spacing ),
		}, customClasses ),
		htmlFor,
		ref,
		...customAttributes,
	};

	const tooltipProps = {
		content: lockedMessage,
		icon,
		iconPrefix,
		iconPreset: 'status-locked',
		maxWidth: 300,
		position: 'right',
		spacing: [ 0, 0, 0, 2 ],
		tagName: 'span',
		theme: 'port',
	};

	return (
		<label { ...componentProps }>
			{ label }
			{ children }
			{ locked && <Tooltip { ...tooltipProps } /> }
		</label>
	);
} );

Label.propTypes = {
	children: PropTypes.oneOfType( [
		PropTypes.arrayOf( PropTypes.node ),
		PropTypes.node,
	] ),
	customAttributes: PropTypes.object,
	customClasses: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.array,
		PropTypes.object,
	] ),
	htmlFor: PropTypes.string,
	iconPrefix: PropTypes.string,
	isVisible: PropTypes.bool,
	label: PropTypes.string,
	locked: PropTypes.bool,
	lockedMessage: PropTypes.string,
	size: PropTypes.string,
	spacing: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.number,
		PropTypes.array,
		PropTypes.object,
	] ),
	weight: PropTypes.string,
};

Label.displayName = 'Label';

export default Label;
