function goBack() {
	sessionStorage.removeItem('selectedProduct')
	window.location.href = 'index.html'
}

document.addEventListener('DOMContentLoaded', () => {
	const productData = sessionStorage.getItem('selectedProduct')
	if (!productData) {
		console.log(`No product!!`)
		goBack()
	}

	const product = JSON.parse(productData)

	const playButtonContainer = document.getElementById('play-button-container')
	playButtonContainer.addEventListener('click', () => {
		togglePlayPause(playButtonContainer, getAudioDemoPath(product))
	})

	document.getElementById('product-title').textContent = product.name
	document.getElementById('detail-image').src = getImagePath(product)

	let header = `<div>${product.numPads} samples</div><div>${product.numKits} kits</div><div>${product.genre}</div>`
	document.getElementById('detail-info-header').innerHTML = header
	let description = '<hr />'
	description += product.description
	description += '<hr /><p>'
	if (product.freeToy && product.freeToy.trim() !== '') {
		description += `Free toy: ${product.freeToy}<br />`
	}

	description += `Credits: <a href=${product.authorLink}>${product.author}</a><br />`
	description += `Tags: ${product.tags.join(', ')}</p>`
	description += '<hr />'
	description += '<p>This sample pack is royalty-free for music creation - no resale, redistribution, or use in other sample packs or products - please see the <a href="https://www.koalasampler.com/sample-pack-license.html">full license</a> for details.</p>'
	document.getElementById('detail-body').innerHTML = description

	document.getElementById('buy-now').addEventListener('click', async () => {
		setPriceSpinnerVisible(true)
		// This must be delayed by a small amount of time to allow the spinner to show
		setTimeout(() => {
			purchase(product.productId)
		}, 50)
	})

	document.getElementById('go-to-pack').addEventListener('click', () => {
		openPack(product.productId)
	})

	getPackPrice(product.productId)
	getPackOwnership(product.productId)
})
