<template>
	<img
		:src="get1X"
		:srcset="`${get1X} 1x, ${get2X} 2x`"
		:alt="alt"
		aria-hidden="true"
	/>
</template>

<script>
import { imageUrl } from '@/helpers/utils'

export default {
	name: 'ImageTag',

	props: {
		src: {
			type: String,
			required: true,
		},
		srcset: {
			type: Boolean,
			default: true,
		},
		alt: {
			type: String,
			default: '',
		},
	},

	computed: {
		/**
		 * Get the default image url.
		 *
		 * @since 1.8.0
		 *
		 * @returns {string}
		 */
		get1X() {
			return imageUrl(this.src)
		},

		/**
		 * Get the 2x image path.
		 *
		 * @since 1.8.0
		 *
		 * @returns {string}
		 */
		get2X() {
			if (this.srcset) {
				return imageUrl(this.src.replace(/(\.[\w\d_-]+)$/i, '@2x$1'))
			} else {
				return false
			}
		},
	},
}
</script>
