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

const { forwardRef } = React;

/**
 * @module Link
 * @description A link component in React.
 *
 * @since 1.1.15
 *
 * @param {object}                     props                  Component props.
 * @param {JSX.Element|null}           props.children         React element children.
 * @param {string}                     props.content          Text content for the link.
 * @param {object}                     props.customAttributes Custom attributes for the component.
 * @param {string|Array|object}        props.customClasses    Custom classes for the component.
 * @param {string}                     props.href             The href attribute for the link.
 * @param {string|number|Array|object} props.spacing          The spacing for the component, as a string, number, array, or object.
 * @param {string}                     props.target           The target attribute for the link.
 * @param {object|null}                ref                    Ref to the component.
 *
 * @return {JSX.Element} The link component.
 *
 * @example
 * import Link from '@gravityforms/components/react/admin/elements/Link';
 *
 * return <Link href="https://path.to.link/" target="_blank">{ 'Click me' }</Link>;
 *
 */
const Link = forwardRef( ( {
	children = null,
	content = '',
	customAttributes = {},
	customClasses = [],
	href = '',
	size = 'text-sm',
	spacing = '',
	target = '',
	weight = 'regular',
}, ref ) => {
	const componentProps = {
		className: classnames( {
			'gform-link': true,
			[ `gform-typography--size-${ size }` ]: true,
			[ `gform-typography--weight-${ weight }` ]: true,
			...spacerClasses( spacing ),
		}, customClasses ),
		href,
		target,
		ref,
		...customAttributes,
	};
	if ( target === '_blank' ) {
		componentProps.rel = 'noopener';
	}

	return (
		<a { ...componentProps }>
			{ content }
			{ children }
		</a>
	);
} );

Link.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,
	] ),
	href: PropTypes.string,
	spacing: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.number,
		PropTypes.array,
		PropTypes.object,
	] ),
	target: PropTypes.string,
};

Link.displayName = 'Link';

export default Link;
