( function () {
	'use strict';

	var btn = document.querySelector( '.wuppi-back-to-top' );

	if ( ! btn ) {
		return;
	}

	// Remove the HTML hidden attribute — JS takes control from here.
	btn.removeAttribute( 'hidden' );

	var SCROLL_THRESHOLD = 300;
	var ticking = false;

	function updateVisibility() {
		ticking = false;
		var scrollY = window.scrollY || window.pageYOffset || 0;

		if ( scrollY > SCROLL_THRESHOLD ) {
			btn.classList.add( 'is-visible' );
		} else {
			btn.classList.remove( 'is-visible' );
		}
	}

	function onScroll() {
		if ( ticking ) {
			return;
		}

		ticking = true;
		window.requestAnimationFrame( updateVisibility );
	}

	btn.addEventListener( 'click', function () {
		var prefersReducedMotion =
			window.matchMedia &&
			window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches;

		window.scrollTo( {
			top: 0,
			behavior: prefersReducedMotion ? 'auto' : 'smooth',
		} );
	} );

	window.addEventListener( 'scroll', onScroll, { passive: true } );

	// Set initial state on load.
	updateVisibility();
}() );
