---
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {}
const { ...props } = Astro.props as Props;
---

<a {...props} data-vtbot-back><slot /></a>
<script>
	document.addEventListener('astro:before-preparation', (e) => {
		if (e.navigationType === 'traverse') {
			const originalLoader = e.loader;
			e.loader = async () => {
				await originalLoader();
				if (
					[...e.newDocument.querySelectorAll<HTMLAnchorElement>('a[data-vtbot-back]')].find(
						(el) => new URL(el.href, e.to).href === e.from.href
					)
				) {
					e.direction = e.direction === 'back' ? 'forward' : 'back';
				}
			};
		} else if (e.sourceElement && e.sourceElement.hasAttribute('data-vtbot-back'))
			e.direction = 'back';
	});
</script>
