import { createElement } from '@wordpress/element'
import { format, getSettings } from '@wordpress/date'
import { withSelect } from '@wordpress/data'

import { __ } from 'ct-i18n'

import { useEntityProp } from '@wordpress/core-data'

const DatePreview = ({
	postId,
	postType,

	attributes: {
		date_type,
		default_format,
		date_format,
		custom_date_format,
		has_field_link,
		has_field_link_wrap_content,
	},
}) => {
	const [date] = useEntityProp(
		'postType',
		postType,
		date_type === 'published' ? 'date' : 'modified',
		postId
	)

	const dateFormat =
		default_format === 'yes'
			? getSettings().formats.date
			: date_format !== 'custom'
			? date_format
			: custom_date_format

	let content = <span>{format(dateFormat, date)}</span>

	if (!postId) {
		return 'Date'
	}

	if (has_field_link === 'yes' && has_field_link_wrap_content === 'no') {
		content = <a href="#">{content}</a>
	}

	return content
}

export default DatePreview
