/**
 * WordPress dependencies
 */
import { _x, sprintf } from '@safe-wordpress/i18n';

/**
 * External dependencies
 */
import {
	useIsUserYou,
	useAuthorName,
	useSocialProfile,
} from '@nelio-content/data';
import { dateI18n, getSettings as getDateSettings } from '@nelio-content/date';
import type { Uuid } from '@nelio-content/types';

export type ProfileCreatorProps = {
	readonly className?: string;
	readonly profileId: Uuid;
};

export const ProfileCreator = ( {
	className,
	profileId,
}: ProfileCreatorProps ): JSX.Element | null => {
	const profile = useSocialProfile( profileId );
	const isYou = useIsUserYou( profile?.creatorId );
	const creatorName = useAuthorName( profile?.creatorId );
	const creationDate = profile?.creationDate;

	if ( ! profile ) {
		return null;
	}

	if ( isYou ) {
		return (
			<div className={ className }>
				{ sprintf(
					/* translators: %s: Date. */
					_x( 'Added by you on %s', 'text', 'nelio-content' ),
					dateI18n( getDateSettings().formats.date, creationDate )
				) }
			</div>
		);
	}

	return (
		<div className={ className }>
			{ !! creatorName
				? sprintf(
						/* translators: %1$s: User name. %2$s: Date. */
						_x( 'Added by %1$s on %2$s', 'text', 'nelio-content' ),
						creatorName,
						dateI18n( getDateSettings().formats.date, creationDate )
				  )
				: sprintf(
						/* translators: %s: Date. */
						_x( 'Added on %s', 'text', 'nelio-content' ),
						dateI18n( getDateSettings().formats.date, creationDate )
				  ) }
		</div>
	);
};
